Add Advanced Custom Fields to Yoast SEO Content Analysis
Install the free plugin ACF Content Analysis for Yoast SEO to add ACF custom fields to Yoast SEO content analysis. The behavior of the plugin can be customized through the following filters.
/* Addon: ACF Content Analysis for Yoast SEO
--------------------------------------------- */
add_filter( 'Yoast\WP\ACF\blacklist_name', 'bubdev_yoastseo_remove_field_from_scoring' );
/**
* Remove Specific Field From Scoring
*/
function bubdev_yoastseo_remove_field_from_scoring( $blacklist_name ) {
$blacklist_name->add( 'my-field-name' );
return $blacklist_name;
}
add_filter( 'Yoast\WP\ACF\blacklist_type', 'bubdev_yoastseo_remove_field_type_from_scoring' );
/**
* Remove Field Type From Scoring
*/
function bubdev_yoastseo_remove_field_type_from_scoring( $blacklist_type ) {
// Define 'text', 'image' etc.
$blacklist_type->add( 'accordion' );
$blacklist_type->add( 'taxonomy' );
$blacklist_type->add( 'flexible-content' );
return $blacklist_type;
}
add_filter( 'Yoast\WP\ACF\headlines', 'bubdev_yoastseo_define_field_heading_value' );
/**
* Define Field a Specific Header Heading Value
*/
function bubdev_yoastseo_define_field_heading_value( $headlines ) {
// Set value from 1-6 (1=h1, 6=h6).
$headlines['field_591eb45f2be86'] = 3;
return $headlines;
}
add_filter( 'Yoast\WP\ACF\refresh_rate', 'bubdev_yoastseo_change_refresh_rate' );
/**
* Change Refresh Rate
*/
function bubdev_yoastseo_change_refresh_rate() {
// Set refresh rate in milliseconds.
return 1000;
}
Check and Modify Analyzed Content
Modify the content that should be analyzed by Yoast SEO to your needs and output it to the console for evaluation. Based on a snippet from the plugin documentation.
/**
* Yoast SEO: Modify and Log Content
*
* Allows for modifications of the content that is being analyzed by the "Yoast
* SEO" plugin and outputs it to the console. Useful to check what's actually
* being analyzed.
*
* Based on: https://developer.yoast.com/customization/yoast-seo/adding-custom-data-analysis/
*/
(function() {
function modifyYoastSeoContent( consoleLog = false ) {
// Ensure YoastSEO.js is present and can access the necessary features.
if ( typeof YoastSEO === 'undefined' || typeof YoastSEO.analysis === 'undefined'
|| typeof YoastSEO.analysis.worker === 'undefined' ) return;
// Register plugin and ensure that the additional data is being seen as a
// modification to the content.
YoastSEO.app.registerPlugin( 'BubLogContent', { status: 'ready' } );
YoastSEO.app.registerModification( 'content', addContent, 'BubLogContent', 10 );
/**
* Adds to the content to be analyzed by the analyzer.
*
* @param {string} data The current data string.
* @return {string} The data string parameter with the added content.
*/
function addContent( data ) {
// Remove empty paragraphs and lines (added by unused fields).
data = data.replace( /<p><\/p>/g, '' );
data = data.replace( /^\s*\n/gm, '' );
// Log and return the data.
if ( consoleLog ) console.log( data );
return data ;
}
}
/**
* Add event listener to load the function.
*/
if ( typeof YoastSEO !== "undefined" && typeof YoastSEO.app !== "undefined" ) {
modifyYoastSeoContent();
}
else {
jQuery( window ).on( 'YoastSEO:ready', function() {
modifyYoastSeoContent();
});
}
})();