Make WordPress Core

Opened 2 years ago

Last modified 8 days ago

#25435 reopened feature request

Introduce alternative to do_shortcode( '[shortcode]' )

Reported by: jdgrimes Owned by:
Milestone: Future Release Priority: normal
Severity: normal Version:
Component: Shortcodes Keywords: has-patch
Focuses: Cc:


There are times when we want to call a shortcode programmatically. Currently, the easiest way to do this is do_shortcode( '[shortcode]' ). But that does lots of unnecessary work. A developer has two alternatives:

  1. Manually search through the source and find the function that handles the shortcode, and call it directly. The only problem is that this doesn't offer a very forward-compatible solution, especially when the shortcode is being offered by a plugin.
  2. Search through $shortcode_tags to find and call the function. That's more forward-compatible, but it kind of seems hacky.

I'd like to request that we offer a core function that does number 2 rather than each developer having to implement it themselves.


call_shortcode_func( 'shortcode', $atts, $content );

(We can change the name of the function.)

Patch forthcoming.

Attachments (1)

25435.diff (1002 bytes) - added by jdgrimes 2 years ago.
Introduces call_shortcode_func()

Download all attachments as: .zip

Change History (12)

@jdgrimes2 years ago

Introduces call_shortcode_func()

comment:1 follow-up: @DrewAPicture2 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed

You can just call the shortcode's callback function directly.

So for instance, the [gallery] shortcode callback is gallery_shortcode(), and you can do:

echo gallery_shortcode( $attr );

comment:2 in reply to: ↑ 1 @jdgrimes2 years ago

  • Resolution invalid deleted
  • Status changed from closed to reopened

Replying to DrewAPicture:

You can just call the shortcode's callback function directly.

So for instance, the [gallery] shortcode callback is gallery_shortcode(), and you can do:

echo gallery_shortcode( $attr );

Yes, I know that and mentioned it above, but it becomes a bigger issue when dealing with plugins, which may change the function name without notice. I guess maybe that's an edge case though?

My thinking was that if we make people search through the source looking for the right function to call, they'll just call do_shortcode( '[shortcode]' ) instead. If we can point them to an easy to use core function, they'll probably use it.

I think this is the best solution to the problem, but if you don't think it's a real problem, or isn't big enough to require this solution, I'll respect that.

comment:3 @wonderboymusic2 years ago

  • Resolution set to wontfix
  • Status changed from reopened to closed

What Drew said - some wrappers cause too much reflection, like the one you are proposing

comment:4 @rmccue22 months ago

+1 in favour of reopening this.

It's certainly possible to call the callback directly if you know it, but that's not necessarily the case. Shortcodes are pretty fluid, and it's possible that a plugin could change the gallery shortcode (e.g.) to its own instead.

The current solutions are either extremely heavy (do_shortcode) or require an obscure way to call them (do_shortcode_tag).

I think we should add something like do_single_shortcode( 'gallery', [ 'a' => 'b' ], 'optional body content' ).

comment:5 @rmccue21 months ago

  • Milestone set to Future Release
  • Resolution wontfix deleted
  • Status changed from closed to reopened

Reopening, because rmccue-likes.

comment:6 follow-up: @nacin21 months ago

Nested shortcodes pose a bit of a problem here.

comment:7 in reply to: ↑ 6 @jdgrimes21 months ago

Replying to nacin:

Nested shortcodes pose a bit of a problem here.

Not sure what you mean by that. If you are using nested shortcodes, then you should be using do_shortcode() instead of this, or calling the nested ones like this before passing the the result in as the $content.

comment:8 @nacin20 months ago

#19806 was marked as a duplicate.

comment:9 @Viper007Bond16 months ago

I like this.

Nested shortcodes shouldn't be a problem -- they would just call do_shortcode() like you do now. This just allows skipping calling do_shortcode() when you know what the shortcode/content is, unlike a nested shortcode.

comment:10 @rmccue16 months ago

I'm still +1 on this. :)

comment:11 @miqrogroove8 days ago

  • Summary changed from Intoduce function for use instead of do_shortcode( '[shortcode]' ) to Introduce alternative to do_shortcode( '[shortcode]' )
Note: See TracTickets for help on using tickets.