[wp-trac] [WordPress Trac] #25644: strip_shortcodes always removes text between shortcode tags, should be optional

WordPress Trac noreply at wordpress.org
Sun Oct 20 19:24:40 UTC 2013


#25644: strip_shortcodes always removes text between shortcode tags, should be
optional
-------------------------+-----------------------------
 Reporter:  jonscaife    |      Owner:
     Type:  enhancement  |     Status:  new
 Priority:  normal       |  Milestone:  Awaiting Review
Component:  Shortcodes   |    Version:  3.6.1
 Severity:  trivial      |   Keywords:  has-patch
-------------------------+-----------------------------
 strip_shortcodes will always remove all of the content between shortcode
 tags.  So, for example, if I have a shortcode tag which wraps a link or a
 style around some text the text is lost when the shortcode is removed.
 Example:
 ''Lorem ipsum [highlight]dolor[ /highlight] sit amet, consectetur
 adipisicing elit''
 becomes
 ''Lorem ipsum sit amet, consectetur adipisicing elit''
 It should become
 ''Lorem ipsum '''dolor''' sit amet, consectetur adipisicing elit''

 Removing the content between shortcodes may often be desirable behaviour,
 but there should be some way to retain the content.  The easiest way would
 be for strip_shortcodes() to take a second parameter which defaults to
 true to remove the content, but if it is false then it leaves the content
 between the tags

 Example change to wp-includes/shortcodes.php

 Before


 {{{
 function strip_shortcodes( $content ) {
         global $shortcode_tags;

         if (empty($shortcode_tags) || !is_array($shortcode_tags))
                 return $content;

         $pattern = get_shortcode_regex();

         return preg_replace_callback( "/$pattern/s",
 'strip_shortcode_tag', $content );
 }

 function strip_shortcode_tag( $m ) {
         // allow [[foo]] syntax for escaping a tag
         if ( $m[1] == '[' && $m[6] == ']' ) {
                 return substr($m[0], 1, -1);
         }

         return $m[1] . $m[6];
 }
 }}}

 After


 {{{
 function strip_shortcodes( $content, $strip_between = true ) {
         global $shortcode_tags;

         if (empty($shortcode_tags) || !is_array($shortcode_tags))
                 return $content;

         $pattern = get_shortcode_regex();

         if($strip_between==true) return preg_replace_callback(
 "/$pattern/s", 'strip_shortcode_tag', $content );
         else return preg_replace_callback( "/$pattern/s",
 'strip_shortcode_tag_notbetween', $content );
 }

 function strip_shortcode_tag( $m ) {
         // allow [[foo]] syntax for escaping a tag
         if ( $m[1] == '[' && $m[6] == ']' ) {
                 return substr($m[0], 1, -1);
         }

         return $m[1] . $m[6];
 }

 function strip_shortcode_tag_notbetween( $m ) {
         // allow [[foo]] syntax for escaping a tag
         if ( $m[1] == '[' && $m[6] == ']' ) {
                 return substr($m[0], 1, -1);
         }

         return $m[1] . $m[5] . $m[6];
 }
 }}}

 It's probably possible to do this with slicker code, but this is fairly
 simple and works.

 An example of when this problem is encountered in the real world is with
 the [http://wordpress.org/plugins/rb-internal-links/ RB internal links
 plugin] being used in post content.  When the post is displayed by the
 [http://wordpress.org/plugins/popular-widget/ popular widget plugin] any
 text which was internally linked is lost, leaving a snippet of the post
 which makes no sense to a human reader.  For an example on a live site see
 the first entry under the '''Most popular (all time)''' section on the
 right hand side of [http://diymediahome.org DIY Media Home]

--
Ticket URL: <http://core.trac.wordpress.org/ticket/25644>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list