For quite a while now, at least a couple years, an eternity in software dev, Yoast has had this small snippet of code they call meta variables.  Now don’t get me wrong here, if you’re not using Yoast, you’re not getting the results you need from your WordPress website.  For most of you, I would highly recommend buying into their premium subscriptions, or at the very least, copying a few tips I have here for better search rankings.

Today I’m going to talk about the Yoast meta variable%%excerpt%%inside the “Search Appearance” menu of Yoast 7.3. You place this in your Yoast settings to display the meta description for your pages and posts in search engine results.

The trouble is this, Yoast uses the native WordPress function strip_shortcodes() inside a function that is stripping shortcodes on your WordPress post content. As the function name suggests, this strips any shortcodes from your post content while it auto-generates your excerpt. This function grabs the first 320 characters of your post content to create your excerpt, unless you have WordPress excerpts explicitly defined, then it grabs your excerpt.

Here’s where the trouble begins. Divi adds snippets of shortcode to the beginning and end of all your post content. It’s not just Divi, it’s most modern WordPress themes today. Which means that any modern theme today could be susceptible to this coding error by Yoast. Now if you’re running WordPress 2017 or any of the free themes that come with WordPress, this will not be an issue for you because they don’t use any shortcodes in the post content. Why you would be running any theme other than Divi is beyond me?!? You’re wasting your time if you’re using any other theme.

Let’s dig into the Yoast code!

Starting at line 207 in /wp-content/plugins/wordpress-seo/inc/class-wpseo-utils.php

[php]/**
* First strip out registered and enclosing shortcodes using native WordPress strip_shortcodes function.
* Then strip out the shortcodes with a filthy regex, because people don’t properly register their shortcodes.
*
* @static
*
* @since 1.8.0
*
* @param string $text Input string that might contain shortcodes.
*
* @return string $text String without shortcodes.
*/
public static function strip_shortcode( $text ) {
return preg_replace( ‘`\[[^\]]+\]`s’, ”, strip_shortcodes( $text ) );
}[/php]

Looking at their own notes, they seem to understand what they’re doing, however, why are they trying to strip shortcodes from a string that already has it’s shortcodes stripped? In addition, they didn’t guard against any empty results, so this function will always return a null result. If you return a preg_replace that is going to be empty or null, then you’ll get a null result every time. To make matters worse, it won’t throw an error, because preg_replace is doing it’s job and returning the correct result as requested.

So for every modern WordPress theme running today that places shortcodes in your post content, you’re meta descriptions will never show if you’re using the%%excerpt%%meta variable inside SEO -> Search Appearance -> Content Types (tab) -> Show Posts -> Meta Description Template dialog window.

For anyone that’s having trouble with their SEO, simply remove the native WordPress strip_shortcodes function, inside the Yoast function, leaving only the $text string variable. Like this:

[php]public static function strip_shortcode( $text ) {
return preg_replace( ‘`\[[^\]]+\]`s’, ”, $text );
}[/php]

The preg_replace regex that Yoast is using is functionally correct, and it will strip your shortcodes just as written. For this purpose, grabbing the first 320 characters of your content, it’s actually a better solution than the native WordPress strip_shortcodes function which uses preg_match_all.

I can confirm this same problem is also inside the Yoast SEO Premium.

Now as a member of the code militia, it is your duty to share this info with the coding community.