Using ACF Field (post_lead) as SEO Description with Fallback to Excerpt

#1061162
  • Resolved Vimal Roy
    Rank Math free

    Hi Rank Math team,

    I’m currently using the Rank Math plugin on my WordPress site, and I’d like to customize how the SEO Description field is populated for blog posts.

    Specifically, I’d like to:

    Use a custom field (post_lead) created via ACF (Advanced Custom Fields) as the SEO Description.

    If post_lead is empty or not set, fallback to the post excerpt.

    I understand that the %customfield(post_lead)% variable can be used in the Meta Description field via the UI, but it doesn’t seem to support conditional logic or fallbacks.

    So my questions are:

    Is there any native support or planned support for conditional logic inside the Rank Math variable system (e.g., if custom field is empty, use excerpt)?

    Is using the rank_math/frontend/description filter the recommended approach for this kind of logic?

    Will applying a filter like this interfere with how Rank Math handles schema or content analysis?

    Here’s a simplified version of the code I’m considering:


    add_filter( 'rank_math/frontend/description', function( $description ) {
    if ( is_singular( 'post' ) ) {
    $post_lead = get_field( 'post_lead' );
    if ( $post_lead ) {
    return wp_strip_all_tags( $post_lead );
    } else {
    return wp_strip_all_tags( get_the_excerpt() );
    }
    }
    return $description;
    });

    Just want to confirm if this is the best practice for what I’m trying to do, or if there’s a better Rank Math-native solution that I’ve overlooked.

    Appreciate your help and awesome plugin. Looking forward to your guidance.

    Best regards,
    Vimal

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hello,

    We apologize for the issue you are facing with customizing the SEO Description field using ACF fields. Thank you for raising this concern.

    To address your questions:

    1. Native Support for Conditional Logic: Currently, Rank Math does not have built-in support for conditional logic within the variable system. However, using filters like the one you’ve provided is a common approach to achieve this functionality.

    2. Using rank_math/frontend/description Filter: Yes, using the rank_math/frontend/description filter is indeed the recommended approach for implementing conditional logic like this. Your code snippet looks good and should work as intended to check if the post_lead field is set and fallback to the post excerpt if it is not.

    3. Interference with Schema or Content Analysis: Applying this filter should not interfere with how Rank Math handles schema or content analysis. The filter you are using only modifies the description output and does not affect other functionalities of the plugin.

    Here’s a quick overview of your code:

    add_filter( 'rank_math/frontend/description', function( $description ) {
        if ( is_singular( 'post' ) ) {
            $post_lead = get_field( 'post_lead' );
            if ( $post_lead ) {
                return wp_strip_all_tags( $post_lead );
            } else {
                return wp_strip_all_tags( get_the_excerpt() );
            }
        }
        return $description;
    });

    This code effectively checks for the post_lead field and falls back to the excerpt if necessary, which is a solid solution for your requirement.

    If you need further assistance or have more questions, feel free to reply to this ticket, and a human agent from our team will be happy to help you further.

    Thank you for your support and for using Rank Math!

    Sources:
    How to Integrate Rank Math With Advanced Custom Fields » Rank Math

    Vimal Roy
    Rank Math free
    function truncate_description( $text, $limit = 160 ) {
        $text = wp_strip_all_tags( $text );
        return mb_substr( $text, 0, $limit );
    }
    
    add_filter( 'rank_math/frontend/description', function( $description ) {
        if ( is_singular( 'post' ) ) {
            $post_lead = get_field( 'post_lead' );
            if ( $post_lead && !empty( trim( $post_lead ) ) ) {
                return truncate_description( $post_lead );
            } else {
                $excerpt = get_the_excerpt();
                return truncate_description( $excerpt );
            }
        }
    
        return $description;
    });

    Will this automatically replace the single post description? Do I need to customize the Single Post Description field, which is currently set to %excerpt%?

    Hello,

    Yes, that code will completely replace the SEO Description meta. If you prefer, you can make a custom variable that can accept conditional logic. Here’s a filter code you can use:

    add_action('rank_math/vars/register_extra_replacements', function () {
    	rank_math_register_var_replacement(
    		'post_lead',
    		[
    			'name'        => esc_html__('Author URL', 'rank-math'),
    			'description' => esc_html__('Output the author URL.', 'rank-math'),
    			'variable'    => 'post_lead',
    			'example'     => post_lead_callback(),
    		],
    		'post_lead_callback'
    	);
    });
    
    function post_lead_callback() {
    	$desc = get_the_excerpt();
    	if (is_singular('post')) {
    		$post_lead = get_field('post_lead');
    		if ($post_lead && !empty(trim($post_lead))) {
    			$desc = truncate_description($post_lead);
    		}
    	}
    	return $desc;
    }
    
    function truncate_description( $text, $limit = 160 ) {
    	$text = wp_strip_all_tags( $text );
    	return mb_substr( $text, 0, $limit );
    }

    After applying the filter code, you can use this variable %post_lead%, which you can use anywhere in our SEO meta fields.

    Don’t hesitate to get in touch with us if you have any other questions.

    Hello,

    Since we did not hear back from you for 15 days, we are assuming that you found the solution. We are closing this support ticket.

    If you still need assistance or any other help, please feel free to open a new support ticket, and we will be more than happy to assist.

    Thank you.

Viewing 3 replies - 1 through 3 (of 3 total)

The ticket ‘Using ACF Field (post_lead) as SEO Description with Fallback to Excerpt’ is closed to new replies.