Make WordPress Core

Opened 8 years ago

Closed 7 years ago

#37767 closed enhancement (fixed)

Conditionally modify $tagnames in strip_shortcodes()

Reported by: danielbachhuber's profile danielbachhuber Owned by: aaroncampbell's profile 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:

  1. Strip all shortcodes except one or more shortcodes.
  2. 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:

  1. Include a second $options = array() argument, which supports include or exclude as options.
  2. Filter the $tagnames variable to permit conditional modification.

Attachments (4)

37767.patch (1.2 KB) - added by DylanAuty 8 years ago.
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
rde-37767.patch (3.2 KB) - added by orvils 8 years ago.
Improved version of strip_shortcodes with tests
37767.diff (3.4 KB) - added by swissspidy 7 years ago.
37767.2.diff (3.0 KB) - added by swissspidy 7 years ago.

Download all attachments as: .zip

Change History (24)

@DylanAuty
8 years ago

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

#1 @DylanAuty
8 years ago

  • Keywords has-patch needs-testing added; needs-patch removed

@orvils
8 years ago

Improved version of strip_shortcodes with tests

#2 @orvils
8 years ago

As this is my first patch, all feedback is welcome

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


7 years ago

@swissspidy
7 years ago

#4 @swissspidy
7 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 @swissspidy
7 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.


7 years ago

#7 follow-up: @aaroncampbell
7 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 @danielbachhuber
7 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 like strip_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 @swissspidy
7 years ago

Yeah a filter definitely makes sense there. What about doing both, adding a filter and a new argument?

#10 @aaroncampbell
7 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 @jorbin
7 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.


7 years ago

#13 @desrosj
7 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.

@swissspidy
7 years ago

#14 @swissspidy
7 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.


7 years ago

#16 @aaroncampbell
7 years ago

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

In 38877:

Shortcodes: Add new strip_shortcodes_tagnames filter.

With the new strip_shortcodes_tagnames filter you can specify which shortcodes are stripped by strip_shortcodes(). The default is all registered shortcodes.

Props DylanAuty, orvils, swissspidy.
Fixes #37767.

#17 @sebastian.pisula
7 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 @swissspidy
7 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' );

#19 @sebastian.pisula
7 years ago

yhym, I thought do_shortcode() calls strip_shortcodes() if user want remove shortcodes via filter.

#20 @swissspidy
7 years ago

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

strip_shortcodes() is only called by wp_trim_excerpt()

Note: See TracTickets for help on using tickets.