WordPress.org

Ready to get started?Download WordPress

Forums

Magazine Basic
meta description garbled for non-ASCII languages (2 posts)

  1. akky
    Member
    Posted 1 year ago #

    Hi,

    I was tracking down that many of meta descriptions in Japanese were garbled, came with bad binaries which only happens when this magazine-basic theme is activated.

    By seeing functions.php/pbt_metaDesc(), I noticed that the code to cut the contents by 155 letters caused the problem. It is actually cutting the contents by 155 "bytes", so any multibytes contents (even other latin languages other than English) may have the similar problems, though the chances are slimmer than languages like Japanese/Chinese/etc.

    There are also smaller problems, 1. the control-letters removal is not working, 2. the generated description is not trimmed, which might affect bad for SEO even with English. So I included those fixes as well.

    I attach my patch here. Hope this helps and would be included in future update.

    Best,
    Akky

    --- functions.php.orig	Mon Nov 26 17:06:53 2012
    +++ functions.php	Mon Nov 26 18:54:12 2012
    @@ -958,18 +958,18 @@
     		$content = apply_filters( 'the_excerpt_rss', strip_tags( get_the_content() ) );
    
     	$content = preg_replace( '/\[.+\]/', '', $content );
    -	$chars = array( '', '\n', '\r', 'chr(13)', '\t', '', '\x0B');
    -	$content = str_replace( $chars, ' ', $content );
    +	$chars = array( '', "\n", "\r", chr(13), "\t", "", "\x0B");
    +	$content = trim(str_replace( $chars, ' ', $content ));
    
     	if ( empty( $content ) )
     		$content = pbt_theme_option( 'site_description' );
    
     	$content = str_replace( '"', '', $content );
    
    -	if ( strlen( $content ) < 155 ) {
    +	if ( mb_strlen( $content ) < 155 ) {
     		echo $content;
     	} else {
    -		$desc = substr( $content, 0, 155 );
    +		$desc = mb_substr( $content, 0, 155 );
     		echo $desc . '...';
     	}
     }
  2. akky
    Member
    Posted 1 year ago #

    The first code did not have a workaround for the environment where mbstring extension is not activated.

    *** functions.php.orig	Tue Nov 27 15:51:04 2012
    --- functions.php	Tue Nov 27 15:56:43 2012
    ***************
    *** 958,976 ****
      		$content = apply_filters( 'the_excerpt_rss', strip_tags( get_the_content() ) );
    
      	$content = preg_replace( '/\[.+\]/', '', $content );
    ! 	$chars = array( '', '\n', '\r', 'chr(13)', '\t', '', '\x0B');
    ! 	$content = str_replace( $chars, ' ', $content );
    
      	if ( empty( $content ) )
      		$content = pbt_theme_option( 'site_description' );
    
      	$content = str_replace( '"', '', $content );
    
    ! 	if ( strlen( $content ) < 155 ) {
    ! 		echo $content;
      	} else {
    ! 		$desc = substr( $content, 0, 155 );
    ! 		echo $desc . '...';
      	}
      }
    
    --- 958,987 ----
      		$content = apply_filters( 'the_excerpt_rss', strip_tags( get_the_content() ) );
    
      	$content = preg_replace( '/\[.+\]/', '', $content );
    ! 	$chars = array( '', "\n", "\r", chr(13), "\t", "", "\x0B");
    ! 	$content = trim(str_replace( $chars, ' ', $content ));
    
      	if ( empty( $content ) )
      		$content = pbt_theme_option( 'site_description' );
    
      	$content = str_replace( '"', '', $content );
    
    ! 	if (!extension_loaded('mbstring')) {
    ! 		// this will make some non-ASCII texts corrupted so non-English users
    ! 		// are recommended to install PHP mbstring extention
    ! 		if ( strlen( $content ) < 155 ) {
    ! 			echo $content;
    ! 		} else {
    ! 			$desc = substr( $content, 0, 155 );
    ! 			echo $desc . '...';
    ! 		}
      	} else {
    ! 		if ( mb_strlen( $content ) < 155 ) {
    ! 			echo $content;
    ! 		} else {
    ! 			$desc = mb_substr( $content, 0, 155 );
    ! 			echo $desc . '…';
    ! 		}
      	}
      }

Topic Closed

This topic has been closed to new replies.

About this Theme

About this Topic