Support

Account

Home Forums Add-ons Flexible Content Field Yoast SEO & ACF

Solving

Yoast SEO & ACF

    • Ash

    • April 23, 2014 at 2:21 am

    Hi,

    Has anyone managed to tie the Yoast SEO plugin in with ACF?

    I’ve looked around & I see there is something implemented within the ySEO plugin, but I’m not sure how to get it to work, maybe this is due to my groups using the flexible field and I never know what a page/post may contain (field wise)

    I found this which point’s in the right direction (I think), but as I say, I don’t know what ‘name’ will be or in what order they may come.

    add_filter( 'wpseo_pre_analysis_post_content', 'filter_yoasts_wpse_119879', 10, 2 );
    
    function filter_yoasts_wpse_119879( $content, $post )
    {
        $fields = get_field( 'name', $post->ID );
        return $content . $fields;
    }
  • You can use that filter in your functions, it will work if you change ‘name’ in what ever you’ve called your custom field. If you change it, yoast will read that field.

    But here’s the problem, it will only read 1 custom field on 1 page. If you need to scan multiple acf, yoast only sees the first one defined in functions. We tried everything, adding multiple filters to our functions for each custom field separately, but it will ignore the other filters. Now the creators of acf say this is something yoast needs to fix (not sure if this attitude helps with promoting acf), so we wont find the answer here.

    • Andrius

    • November 25, 2014 at 11:23 pm

    I have tested %%cf_<acf custom field name>%% worked fine

    • WooHoo

    • July 17, 2015 at 9:24 pm

    I just combined two or more fields into 1, and that worked!
    So you have to write something like this:

    
    add_filter( 'wpseo_pre_analysis_post_content', 'filter_yoasts_wpse_119879', 10, 2 );
    
    function filter_yoasts_wpse_119879( $content, $post )
    {
        $field1 = get_field( 'text', $post->ID );
        $field2 = get_field( 'more_text', $post->ID );
        $fields = $field1 . ' ' . $field2;
        return $content . $fields;
    }
    
    • Ash

    • July 18, 2015 at 12:13 am

    Funny,

    I was looking at this just yesterday again. I think I have found someone that has done this now ( see
    https://imperativeideas.com/making-custom-fields-work-yoast-wordpress-seo/ ) I’ve tried it, and seems to be almost working. I say almost as the check still shows the focus keyword(s) not to be in the content on the traffic light system, even though it does find them in there to pass it.

    Try it, it will make more sense than I have just made here.

    • WooHoo

    • July 18, 2015 at 12:54 am

    I’ll check that one out.
    It looks as if it should work (it looks a little like my solution, but more complex). But my guess is that this one won’t work on a ‘Flexible Layout’, because you have to target the sub fields (rows > layouts > sub_fields) to get the correct content.
    Say you have a Flexible layout by the name of ‘project_content’, and a row where the actual content goes into is called ‘content’ and the actual content has the field name ‘text’, you have to target that one as:

    
    if( have_rows('project_content') ):
        while ( have_rows('project_content') ) : the_row();
            if( get_row_layout() == 'content' ):
    	    $custom_content = get_sub_field( 'text', $post->ID );
    	endif;
        endwhile;
    endif;
    

    (or something like that…)

    • Ash

    • July 18, 2015 at 12:57 am

    Very good point. I will take a look at yours and this one and see. I would be grateful to any solution you come up with as this one has been on my list of things to try and fix for some time now, and I still can’t get to the bottom of it.

    • WooHoo

    • July 18, 2015 at 1:44 am

    The exact code I’ve put into my ‘functions.php’ looks like this:

    
    add_filter( 'wpseo_pre_analysis_post_content', 'filter_yoasts_wpse_119879', 10, 2 );
    
    function filter_yoasts_wpse_119879( $content, $post )
    {
    	if( have_rows('project_page') ):
    	     // loop through the rows of data
    	    while ( have_rows('project_page') ) : the_row();
    	        if( get_row_layout() == 'intro' ):
    	        	$field1 = get_sub_field( 'text' );
    			endif;
    	        if( get_row_layout() == 'in-depth' ):
    	        	$field2 = get_sub_field( 'in_depth' );
    			endif;
    	    endwhile;
    	endif;
        $fields = $field1 . ' ' . $field2;
        return $content . $fields;
    }
    

    As you can see, my Flexible Layout is called ‘project_page’.
    The first ‘while’ statement checks if there are any rows, and if so, get the one with the names ‘intro’ and ‘in-depth’, and put the content (‘text’ and ‘in_depth’) of the corresponding sub fields in a variable labeled $field1 and $field2.
    The last thing I do is combine these two variables into one ($fields) and return that.
    I’m not a PHP guru, absolutely not. But looking at different pieces of code I came up with this one. I do not know if it is very ‘clean’ or efficient PHP-code, but it works (for me)
    Actually, this function is based on the one you posted earlier.

    • Paul

    • August 20, 2015 at 6:02 pm

    Hey,

    I’ve been able to get flexible layouts, repeater fields, nearly everything working using the above approaches, thanks!

    The final thing I can’t get Yoast to see is images. Do I need to write something other than:

    $field1 = get_field( 'image', $post->ID );

    To get Yoast to see images?

    Any advice welcome.

    Thanks in advance.

    Paul

    • WooHoo

    • August 20, 2015 at 10:33 pm

    Paul,
    Good question, but I don’t know (yet).
    But what puzzles me is how Yoast ‘looks’ at images in the first place.
    I know that Yoast checks the featured image of a post or page to see if the so-called ‘alt-text’ has the Focus Keyword in it you’ve set in Yoast’s interface.
    But Yoast doesn’t check this with attached images to a post or page, as far as I know.
    This means, that if you add images to a post or page with an ACF (repeater) field, these images get attached, but Yoast won’t check them.

    I think you should check Yoast’s documentation on this subject.
    And if you have found out a way on how to let Yoast look at attached images, I really like to know how you did it.

    • WooHoo

    • August 20, 2015 at 10:52 pm

    Paul,
    I just came across this article on Yoast’s website:
    http://kb.yoast.com/article/131-images-in-the-xml-sitemap

    Maybe it wil make you understand a bit more on how WordPress handles attached images and how Yoast deals with them.

    • nicmare

    • November 17, 2015 at 4:04 am

    hey paul. i took a look into the code of yoast. seems it expects < img > strings in $content. so all you need to do is something like this:

    add_filter( 'wpseo_pre_analysis_post_content', 'filter_yoasts_wpse_119879', 10, 2 );
    function filter_yoasts_wpse_119879( $content, $post ){
    	$myimage = get_field("myimage",$post->ID);
    	$content .= "<img src='".$myimage["sizes"]["medium"]."' alt='".$myimage["alt"]."'/>";
    	return $content;
    }

    and it would recognize your custom field images.

    • Legin76

    • December 7, 2015 at 10:06 pm

    Has anyone tested this recently as it appears that “wpseo_pre_analysis_post_content” has now been deprecated?

    https://yoast.com/dev-blog/yoast-seo-breaking-api-changes/

    • nicmare

    • December 7, 2015 at 10:13 pm

    i missed that statement from yoast. but i click thru their discussion and ended with this javascript documentation: https://github.com/Yoast/YoastSEO.js/blob/master/README.md

    but at the moment i have no clue how to use this code with our old hook

    • dominic

    • December 15, 2015 at 9:12 am

    I’m just writing something to get notified if someone comes up with a fix…

    • Apssinc

    • December 15, 2015 at 8:25 pm

    We use this little plugin for some time now, It still works fine. even after the Yoast update

    https://nl.wordpress.org/plugins/wp-seo-acf-content-analysis/

    Maybe it can help solve the problem

    • nicmare

    • December 15, 2015 at 8:33 pm

    as far as i can see this plugin won’t work in the future too

    • WooHoo

    • December 15, 2015 at 9:16 pm

    Maybe Elliot (Mr. ACF) can have a look at this, because this is very important to a lot of ACF Pro users out there.
    For now, I’ll stay away from the 3.x Yoast plugin until someone comes with a plugin or JavaScript to replace the “wpseo_pre_analysis_post_content” filter.

    • Ash

    • December 15, 2015 at 9:24 pm

    I agree, this is very important to a lot of people out there.

    I would say that I wouldn’t think about building a WordPress site without ACF & Yoast SEO, I would think others will say the same?

    • Paul

    • December 15, 2015 at 9:29 pm

    I agree too, Yoast is a must for me and it’s massive down to be having issues with these two plugins together.

    That being said, check out this post: http://support.advancedcustomfields.com/forums/topic/acf-yoast-seo-3-0/

    That’s a pretty neat solution for Yoast 3

    • WooHoo

    • December 15, 2015 at 9:34 pm

    I just sent an email to Elliot about this issue. Don’t know if he will reply, but it is worth trying it.

    • WooHoo

    • December 17, 2015 at 7:12 pm

    Elliot responded on this post:
    http://support.advancedcustomfields.com/forums/topic/acf-yoast-seo-3-0/

  • I made a plugin a while ago that add ACF field data to Yoast SEO v3.0, might be of use to someone.
    https://wordpress.org/plugins/acf-content-analysis-for-yoast-seo/

    • aboasy

    • July 19, 2017 at 5:05 pm

    i’m using the below code to add a custom meta
    where organizer_gender is radio button,
    organizer_certification is text,
    city is multiple checkbox.

    i have only one problem, the city is always coming first not in sequence.

    function custom_content_filter_the_content( $content ) {     
        if ( is_singular( 'tribe_organizer' ) && function_exists('get_field') ) {   
         $content .= get_the_title();
         $content .=  ' is trainer for ' . get_field('organizer_gender') . ' gender ' ;
         $content .= ' certified: ' . get_field('organizer_certification');
         $content .= ' located in ' . the_field('city') . '';
         $content .=  ' Find contact details.' ;
        }
         return $content;
    }
    
    function hook_meta() {
    ?>
    <meta name="description" content="<?php echo strip_tags(custom_content_filter_the_content()); ?>" />
    <meta property="og:description" content="<?php echo strip_tags(custom_content_filter_the_content()); ?>" />
    <meta name="twitter:description" content="<?php echo strip_tags(custom_content_filter_the_content()); ?>" />
    <?php
    }
    add_action('wp_head', 'hook_meta');
Viewing 24 posts - 1 through 24 (of 24 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.