Support » Plugin: Yoast SEO » [Plugin: WordPress SEO by Yoast] WordPress SEO: meta-description generation bug with non-English pos

  • Hello

    I’m not sure if that was already reported, but there is a bug in meta-description generation when using non-English characters. For the majority of the posts you will get an empty meta-description.

    The problem seems to be in the post content truncation function. To be precise in wpseo-functions.php -> wpseo_replace_vars().
    The code seems to be unaware of the fact that the content may be UTF-8 encoded and all its truncation logic may fail (not always, but very often). To fix the problem I borrowed a piece of code from another theme, but I think ideally a proper length calculation should be done regardless of the encoding (I’m not a PHP expert but we never have this problem in Unicode supporting languages!!)

    Fixed by adding the following function:

    function wpseo_truncate_post($truncate, $amount) {
    		$truncate = preg_replace('@\[caption[^\]]*?\].*?\[\/caption]@si', '', $truncate);
    		if ( strlen($truncate) <= $amount ) $echo_out = ''; else $echo_out = '...';
    		$truncate = preg_replace('@<script[^>]*?>.*?</script>@si', '', $truncate); // <?
    		$truncate = preg_replace('@<style[^>]*?>.*?</style>@si', '', $truncate); // <?
    		$truncate = strip_tags($truncate);
    		if ($echo_out == '...') $truncate = substr($truncate, 0, strrpos(substr($truncate, 0, $amount), ' '));
    		else $truncate = substr($truncate, 0, $amount);
    		return ($truncate . $echo_out);

    and then replacing the following line
    '%%excerpt%%' => ( !empty($r->post_excerpt) ) ? strip_tags( $r->post_excerpt ) : substr( strip_shortcodes( strip_tags( $r->post_content ) ), 0, 155 ),
    '%%excerpt%%' => ( !empty($r->post_excerpt) ) ? strip_tags( $r->post_excerpt ) : wpseo_truncate_post($r->post_content, 250 ),

    The actual truncate number is still another story because again we would need a proper length calculation function!!

Viewing 2 replies - 1 through 2 (of 2 total)
  • webleo


    I can confirm this bug on a non-English site.
    Once you get non-English characters in the excerpt, the whole description content becomes empty (”).

    Even if there were other tags in the template (hello %%excerpt%% world), the whole thing gets removed.

    It’s only about characters, not site locale. If I set up an English-only page, within my non-English site – everything works fine.

    How can we bring this to the attention of the developer?

    Seems to be solved in the latest version. Awesome!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘[Plugin: WordPress SEO by Yoast] WordPress SEO: meta-description generation bug with non-English pos’ is closed to new replies.