• Resolved Drew Baker

    (@dbaker)


    Hey Guys,
    I’m building a CMS type site here and would like to make it so that if you view index.php (or home.php) then all the shortcodes are striped from the posts (the_content). When viewing another page like single.php then the shortcodes should be left along.

    I think I need to use this code, but I’m not sure how:
    <?php strip_shortcodes( $content ); ?>

    This is the Codex page for it.

    The reason for doing this is that my main page (home.php and index.php) get messed up when a Viper Video or Gallery shortcode get used, but obviously I want them to work when viewing them on single.php.

    Thanks for the help!

Viewing 7 replies - 1 through 7 (of 7 total)
  • Going to guess you would need something similar to:

    function remove_shortcode_from_index($content) {
      if ( is_home() ) {
        $content = strip_shortcodes( $content );
      }
      return $content;
    }
    add_filter('the_content', 'remove_shortcode_from_index');
    Thread Starter Drew Baker

    (@dbaker)

    This example is perfect! Thanks Michael! Is there a way to make it work for the_content tags that are in a certain DIV perhaps?

    It’s that now that I have this working I released I have a part on my index.php page that shows a Video Highlight and that uses shortcodes to display a Viper Video. Here is the code I use to pull the latest post to show the video:

    <?php $my_query = new WP_Query('cat=1532&showposts=1');
    while ($my_query->have_posts()) : $my_query->the_post();
    $do_not_duplicate = $post->ID;?>
    
    <?php the_content(''); ?>
    
    <?php endwhile; ?>

    Thanks again! I’m going to add that code as an example on the Codex for strip_shortcodes.

    Thread Starter Drew Baker

    (@dbaker)

    Michael, looking through the codex page for conditional tags, it’s seems I need to combine the above with something using the is_category(‘Video Highlight’) tag, but I’m not sure what the correct PHP code should be (I suck at PHP).

    Thanks again!

    dbaker – okay I put that example in the Codex Function_Reference/strip_shortcodes#Examples. Thanks for the suggestion.

    Also note you may want to keep an eye on this trac ticket if you have problems:
    http://core.trac.wordpress.org/ticket/10082

    Finally, see Conditional Tags related to your last comment.

    Thread Starter Drew Baker

    (@dbaker)

    Hey Micheal,
    Thanks so much for your detailed response.

    I think the below code is the correct code for going through my home.php file, and stripping all shortcodes except for the posts that are in cat=438 (my video highlight category). But it gives a syntax error saying the ‘else’ line is bad. Like I said, my PHP sucks so any suggestions are greatly appreciated.

    function remove_shortcode_from_index($content) {
      if ( in_category('438') ) {
        $content = $content;
      }
      return $content;
    }
    
    else ( is_home() ) {
        $content = strip_shortcodes( $content );
      }
      return $content;
    }
    add_filter('the_content', 'remove_shortcode_from_index');

    Thanks!

    If ‘home’ and not in category 438

    function remove_shortcode_from_index($content) {
      if ( is_home() && !in_category('438') ) {
        $content = strip_shortcodes( $content );
      }
      return $content;
    }
    add_filter('the_content', 'remove_shortcode_from_index');
    Thread Starter Drew Baker

    (@dbaker)

    Michael,
    That’s amazing. I really have to learn PHP, you can do so much with it!

    Thanks a lot!
    DB

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘How to strip shortcodes if on index.php?’ is closed to new replies.