WordPress.org

Ready to get started?Download WordPress

Forums

How do you strip shortcodes from search pages? (4 posts)

  1. kangel
    Member
    Posted 3 years ago #

    Hi -
    The posts' image shortcodes are appearing in my teaser on all archive pages. I've tried removing them by inserting this code into my functions.php:

    add_filter('the_content', 'strip_shortcodes');
    
    function remove_shortcode_from_index($content) {
      if ( is_search() ) {
        $content = strip_shortcodes( $content );
      }
      return $content;
    }

    However, this is only removing the shortcode from my actual posts (which removes the post's image and caption), but does not remove the shortcode from all archive listings and search results.

    Can anyone help with this functions.php code? Or should I change the wp code in shortcodes.php?

    Thanks so much.

  2. vtxyzzy
    Member
    Posted 3 years ago #

    You are close! This is UNTESTED, but I think it will work (don't know about the is_search() part):

    add_filter('the_excerpt', 'remove_shortcode_from_excerpt');
    
    function remove_shortcode_from_excerpt($excerpt) {
      if ( is_search() ) {
        $excerpt = strip_shortcodes( $excerpt );
      }
      return $excerpt;
    }
  3. kangel
    Member
    Posted 3 years ago #

    Thanks vtxyzzy—I tried the code and it didn't seem to work.

    The theme is built with 'the_content' showing all teasers so we have more flexibility with filters and styling. I think my big problem is the 'is_search' function, since it's not working like the is_home is.

    Is there a different way to tell all search result pages to hide shortcodes?

    Reference to is_search is found on the wordpress doc here towards the lower half of the page.

  4. vtxyzzy
    Member
    Posted 3 years ago #

    I don't know about alternatives to is_search(). You might need to create a new topic for that question to get others to take a look.

Topic Closed

This topic has been closed to new replies.

About this Topic