Get Yoast SEO to check ACF fields

In this tutorial, we'll learn how to get Yoast SEO to check ACF fields in WordPress

Get Yoast SEO to check ACF fields

Two plugins that I find myself using time and time again are Yoast WordPress SEO and Advanced Custom Fields. They both provide extra functionality that I feel WordPress should have out of the box.

The only issue I find is that Yoast SEO doesn't take content in ACF fields into account when it comes to checking a post for its SEO quality. It's very easy to get Yoast SEO to check ACF fields though.

Checking ACF fields with Yoast

There's a handy filter that can add ACF field content to the Yoast SEO check. In this example, I'm going check an acf field named 'example_acf':

add_filter( 'wpseo_pre_analysis_post_content', function () use ($content, $post) {
    $checkField = get_field('example_acf', $post->ID);
    return $content . $checkField;
}, 10, 2);

If you're using a version of PHP earlier than 5.3.0, anonymous functions won't work for you, so this solution should work instead:

add_filter( 'wpseo_pre_analysis_post_content', 'wpseo_example_acf', 10, 2);
function wpseo_example_acf($content, $post) {
    $checkField = get_field('example_acf', $post->ID); return $content . $checkField;
}

Add a filter for each ACF field you have, and put the code somewhere sensible (like your functions.php file).

Not heard of ACF? Where have you been?

Just in case you've come across this post and not used ACF before, I'd certainly recommend it. Advanced Custom Fields (ACF) is a great plugin that allows you to define a tonne of extra fields for your posts. These include related articles, Google maps, extra content blocks, repeating fields to name a few. You should check it out.

Any questions or comments, fire away below.