WordPress.org

Ready to get started?Download WordPress

Forums

All in One SEO Pack
[resolved] Access ACF fields for title and description META (6 posts)

  1. sandiboudreau
    Member
    Posted 2 months ago #

    I'm moving my sites away from Yoast, and need to access ACF fields on my pages for the title and description areas on pages.

    For example, I have a site for a car dealer, and with YOAST I was pulling in make, model, miles stuff automatically for title and description META.

    Is there a way to do this with All In One SEO?

    https://wordpress.org/plugins/all-in-one-seo-pack/

  2. theatereleven
    Member
    Posted 2 months ago #

    I got an answer on this from Peter Baylies on the AIOSEO support team.

    It's easy, simply add this to your functions.php:

    function sfwd_acf_field_replace( $matches ) {
            $result = '';
            if ( !empty( $matches ) ) {
                    if ( !empty( $matches[1] ) )
                            $result = get_field( $matches[1] );
                    else
                            $result = $matches[0];
            }
            return $result;
    }
    
    function sfwd_acf_fields( $format ) {
            if ( function_exists( 'get_field' ) ) {
                    return preg_replace_callback( '/%acf_([^%]*?)%/', 'sfwd_acf_field_replace', $format );
            } else {
                    return $format;
            }
    }
    
    add_filter( 'aioseop_title', 'sfwd_acf_fields' );
    add_filter( 'aioseop_description', 'sfwd_acf_fields' );

    Then, in the AIOSEO plugin, you can use %acf_fieldname% to pull stuff in. Works the same way as Yoast did.

  3. sandiboudreau
    Member
    Posted 2 months ago #

    Worked beautifully - THANK YOU!!!

  4. wpsmort
    Member
    Posted 2 months ago #

    We'll include this functionality in the next version of the plugin.

  5. theatereleven
    Member
    Posted 2 months ago #

    Hey, here's an updated code set until the next version. This fixed an issue with the social meta.

    *Also note, now use the format %%cf_fieldname%%

    function sfwd_acf_field_replace( $matches ) {
            $result = '';
            if ( !empty( $matches ) ) {
                    if ( !empty( $matches[1] ) ) {
    					if ( function_exists( 'get_field' ) ) $result = get_field( $matches[1] );
    					if ( empty( $result ) ) {
    						global $post;
    						if ( !empty( $post ) ) $result = get_post_meta( $post->ID, $matches[1], true );
    					}
    					if ( empty( $result ) ) $result = $matches[0];
    				}
                    else $result = $matches[0];
            }
            return $result;
    }
    
    function sfwd_acf_fields( $format ) {
    	return preg_replace_callback( '/%%cf_([^%]*?)%%/', 'sfwd_acf_field_replace', $format );
    }
    
    add_filter( 'aioseop_title', 'sfwd_acf_fields' );
    add_filter( 'aioseop_description', 'sfwd_acf_fields' );
    add_filter( 'aiosp_opengraph_meta', 'sfwd_acf_fields' );
  6. theatereleven
    Member
    Posted 2 months ago #

    Final addition to this, if you want to pull custom ACF fields to build a default META description for say a custom post type, place this code in your functions.php:

    add_filter( 'aioseop_description', 'sfwd_default_description', 1 );
    function sfwd_default_description( $desc ) {
        if ( is_singular( 'product' ) ) {
            if ( empty( $desc ) ) $desc = "%cf_description%";
        }
        return $desc;
    }

    Where 'product' is the name of your CPT and 'cf_description' is the actual META tag content that can be a combination of test and %cf_fiednames%.

Reply

You must log in to post.

About this Plugin

About this Topic