Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm using the following function to create a custom excerpt for my homepage and categories pages so I can do it by character count and have a custom "read more". However, I have captions shortcode showing up in my excerpt.

Example:

[caption id="attachment_4656" align="aligncenter" width="450"] Crispy Vegetable Tacos[/caption] Do you sometimes feel overwhelmed when trying to decide what to make for dinner? What I’ve learned from my years as a family dinner planning expert ... continue reading

I tried adding line 5 to strip out the actual shortcode code but it isn't working.

Am I on the right track? I'd prefer the shortcodes not to show at all and I've used the function I've seen on the 'net but it isn't working (I'm guessing cause I'm using a custom excerpt function). Anyone want to help me out, please?

function get_excerpt($count){
   $permalink = get_permalink($post->ID);
   $excerpt = get_the_content();
   $excerpt = strip_tags($excerpt);
   $excerpt = str_replace(']]>', ']]>', $excerpt);
   $excerpt = substr($excerpt, 0, $count);
   $excerpt = substr($excerpt, 0, strripos($excerpt, " "));
   $excerpt = $excerpt.' ... <a href="'.$permalink.'" class="read-more">continue reading <i class="foundicon-right-arrow"></i></a>';
   return $excerpt;
}

The fifth line I attempted to convert from this:

$content = str_replace(']]>', ']]>', $content);

TIA!

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

Don't use a custom function. You should use the hooks. You don't have to strip shortcodes, wordpress does that for you automatically, just use something like this

// setting higher priority so that wordpress default filter have already applied
add_filter('the_excerpt', 'custom_excerpt_filter', 11);
function custom_excerpt_filter($excerpt) {
    // apply your logic of read more link here
    return $excerpt . 'Custom Read More Text';
}

add_filter('excerpt_length', 'custom_excerpt_length');
function custom_excerpt_length($length) {
    return 30; // replace this with the character count you want
}

RULE OF THUMB

Never Ever create a custom function for something there is a hook or core function available

share|improve this answer
    
Thanks, this worked a treat! –  Justice Is Cheap Oct 19 '12 at 19:07
add comment

Use strip_shortcodes( $excerpt ) to … well … strip shortcodes. :) Do that early, before you call strip_tags().

<?php
/** Plugin Name: (#69848) Strip shortcodes from the excerpt */
function wpse69848_noshortcode_excerpt( $excerpt )
{
    return strip_shortcodes( $excerpt );
}
add_filter( 'the_excerpt', 'wpse69848_noshortcode_excerpt' );
share|improve this answer
1  
Have a look at How to Answer as this was flagged as low quality. –  Brian Fegter Oct 19 '12 at 19:05
    
Totally kidding...just testing out the shortcuts. :) –  Brian Fegter Oct 19 '12 at 19:05
    
Tried this but it didn't work. Thanks though. –  Justice Is Cheap Oct 19 '12 at 19:08
    
@BrianFegter I was expecting that. But there was nothing to add. :) –  toscho Oct 19 '12 at 19:08
    
Just busting your chops. :) –  Brian Fegter Oct 19 '12 at 19:31
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.