Last updated on September 21, 2023

ACF and Yoast SEO conflict

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Resolving ACF and Yoast SEO Plugin Conflict.

The ACF (Advanced Custom Fields) and Yoast SEO plugins are both popular tools used in WordPress development. However, there can be conflicts between these two plugins when it comes to the meta tags and content analysis functionality provided by Yoast SEO. This conflict can result in unexpected behavior or missing meta tags on your website.

To resolve the ACF and Yoast SEO conflict, you can use the wpseo_robots filter provided by Yoast SEO. This filter allows you to modify the robots meta tag output generated by Yoast SEO. By default, Yoast SEO sets the value of the robots meta tag based on its own analysis of the page content. However, you can override this value using the wpseo_robots filter.

Here’s an example code snippet that demonstrates how to use the wpseo_robots filter to resolve the ACF and Yoast SEO conflict:

function wpsnippets_disable_yoast_seo_robots( $robots ) {
    // Check if ACF fields are present on the current page
    if ( function_exists( 'get_field' ) && get_field( 'your_acf_field_name' ) ) {
        // Disable Yoast SEO robots meta tag
        $robots['index'] = false;
        $robots['follow'] = false;
    }
    return $robots;
}
add_filter( 'wpseo_robots', 'wpsnippets_disable_yoast_seo_robots' );

In this code snippet, we define a custom function wpsnippets_disable_yoast_seo_robots that takes the $robots array as a parameter. We then check if the ACF field you specify (replace 'your_acf_field_name' with the actual field name) is present on the current page using the get_field function. If the ACF field is present, we modify the $robots array to disable indexing and following by setting the values of 'index' and 'follow' to false. Finally, we return the modified $robots array.

By using this code snippet, you can prevent Yoast SEO from adding the robots meta tag when certain ACF fields are present on a page. This can be useful in scenarios where you want to have more control over the indexing and following behavior of specific pages on your website.

Examples

Example 1: Fixing ACF and Yoast SEO conflict by disabling Yoast SEO on specific ACF fields

This example demonstrates how to resolve a conflict between Advanced Custom Fields (ACF) and Yoast SEO by disabling Yoast SEO on specific ACF fields.

function wpsnippets_disable_yoast_seo_on_acf_fields() {
    add_filter( 'wpseo_pre_analysis_post_content', 'wpsnippets_disable_yoast_seo_on_specific_acf_fields' );
}

function wpsnippets_disable_yoast_seo_on_specific_acf_fields( $content ) {
    global $post;

    // Replace 'field_name' with the actual name of the ACF field
    if ( get_field( 'field_name', $post->ID ) ) {
        return '';
    }

    return $content;
}
add_action( 'init', 'wpsnippets_disable_yoast_seo_on_acf_fields' );

The code above adds a filter to disable Yoast SEO analysis on specific ACF fields. It checks if the ACF field with the name ‘field_name’ has a value for the current post. If it does, the filter returns an empty string, effectively disabling Yoast SEO analysis for that specific field.

Example 2: Fixing ACF and Yoast SEO conflict by excluding ACF fields from Yoast SEO sitemap

This example demonstrates how to exclude specific ACF fields from the Yoast SEO sitemap to resolve a conflict between ACF and Yoast SEO.

function wpsnippets_exclude_acf_fields_from_yoast_sitemap( $excluded ) {
    // Replace 'field_name' with the actual name of the ACF field
    $excluded[] = 'field_name';

    return $excluded;
}
add_filter( 'wpseo_sitemap_exclude_post_type', 'wpsnippets_exclude_acf_fields_from_yoast_sitemap' );

The code above adds a filter to exclude specific ACF fields from the Yoast SEO sitemap. It adds the ACF field with the name ‘field_name’ to the array of excluded post types, preventing it from being included in the Yoast SEO sitemap.

Example 3: Fixing ACF and Yoast SEO conflict by disabling Yoast SEO on specific ACF field groups

This example demonstrates how to disable Yoast SEO analysis on specific ACF field groups to resolve a conflict between ACF and Yoast SEO.

function wpsnippets_disable_yoast_seo_on_acf_field_groups() {
    add_filter( 'wpseo_pre_analysis_post_content', 'wpsnippets_disable_yoast_seo_on_specific_acf_field_groups' );
}

function wpsnippets_disable_yoast_seo_on_specific_acf_field_groups( $content ) {
    global $post;

    // Replace 'field_group_name' with the actual name of the ACF field group
    if ( function_exists( 'get_field_objects' ) && get_field_objects( $post->ID, 'field_group_name' ) ) {
        return '';
    }

    return $content;
}
add_action( 'init', 'wpsnippets_disable_yoast_seo_on_acf_field_groups' );

The code above adds a filter to disable Yoast SEO analysis on specific ACF field groups. It checks if the ACF field group with the name ‘fieldgroupname’ exists for the current post. If it does, the filter returns an empty string, effectively disabling Yoast SEO analysis for that specific field group.

Last updated on September 21, 2023. Originally posted on September 23, 2023.

Leave a Reply

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