#37906 closed enhancement (fixed)
Filter to short-circuit do_shortcode_tag
Reported by: |
|
Owned by: |
|
---|---|---|---|
Milestone: | 4.7 | Priority: | normal |
Severity: | normal | Version: | |
Component: | Shortcodes | Keywords: | has-patch |
Focuses: | Cc: |
Description
Working for a client I noticed that there is no way for plugins to modify or short-circuit shortcode output.
My use case - I wanted to fragment-cache some resource-intensive shortcodes from other plugins. In my case I solved it by introducing new shortcode and calling do_shortcode
from inside, but I think a general filter for this would be good to have. We do have similar pre_
type of filters for http requests, get_option and elswhere, so introducing one for shortcodes would be good for consistency, to.
My suggestion is to introduce a pre_do_shortcode_tag
filter that would allow plugins to short-circuit and return their own value for any shortcode they choose. I am attaching a possible patch for this.
Attachments (3)
Change History (12)
#1
follow-ups:
↓ 2
↓ 4
@
8 years ago
- Keywords has-patch needs-unit-tests added
- Milestone changed from Awaiting Review to 4.7
- Version trunk deleted
Thanks for the suggestion, @ideag!
I like it! If you have some time, would you mind writing some unit tests for this new filter?
#2
in reply to:
↑ 1
@
8 years ago
Replying to pento:
If you have some time, would you mind writing some unit tests for this new filter?
I have the time, but I have no experience writing unit tests. I guess this could be a good chance to learn that :)
#3
@
8 years ago
I think #32790 might be related to this too, somehow I didn't see it in my search before.
#4
in reply to:
↑ 1
@
8 years ago
Replying to pento
I've added some unit tests for this patch. Please keep in mind this is my first time writing unit tests, so I might be doing things wrong.
#5
@
8 years ago
- Keywords needs-refresh added; needs-unit-tests removed
- Owner set to ideag
- Status changed from new to assigned
Nice work, @ideag!
There are a handful of small changes I'd like you to make.
- Please put the tests in
tests/shortcode.php
, nottests/filters.php
.filters.php
is just for testing the filter handling methods, testing an actual filter belongs in the file related to the area of code where the filter runs. - Don't use
rand_str()
to generate the shortcode. There's a chance (however small), that it could collide with an existing shortcode, unit tests should be as deterministic as possible. Naming the shortcode something related to the new filter will be unique, in the context of the tests. - We still support PHP 5.2, so the shortcode lambda functions actually need to be methods. The general naming convention for filter helper methods is to prefix them with
_filter_
, like test methods are prefixedtest_
. - Clean up at the end of the test - remove the filters and shortcode. We try to keep unit tests as self-contained as possible, to avoid side effects on tests that run later.
- Please put both the tests and the new filter in the same patch, it makes them easier for folks to apply to their dev installs.
All up, it's looking quite good, thank you for taking the time to get it done! :-)
a patch to introduce pre_do_shortcode_tag filter