Hack Fix for Yoast WordPress SEO Plugin

Hack Fix for Yoast WordPress SEO Plugin

One of the key services that we provide when developing WordPress web sites for our clients is Search Engine Optimization, SEO. SEO is important because developing a great web site isn’t going to be of very much use to a client if nobody can find it. One of the best plugins that we use to perform SEO on the sites we develop is the highly acclaimed WordPress SEO Plugin by Yoast. This plugin helps you select focus keywords for each page or post on a site and then it helps you to refine your content to maximize the search-ability of those keywords on that page or post. Unfortunately there are a few bugs in the plugin that cause the search scoring to often fail. This can make it frustrating to work with so I did a little investigation to come up with this quick hack fix for Yoast WordPress SEO plugin. I figured that if this saved me some frustration that I’d post it to our blog so that it may also help some others avoid some frustration until Yoast can update the plugin to fix the problems permanently.

Symptoms of the Yoast WordPress SEO Plugin Bugs

The symptoms of the problem are that even though you know and verify that your search keyword phrase is included in your content, the Yoast WordPress SEO Plugin sometimes fails to find the phrase in the content. This often happens with longer phrases. Searching online revealed others having the same problem and posting about the problem. Unfortunately the reply was often to say that the person reporting the problem shouldn’t be using long phrases in the first place, rather than actually reproducing and fixing the bugs that cause the plugin to fail to find the keyword phrases. So I set about tracking down the cause of the problem and created this quick hack fix for Yoast WordPress SEO plugin myself.

Here are two examples that exhibit the symptoms of both bugs.

1) WordPress SEO by Yoast’s General Tab

Hack Fix for Yoast WordPress SEO Plugin - Content: No

Sometimes on WordPress SEO by Yoast’s General tab, the flag for finding the focus keywords in the content will say “no”, even though you verify that the content does indeed contain the keyword phrase. In the above screenshot you can clearly see that the keyword phrase “interior character sets” is indeed in the content because it is listed in the snippet, which is pulled right from the content (and is all the content on this test page). Yet Yoast WordPress SEO states whether it is found in the content as “Content: No” which is clearly an error.

2) WordPress SEO by Yoast’s Page Analysis Tab

Hack Fix for Yoast WordPress SEO Plugin - Density 0

Sometimes WordPress SEO by Yoast’s Page Analysis tab, the analysis report states that the keyword phrase is found 0 times in the content, when it actually is in the content. Sometimes it will even say that it is in the first paragraph, but not in the content, which obviously can’t both be true because the first paragraph is part of the content.

Creating a new page with the same keywords “interior character sets” and the following paragraph content (because you need over 100 words for a density analysis):

This series of 3D architectural interior character sets is a DreamLight classic from 1997. The Yoast WordPress SEO Plugin needs at least 100 words in the content in order to perform a page analysis that includes the keyword density test. The Yoast WordPress SEO Plugin needs at least 100 words in the content in order to perform a page analysis that includes the keyword density test. The Yoast WordPress SEO Plugin needs at least 100 words in the content in order to perform a page analysis that includes the keyword density test. The Yoast WordPress SEO Plugin needs at least 100 words in the content in order to perform a page analysis that includes the keyword density test.

Results in a page analysis (screenshot above) that states:

The keyword density is 0%, which is a bit low, the keyword was found 0 times.

and

The keyword appears in the first paragraph of the copy.

Obviously another error because both statements can not be true at the same time.

Tracking Down the Bugs in Yoast WordPress SEO Plugin

1) WordPress SEO by Yoast’s General Tab – JavaScript Bug

The WordPress SEO by Yoast’s General Tab updates dynamically as you type and edit the fields in the general tab’s meta box, so the dynamic processing is handled in JavaScript. Investigating the plugin files reveals something interesting. If we look in the file wordpress-seo/js/wp-seo-metabox.js, in the yst_testFocusKw() function, we find the following regular expression which is used to match the keywords in the content.

RegExp("(^|[ \s\n\r\t\.,'\(\"\+;!?:\-])" + focuskw + "($|[ \s\n\r\t.,'\)\"\+!?:;\-])", 'gim');

That JavaScript file is the human readable version of the file. The plugin actually uses the condensed version of the file named wordpress-seo/js/wp-seo-metabox.min.js instead. Locating the same regular expression in the condensed file we find something unusual. The regular expression is malformed. Some of the backslashes are missing.

RegExp("(^|[ s\n\r\t.,'(\"+;!?:-])"+g+"($|[ s\n\r\t.,')\"+!?:;-])","gim");

It turns out that creating a regular expression with the RegExp function in JavaScript using strings requires that the backslashes be escaped themselves (because they are escape characters in JavaScript strings), so they must be doubled up. Otherwise the regular expression becomes corrupted and the matches will not be performed as they were intended. This is what causes the content searches to sometimes fail on the general tab.

