[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 ),
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!!
- The topic ‘[Plugin: WordPress SEO by Yoast] WordPress SEO: meta-description generation bug with non-English pos’ is closed to new replies.