Make WordPress Core

Opened 9 years ago

Closed 8 years ago

#32790 closed enhancement (fixed)

Filter for do_shortcode_tag()

Reported by: danielbachhuber's profile danielbachhuber Owned by: flixos90's profile flixos90
Milestone: 4.7 Priority: normal
Severity: normal Version:
Component: Shortcodes Keywords: has-patch has-unit-tests
Focuses: Cc:


As a WordPress developer with a collection of shortcodes, I want to include some standard JavaScript every time a shortcode in my collection is run.

In this pull request, I had to create a standard callback for all shortcodes in order to run wp_enqueue_script(). If I could filter the return value for do_shortcode_tag(), I could still enqueue my scripts, as well as modify the output of the shortcode.

Attachments (2)

32790.diff (1.0 KB) - added by flixos90 8 years ago.
32790.2.diff (3.9 KB) - added by flixos90 8 years ago.

Download all attachments as: .zip

Change History (9)

#1 @miqrogroove
9 years ago

  • Keywords needs-patch removed
  • Milestone changed from Future Release to Awaiting Review

#2 @bobbingwide
9 years ago

I had similar requirements.

  1. Where I wanted to do some generic processing based on the values of shortcode attributes.
  2. Where I wanted to post process the shortcode output: to support pagination
  3. And whatever I need to do to appease shortcake when I enqueue jQuery and CSS

Having already implemented all my shortcodes using the same handler
it was easy for me to add the new filter functions for 1. and 2.
I don't know how to do 3. yet. :-)


You may also want to look at #32445:3
where Nacin proposes rewriting the shortcode API to use hooks under the hood.

Perhaps if do_shortcode_tag() or do_shortcode() were pluggable then we'd be able to do all sorts of interesting things.

Maybe the function to invoke in the preg_replace_callback(,do_shortcode_tag, ) calls could be determined using a filter.

Let's hope this requirement gets considered in the shortcode roadmap.

8 years ago

#3 @flixos90
8 years ago

  • Keywords has-patch needs-unit-tests added

32790.diff introduces the do_shortcode_tag filter to allow filtering the shortcode output. This also allows us, in combination with 38506, to cache expensive shortcodes.

I also removed a bit of repetitive code before to only handle what's different between the two if-clauses (namely whether $content is passed to the shortcode callback or not).

#4 @pento
8 years ago

  • Milestone changed from Awaiting Review to 4.7
  • Owner set to flixos90
  • Status changed from new to assigned

32790.diff looks good. Just add some unit tests, and we're good to go! :-)

8 years ago

#5 @flixos90
8 years ago

  • Keywords has-unit-tests added; needs-unit-tests removed

32790.2.diff adds unit tests for the filter.

Last edited 8 years ago by ocean90 (previous) (diff)

This ticket was mentioned in Slack in #core by chriscct7. View the logs.

8 years ago

#7 @pento
8 years ago

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

In 38713:

Shortcodes: Add a do_shortcode_tag filter.

The addition of the pre_do_shortcode_tag in [38506] allows plugins to short-circuit the shortcode execution process, which is particularly helpful for caching expensive shortcodes.

The do_shortcode_tag is the corresponding part of that system - when a shortcode hasn't been executed previously, there needs to be a clean method of populating the cache.

Props flixos90.
Fixes #32790.

Note: See TracTickets for help on using tickets.