$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Shortcodes in excerpts returning empty string|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Shortcodes in excerpts returning empty string

matteradmin9PV0评论

I got this code:

function ntt_movie_shortcode($atts, $content = null)
{
    return 'TEST';
}
add_shortcode('ntt_movie', 'ntt_movie_shortcode');

The shortcode works perfectly on a single page, but on the Home Page / in excerpts the shortcodes are rendered as an empty string. So, the shortcode gets recognized but it seems, that the function doesn't get processed.

Any hints or approaches?

Thanks in advance.

I got this code:

function ntt_movie_shortcode($atts, $content = null)
{
    return 'TEST';
}
add_shortcode('ntt_movie', 'ntt_movie_shortcode');

The shortcode works perfectly on a single page, but on the Home Page / in excerpts the shortcodes are rendered as an empty string. So, the shortcode gets recognized but it seems, that the function doesn't get processed.

Any hints or approaches?

Thanks in advance.

Share Improve this question asked Jan 20, 2019 at 11:07 PhilPhil 113 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

If you’re using auto-generated excerpts, then all shortcodes will be removed (https://developer.wordpress/reference/functions/the_excerpt/):

An auto-generated excerpt will also have all shortcodes and tags removed. It is trimmed down to a word-boundary and the default length is 55 words. For languages in which words are (or can be) described with single characters (ie. East-Asian languages) the word-boundary is actually the character.

And it makes sense - you don’t want any advanced HTML in these excerpts and you have no control over what output will a shortcode generate (it may be something basic, but it may be a lot of complicated HTML code...)

In my case, I use (enclosing) shortcodes for formatting texts. So it's needed, that the shortcodes won't get removed because it removes words from sentences.

So I fiddled a bit and found this snippet, which helps to strip the shortcodes, but leaving the words in them: https://wordpress.stackexchange/a/229879/33303

It removes the default filter for the_excerpt and replaces it with a custom function. I presume, that this will lead me to additional problems ;) , but in this case it helps.

function ntt_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', 'ntt_custom_excerpt'  );

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far