• Hello,

    Can anyone explain to me how the wpseo_pre_analysis_post_content should be used?

    I’m having a hard time finding documentation and it isn’t quite clear what I should do.

    Yoast’s own blog posts says the following:

    This filter is called wpseo_pre_analysis_post_content and takes 1 argument: a string containing the post’s content.

    I want to use this filter to include my custom fields into the page analysis

    https://www.ads-software.com/plugins/wordpress-seo/

Viewing 15 replies - 1 through 15 (of 16 total)
  • Hey,

    I literally just had the same problem. We had a pretty complex setup but you basically do the following:

    function add_custom_content_to_analysis($content, $post)
    {
    	global $post;
    
    	if(is_admin()) {
    		if(has_sub_field('your_flexible_fields', $post->ID)) {
    			$sections = get_field('your_flexible_fields', $post->ID);
    			foreach($sections as $section) {
    				$content .= $section['your_content_field'];
    			}
    		}
    	} else {
    		return false;
    	}
    
    	return $content;
    }
    add_filter('wpseo_pre_analysis_post_content', 'add_custom_content_to_analysis');

    Hope this helps!

    Thread Starter lwish

    (@lwish)

    I’m going to check it out, thanks!

    Thread Starter lwish

    (@lwish)

    This doesn’t appear to work, what should I put in if(has_sub_field('your_flexible_fields', $post->ID))

    Hello,

    I am trying to find exactly the same thing. A few questions:

    Where do I enter the above code? Is it in the functions.php file for my theme?

    Is the ‘your_flexible_fields’ where you enter css class labels to add to content in your posts to then get added into the Yoast XML Sitemap?

    Will this work outside the standard ‘the_content’ filters?

    I ask as I am trying to find a way to force add images and videos to the XML sitemap that are outside the standard content of posts/pages. The easiest way I can think of doing this is by adding a class element to any image I want added to the sitemap that Yoast would then hook into to add to the page XML sitemap the image is on.

    @awb Collier: It is impolite to interrupt another poster’s ongoing thread with a question of your own. It causes significant problems for the forum’s volunteers and prevents us from being able to track issues by topic. Please post your own topic.

    Sorry, I thought this was a related issue question that was inline with exactly what the original poster was asking to do. No interruption was intended and I apologise for any rudeness, as it was unintended. I was just trying to get some help.

    I am having the same issue, and it’s not related to the way I retrieve ACF info. That’s not a problem.

    I can even explicitly append my keyword to the wpseo_pre_analysis_post_content output and they seem to be ignored.

    function seo_data($post_content){
      $post_content .= ' my exact focus keyword';
      return $post_content;
    }
    add_filter('wpseo_pre_analysis_post_content','seo_data');

    Any advice?

    Hello my site ( https://seoamk.com ) have another SEO tool. Can I delete this plugin and install SEO by Yoast without any trouble?

    Thread Starter lwish

    (@lwish)

    Hmmm, I still can’t seem to get this to work, I would really like it to work though..

    If you are building your own metaboxes with custom fields in, add this to your file, or if you just added the code to functions.php, add it there:

    function add_custom_content_to_analysis( $content ) {
    	global $post;
    	$custom = get_post_custom( $post->ID );
    	$custom_content = '';
    	foreach( $custom as $field )
    	{
    		$custom_content .= $field[0].' ';
    	}
    	return $content . ' ' . $custom_content;
    }
    add_filter( 'wpseo_pre_analysis_post_content', 'add_custom_content_to_analysis' );

    This should run through all custom fields in your post and add them to the content that SEO by Yoast looks at.

    Moosch’s code is good, but…

    I found that the yoast focus keyword was actually added as a custom field to the $custom array – meaning that the Page Analysis reports the focus keyword is found (even if it does not exists in the content or acf).

    So, be sure to remove the focus keyword [‘_yoast_wpseo_focuskw’] from the array first…

    function add_custom_content_to_analysis( $content ) {
    	global $post;
    	$custom = get_post_custom( $post->ID );
    	unset($custom['_yoast_wpseo_focuskw']); // ADD THIS
    	$custom_content = '';
    	foreach( $custom as $field )
    	{
    		$custom_content .= $field[0].' ';
    	}
    	return $content . ' ' . $custom_content;
    }
    add_filter( 'wpseo_pre_analysis_post_content', 'add_custom_content_to_analysis' );

    I’ve used at first for seo wpseo for https://gripijama.com but unfortunately I did not see a benefit

    @goxsel why would you see a benefit? This feeds the Yoast planning tool, not Googlebot. It’s pure back-end.

    I can’t get it to work either. Any other recommended solutions out there?

    I had to iterate through a bunch of *almost* working suggestions. This will grab the post content, check the custom fields, then drop any keys starting in _ or values ending in } (lets us avoid junk data in the calculation). The whole thing then gets appended to the original post content (passed by reference) and returned to the filter output.

    Finally, I end by removing the filter because in the current version of Yoast’s plugin, wpseo_pre_analysis_post_content runs twice and screws up your density numbers as a result.

    /**
     * Add custom fields to Yoast SEO analysis
     */
    
    add_filter('wpseo_pre_analysis_post_content', 'add_custom_to_yoast');
    
    function add_custom_to_yoast( $content ) {
    	global $post;
    	$pid = $post->ID;
    
    	$custom = get_post_custom($pid);
    	unset($custom['_yoast_wpseo_focuskw']); // Don't count the keyword in the Yoast field!
    
    	foreach( $custom as $key => $value ) {
    		if( substr( $key, 0, 1 ) != '_' && substr( $value[0], -1) != '}' && !is_array($value[0]) && !empty($value[0])) {
    		  $custom_content .= $value[0] . ' ';
    		}
    
    	}
    
    	$content = $content . ' ' . $custom_content;
    	return $content;
    
    	remove_filter('wpseo_pre_analysis_post_content', 'add_custom_to_yoast'); // don't let WP execute this twice
    }

    Remember: Yoast has not re-written the first tab in AJAX yet so it won’t reflect the changes. Everything else will though.

Viewing 15 replies - 1 through 15 (of 16 total)
  • The topic ‘How to wpseo_pre_analysis_post_content’ is closed to new replies.