I'm trying to get a default custom excerpt for posts.

I've got a shortcode that executes if it is manually entered into the WordPress post editor excerpt meta box. To do this functions.php has

add_filter( 'get_the_excerpt', 'shortcode_unautop');
add_filter( 'get_the_excerpt', 'do_shortcode');
add_filter( 'get_the_excerpt', 'do_shortcode', 5 );

(I know I shouldn't have several filters, but don't know which one works)

If I add the following to functions.php the shortcode itself gets displayed on the live site, but not executed. And manually entered custom excerpts are overwritten by the function

add_filter( 'get_the_excerpt', function( $post_excerpt, $post ) {
    if ( $post->post_type != 'post' ) return $post_excerpt;

    return '[myshortcode]';
}, 99, 2 );

Please advise:

  1. Which filter is appropriate to execute shortcodes in custom excerpts (general usage)?
  2. What would be a good add_filter to have a default custom excerpt, with a shortcode, that executes?
  3. I would need that default custom excerpt to be overwritten by manual custom excerpt added to metabox. Note it seems that we'd want the shortcode added to the metabox so that it could be executed from there rather than returned on the frontend.
  4. Would it be possible to control this by category for example for category1 use this shortcode, for category2 use that shortcode, and maybe all categories by default, no default custom excerpt declared.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.