Working with WordPress SEO by Yoast, the Root Relative URLs plugin and Facebook

Having problems getting Facebook Like buttons to work on your website? Are you also using something like the Root Relative URLs plugin to make all your URLs relative to the root of the site instead of to the domain?

The Root Relative URLs plugin is a wonderful add-on to WordPress which not only not only help reduce the size of your web pages, but more importantly to developers, it make your website or blog portable by making all new links created on your website relative to the root of your website instead of the domain.

While this works fine in 99% of all situations, it fails to produce the desired results in places where your website generates links, such as the OpenGraph og:url meta property that Facebook uses. This problem first came to my attention while troubleshooting an issue with a Facebook Like counters using the Facebook Developer's Debugger.

WorPress SEO generates OpenGraph information that Facebook uses, for example, whenever you post link to your website on Facebook or when people click the Facebook Like button on your website.

WITHOUT the Root Relative URLs plugin installed, WordPress SEO generates the following OpenGraph line of code:

<meta property='og:url' content='http://www.yoursite.com/'/>

WITH the Root Relative URLs plugin installed, WordPress SEO generates the following OpenGraph line of code:

<meta property='og:url' content='/'/>

While I realize that Root Relative URLs plugin is at the root of the problem, it would have been great if WordPress SEO took the initiative to ensure all URL's are complete in instances where links are used by other site, like Facebook, to link back to your website.

How to Fix the Problem (short-term solution)

In the case of og:url, it is possible to fix the OpenGraph og:url meta property by editing the file named wp-content/plugins/wordpress-seo/frontend/class-opengraph.php and replace this line…

echo "<meta property='og:url' content='" . esc_attr( $this->canonical( false ) ) . "'/>\n";

… with the following two lines …

$SiteRoot = (substr(esc_attr( $this->canonical( false ) ),0,1) == '/' ? (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443 ? "https" : "http") : '') . "://" . $_SERVER['HTTP_HOST'];
echo "<meta property='og:url' content='". $SiteRoot . esc_attr( $this->canonical( false ) ) . "'/>\n";

The Longer-Term Solution

In the longer term, I am hoping that wonderful developers of WordPress SEO will fix the problem. You can track the status of this issue and add your comments by visiting the WordPress SEO Support Forum.

In the meantime, remember to always re-apply this fix after updating WordPress SEO until the issue has been resolved.


Add a comment:

Your email address will not be published. Required fields are marked *