Support » Fixing WordPress » Stripping shortcodes, keeping the content

  • Resolved benPog



    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.

Viewing 15 replies - 1 through 15 (of 15 total)
  • esmi


    Forum Moderator

    Have you tried using strip_shortcodes?

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

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

    For instance,


    will display the text “[youtube=]” in the html column.



    Forum Moderator

    What about remove_shortcode?

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



    Forum Moderator

    What about using something like:

      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?

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

    Moderator keesiemeijer


    try it with this in your functions.php:

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

    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();

    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!

    Moderator keesiemeijer


    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>

    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) ?

    Moderator keesiemeijer


    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.

    Done =)

    Thanks again!

    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

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

    Thanks keesiemeijer for solving the problem.

Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘Stripping shortcodes, keeping the content’ is closed to new replies.