WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Stripping shortcodes, keeping the content (16 posts)

  1. benPog
    Member
    Posted 2 years ago #

    Hello.

    My problem is the following: I have text formatting shortcodes aimed at displaying the content in columns. Here is how it works:

    [one-half]Here is the content of the first column[/one-half]
    [one-half-last]And there is the content of the second column[/one-half-last]

    When using the_excerpt() function, shortcodes are automatically stripped (if no excerpts was found, ie. WordPress generates an automatic excerpts) and therefore, any content INSIDE the shortcodes is deleted. As my whole content is inside shortcodes for formatting purposes, that means is excerpt is basically an empty string.

    Is there an other function that strips only the '[shortcodes]' and '[/shortcodes]' tags, keeping the content between these two?

    Thanks in advance for your answer.
    Cheers,
    Ben.

  2. esmi
    Forum Moderator
    Posted 2 years ago #

    Have you tried using strip_shortcodes?

  3. benPog
    Member
    Posted 2 years ago #

    Yup. It gives the same result. All content between opening and closing tags [shortcode]content[/shortcode] is deleted...

  4. benPog
    Member
    Posted 2 years ago #

    Kind of similar problem, if adding a shortcode inside these shortcodes tags, the inner shortcode won't trigger the shortcode activation.

    For instance,

    [one-half][youtube=http://www.youtube.com?watch=xxxxxx][/one-half]

    will display the text "[youtube=http://www.youtube.com?watch=xxxxxx]" in the html column.

  5. esmi
    Forum Moderator
    Posted 2 years ago #

    What about remove_shortcode?

  6. benPog
    Member
    Posted 2 years ago #

    Apparently, remove_shortcode() only deletes the hook, ie. the shortcode text is displayed, but does not activate the function.

  7. esmi
    Forum Moderator
    Posted 2 years ago #

    What about using something like:

    <?php
      add_filter("the_content", "plugin_myContentFilter");
      function plugin_myContentFilter($content) {
        // Take the existing content and return a subset of it
        return substr($content, 0, 55);
      }
    ?>

    in functions.php?

  8. benPog
    Member
    Posted 2 years ago #

    This solution does not strip the shortcode tags. It displays the first 55 characters of the whole content (the shortcodes being displayed as text).

  9. keesiemeijer
    moderator
    Posted 2 years ago #

    try it with this in your functions.php:

    add_filter( 'the_excerpt', 'shortcode_unautop');
    add_filter( 'the_excerpt', 'do_shortcode');

    http://wordpress.stackexchange.com/questions/42743/the-excerpt-and-shortcodes

    Or use your own filter on get_the_excerpt. Put this in your theme's functions.php.

    function custom_excerpt($text = '') {
    	$raw_excerpt = $text;
    	if ( '' == $text ) {
    		$text = get_the_content('');
                    // $text = strip_shortcodes( $text );
    		$text = do_shortcode( $text );
    		$text = apply_filters('the_content', $text);
    		$text = str_replace(']]>', ']]>', $text);
    		$excerpt_length = apply_filters('excerpt_length', 55);
    		$excerpt_more = apply_filters('excerpt_more', ' ' . '[...]');
    		$text = wp_trim_words( $text, $excerpt_length, $excerpt_more );
    	}
    	return apply_filters('wp_trim_excerpt', $text, $raw_excerpt);
    }
    remove_filter( 'get_the_excerpt', 'wp_trim_excerpt'  );
    add_filter( 'get_the_excerpt', 'custom_excerpt'  );

    This will allow shortcodes in the_excerpt();

  10. benPog
    Member
    Posted 2 years ago #

    I think my problem wasn't understood. My bad for not having been clear enough.

    I don't want the shortcodes to be activated in the excerpts. I want the shortcodes to be hidden, but keep the text content between them (as my shortcodes are like opening and closing tags).

    By default, WordPress deletes all content inside the [shortcode]content[/shortcode] tags when using the strip_shortcodes() function (which I assume is used when displaying the_excerpt()).

    I guess the best solution would be a custom excerpt function using a regexp (that strips all "[*]" and "[/*]" expressions), but I'm not familiar with this.

    Thanks for your answers!

  11. keesiemeijer
    moderator
    Posted 2 years ago #

    I think the last code will strip the shortcode but keep the text inside the shortcode.

    if you have this in your content:

    [one-half]Here is the content of the first column[/one-half]
    [one-half-last]And there is the content of the second column[/one-half-last]

    the_excerpt() will output:

    <p>Here is the content of the first column And there is the content of the second column</p>
  12. benPog
    Member
    Posted 2 years ago #

    Sorry for the late answer, I did not have Internet access anymore.

    Yes, the second code proposition actually does the trick (except that the resulting content is not wrapped in <p> tags, but that's actually beneficial to my design). Thanks a lot!

    Is there any way to "close" this topic (like adding a [solved] tag) ?

  13. keesiemeijer
    moderator
    Posted 2 years ago #

    Is there any way to "close" this topic (like adding a [solved] tag) ?

    Yes, use the dropdown on the right side of this page (when logged in).

    Glad you got it resolved.

  14. benPog
    Member
    Posted 2 years ago #

    Done =)

    Thanks again!

  15. kraterdesign
    Member
    Posted 1 year ago #

    In case anyone needs to strip shortcodes, but KEEP <p> tags, you can also use a regular expression on the_content:

    $the_content = get_the_content();
    $the_content = preg_replace("~(?:\[/?)[^/\]]+/?\]~s", '', $the_content);  # strip shortcodes, keep shortcode content

    Or if you wish to keep specific shortcodes in, and strip the rest:

    $exclude_codes = 'shortcode_to_keep_1|keep_this_shortcode|another_shortcode_to_keep';
    
    $the_content = get_the_content();
    $the_content= preg_replace("~(?:\[/?)(?!(?:$exclude_codes))[^/\]]+/?\]~s", '', $the_content);  # strip shortcodes, keep shortcode content
  16. Banzboy
    Member
    Posted 1 year ago #

    Holy cow, I never expected that somebody else would have the same problem. Truly 1 in a million.

    Thanks keesiemeijer for solving the problem.

Topic Closed

This topic has been closed to new replies.

About this Topic