Opened 8 years ago
Closed 8 years ago
#37767 closed enhancement (fixed)
Conditionally modify $tagnames in strip_shortcodes()
Reported by: | danielbachhuber | Owned by: | aaroncampbell |
---|---|---|---|
Milestone: | 4.7 | Priority: | normal |
Severity: | normal | Version: | |
Component: | Shortcodes | Keywords: | good-first-bug has-patch has-unit-tests |
Focuses: | Cc: |
Description
As a WordPress developer, I may want to use strip_shortcodes()
in a couple novel ways:
- Strip all shortcodes except one or more shortcodes.
- Strip one or more shortcodes, instead of all shortcodes.
If it was possible to conditionally modify $tagnames
, I could use strip_shortcodes()
in this way without modifying the $shortcode_tags
global.
Two potential ways this could be implemented:
- Include a second
$options = array()
argument, which supportsinclude
orexclude
as options. - Filter the
$tagnames
variable to permit conditional modification.
Attachments (4)
Change History (24)
This ticket was mentioned in Slack in #core by orvils. View the logs.
8 years ago
#4
@
8 years ago
- Keywords has-unit-tests added; needs-unit-tests needs-testing removed
- Milestone changed from Future Release to 4.7
Thanks for your updated patch @orvils! It works really well and the tests look good too. Note that you can add the has-unit-tests
keyword in such a case. I'm moving this to the 4.7 milestone for consideration :-)
I think we can simplify the patch a bit though. There's no need for $tag_array
to be a key => value array. Also, there are already a bunch of shortcodes registered in the test suite we can use instead of adding another one.
In 37767.diff I moved the tests to a data provider which makes it easier to add additional tests if needed. Plus I changed the new code slightly to follow the WordPress coding and documentation standards.
#5
@
8 years ago
@aaroncampbell As you're the maintainer of the shortcodes component, would you mind reviewing the latest patch? Thanks in advance.
This ticket was mentioned in Slack in #core by jeffpaul. View the logs.
8 years ago
#7
follow-up:
↓ 8
@
8 years ago
- Keywords reporter-feedback added; has-patch has-unit-tests removed
First of all, I do like the idea of being able to strip only certain shortcodes. Having said that, I'm not sold on the approach here. If you're trying to protect a certain shortcode, it might be nice to do it everywhere. I could pretty easily imagine a plugin that has something like a footnote shortcode and would want to be able to leave it in excerpts (where core calls strip_shortcodes()
).
What are the downsides of just making the $tagnames
array filterable (before it's intersected with the matches). A filter name like strip_shortcode_tagnames
seems reasonable.
#8
in reply to:
↑ 7
@
8 years ago
Replying to aaroncampbell:
What are the downsides of just making the
$tagnames
array filterable (before it's intersected with the matches). A filter name likestrip_shortcode_tagnames
seems reasonable.
I'd be amenable to taking the filter approach instead of introducing another supplied argument.
I think the downside to the filter approach is that you'd need to create the filter before using strip_shortcodes()
, and then remove it afterwords. Filter definition is easy enough with closures though, and does grant more flexibility.
#9
@
8 years ago
Yeah a filter definitely makes sense there. What about doing both, adding a filter and a new argument?
#10
@
8 years ago
It seems like doing both is maybe overkill? Think of it in terms of "if we had the filter so this was already possible, would we add the parameter?" Is the case of "I have to add and remove the filter" strong enough to justify that? I don't think so. Are there more compelling use cases?
#11
@
8 years ago
- Owner set to aaroncampbell
- Status changed from new to assigned
Assigning to @aaroncampbell as the component maintainer
This ticket was mentioned in Slack in #core by desrosj. View the logs.
8 years ago
#13
@
8 years ago
Per bugscrub talk in Slack, there is no problem with the filter, but a more compelling argument needs to be made for adding a parameter here.
#14
@
8 years ago
- Keywords has-patch has-unit-tests added; reporter-feedback removed
37767.2.diff only adds a new filter, not an additional parameter.
This ticket was mentioned in Slack in #core by jeffpaul. View the logs.
8 years ago
#17
@
8 years ago
- Resolution fixed deleted
- Status changed from closed to reopened
hmm, I don't know but i try use code but this don't work.
<?php add_filter('strip_shortcodes_tagnames', function(){ return array( 'gallery' ); });
My content:
before [gallery link="file" ids="603,582,580,578"] after
but this work:
before [gallery] after
#18
@
8 years ago
@sebastian.pisula How are you calling strip_shortcodes()
?
This should work totally fine. There's a unit test to prove that.
<?php add_filter('strip_shortcodes_tagnames', function(){ return array( 'gallery' ); }); $output = strip_shortcodes( 'before [gallery link="file" ids="603,582,580,578"] after' );
Add's additional param to the strip_shortcode function which can be used to override the array of tags being processed. If it is set, the global variable is ignored. Meaning it will only allow the shortcodes passed in optionally