2) WordPress SEO by Yoast’s Page Analysis Tab – PHP Bug

The WordPress SEO by Yoast’s Page Analysis Tab only updates when the page is updated or published. This is because it is processed in PHP rather than JavaScript. Investigating the PHP files reveals something interesting as well. If we look in the wordpress-seo/admin/class-metabox.php file in the score_body function, we find the following code with a regular expression which is used to match the keywords in the content.

// Keyword Density check
$keywordDensity = 0;
if ( $wordCount > 100 ) {
$keywordCount = preg_match_all( '`\b' . preg_quote( $job['keyword'], '`' ) . '\b`miu', utf8_encode( $body ), $res );

If we look further down in the function we find the following code with a similar regular expression which is used to match the keywords in the first paragraph.

// First Paragraph Test
// check without /u modifier as well as /u might break with non UTF-8 chars.
if ( preg_match( '`\b' . preg_quote( $job['keyword'], '`' ) . '\b`miu', $firstp ) || preg_match( '`\b' . preg_quote( $job['keyword'], '`' ) . '\b`mi', $firstp ) || preg_match( '`\b' . preg_quote( $job['keyword_folded'], '`' ) . '\b`miu', $firstp )

Notice that in this first paragraph test, which works, the comment specifically mentions that it tests with and without the /u modifier because sometimes that breaks. However the keyword density check only tests with that /u modifier and runs the $body through utf8_encode() function. That’s what’s causing the keyword matching to sometimes fail.

The Quick Hack Fix for Yoast WordPress SEO Plugin

I’ve had this problem throughout a number of versions of the plugin and performed this temporary hack/fix on version 1.6.3. It worked fine for me here in my English version but of course your mileage may vary. Neither DreamLight nor I make any warranty or representations about this hack/fix and only offer it as an educational example. Please proceed with caution and at your own risk. This may not work on other languages, especially double-byte languages. It’s easy enough to perform this quick hack fix for Yoast WordPress SEO Plugin if you are comfortable editing a couple of files after backing them up. Please be sure to back up your site first and specifically make backup copies of both files that will be edited. This is a temporary hack/fix and it will be reversed anytime you update the plugin and will need to be reapplied until the bugs in the plugin itself are fixed. Hopefully it will be permanently fixed in a future version of the plugin so that this hack is no longer necessary. If you wish to apply this hack/fix here are the steps that I used.

1) Perform Backups!

  • Back up your entire WordPress site including all content and databases.
  • Back up the entire wp-content/plugins/wordpress-seo folder by making a zip archive of it.
  • Back up the file wp-content/plugins/wordpress-seo/admin/class-metabox.php by making a zip archive of it.
  • Back up the file wp-content/plugins/wordpress-seo/js/wp-seo-metabox.min.js by making a zip archive of it.

2) Update the JavaScript File.

  • In the WordPress backend go to the Plugins Editor and select plugin to edit: WordPress SEO
  • Select the wordpress-seo/js/wp-seo-metabox.min.js file loading it into the editor
  • Scroll down to locate the following text:

RegExp("(^|[ s\n\r .,'(\"+;!?:-])"+a+"($|[ s\n\r .,')\"+!?:;-])","gim")

  • Very carefully edit it as follows (be careful to replace the actual tab after each \r with \\t and to use triple backslashes before the indicated double quotes as \\\”)

RegExp("(^|[ \\s\\n\\r\\t\\.,'\\(\\\"\\+;!?:\\-])"+a+"($|[ \\s\\n\\r\\t.,'\\)\\\"\\+!?:;\\-])","gim")

  • Click the Update File button.

3) Update the PHP File.

  • Select the wordpress-seo/admin/class-metabox.php file loading it into the editor
  • Search for keywordDensity and scroll down to the following line:

$keywordCount = preg_match_all( '`\b' . preg_quote( $job['keyword'], '`' ) . '\b`miu', utf8_encode( $body ), $res );

  • Locate the part that says: miu and delete the u so that it now says mi as follows:

$keywordCount = preg_match_all( '`\b' . preg_quote( $job['keyword'], '`' ) . '\b`mi', utf8_encode( $body ), $res );

  • Click the Update File button.

4) Verify that everything is OK.

  • Load a page of your site in the WordPress backend and reload it to be sure it reloads and runs the updated JavaScript and PHP code.
  • Scroll down to the WordPress SEO metabox and verify that it’s working OK now.
  • If anything is not working properly then use the backup zip files you made to revert the two edited files back to the way they were, or reinstall the plugin to revert back to the original version.
  • If it is working enjoy your keyword phrases being found in your content!

Custom WordPress Web Site Development

Contact Us Today for custom WordPress Web Site Development if you have an aging static HTML Web site and would like to upgrade to a new custom designed WordPress based site with a built in content management system, blog, SEO and more!

Leave a Reply