WordPress.org

Ready to get started?Download WordPress

Forums

WordPress SEO by Yoast
WordPress SEO: meta-description generation bug with non-English pos (3 posts)

  1. hautecouturenews
    Member
    Posted 3 years ago #

    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 ),
    with
    '%%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!!

  2. webleo
    Member
    Posted 2 years ago #

    Hey,
    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?

  3. webleo
    Member
    Posted 2 years ago #

    Seems to be solved in the latest version. Awesome!

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic