Skip to content

Instantly share code, notes, and snippets.

@j-cam
Created May 29, 2015 04:40
Show Gist options
  • Save j-cam/fa1a46bd0bec898d382f to your computer and use it in GitHub Desktop.
Save j-cam/fa1a46bd0bec898d382f to your computer and use it in GitHub Desktop.
Add Advanced Custom Fields to Yoast SEO keyword density filter.
<?php
/**
* Add ACF Fields to Keyword evaluation for Yoast SEO Plugin
* @link https://imperativeideas.com/making-custom-fields-work-yoast-wordpress-seo/
*/
if ( is_admin() && defined('WPSEO_VERSION') ) { // check to make sure we aren't on the front end
add_filter('wpseo_pre_analysis_post_content', 'add_acf_to_yoast');
function add_acf_to_yoast( $content ) {
global $post;
$pid = $post->ID;
$custom_content = '';
$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_acf_to_yoast'); // don't let WP execute this twice
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment