WordPress.org

Make WordPress Core

Opened 6 years ago

Closed 6 years ago

#32445 closed enhancement (fixed)

`is_callable()` should be checked in `do_shortcode_tag()`, not `add_shortcode()`

Reported by: wonderboymusic Owned by: wonderboymusic
Milestone: 4.3 Priority: normal
Severity: normal Version: 2.6
Component: Shortcodes Keywords: has-patch commit
Focuses: Cc:

Description

Actions and filters can be registered before their callbacks actually exist. The same should be true for shortcodes. This is a followup of #30947 - would be good to be able to add all shortcodes in default-filters.php

Attachments (2)

32445.diff (3.6 KB) - added by wonderboymusic 6 years ago.
32445.2.diff (893 bytes) - added by aaroncampbell 6 years ago.

Download all attachments as: .zip

Change History (9)

@wonderboymusic
6 years ago

#1 follow-up: @wonderboymusic
6 years ago

  • Keywords has-patch added; needs-patch removed

Another piece here - if you register a shortcode now without a valid callback, you get no notice in the logs and nothing happens to your shortcode (it isn't parsed).

32445.diff is all of the code that is required to make shortcode registration possible in default-filters.php. The only major diff is having to load shortcodes.php before default-filters.php in the bootstrap.

In my patch, when a shortcode is being parsed and doesn't have a valid callback, a _doing_it_wrong() is fired. _doing_it_wrong() can't be called on add_shortcode() in default-filters.php because i10n is not loaded.

#2 @aaroncampbell
6 years ago

  • Keywords needs-refresh added

The patch seems to work, passes all the tests, etc, but I'm a little leery of moving all our default shortcodes into default-filters.php. It all seems to work for core, but I'm convinced it won't cause issues elsewhere. Can we limit this patch to just the is_callable fix?

#3 @nacin
6 years ago

  • Keywords needs-refresh removed

Yeah, moving add_shortcode() calls to default-filters.php will break things in weird situations, like including a few random files outside of WordPress. I'm not saying we should ever support that (and I often argue, such as in the SHORTINIT situations, that we should not), but in this case, the only functions called in default-filters.php are add_action() and add_filter() (and is_admin()). It should stay that way.

I'd agree with moving them out of here, but it should probably go to some kind of shortcodes_init() like widgets and other things. Moving them too late could break plugins that currently remove them pretty early, however. Kind of stuck here.

I do like the callback change here, though.

(Random thought, if anyone wants to have some fun, we should rewrite the API to use hooks under the hood, and ditch $shortcode_tags. And then break everyone reaching directly into $shortcode_tags.)

#4 in reply to: ↑ 1 @aaroncampbell
6 years ago

  • Keywords commit added

32445.2.diff is wonderboymusic's 32445.diff without the moving of the add_shortcode() calls. I think this fixes the actual problem without the extra code churn.

#5 @wonderboymusic
6 years ago

  • Owner set to wonderboymusic
  • Status changed from new to assigned

#6 @wonderboymusic
6 years ago

In 32867:

Check is_callable() in do_shortcode_tag(), not add_shortcode().
Add a _doing_it_wrong() in do_shortcode_tag() when is_callable() is false.

Props aaroncampbell.
See #32445.

#7 @wonderboymusic
6 years ago

  • Resolution set to fixed
  • Status changed from assigned to closed

Done for now

Note: See TracTickets for help on using tickets.