To customize the code of Yoast SEO, you can use various filters and hooks provided by the plugin. Here are some examples of how to customize different aspects of Yoast SEO:
Customizing the Sitemap Index
1. Exclude Post Types from Sitemap:
php
add_filter( 'wpseo_sitemap_exclude_post_type', 'sitemap_exclude_post_type', 10, 2 );
function sitemap_exclude_post_type( $value, $post_type ) {
if ( $post_type == 'post_type_slug' ) {
return true;
}
}
2. Exclude Multiple Post Types from Sitemap:
php
add_filter( 'wpseo_sitemap_exclude_post_type', 'sitemap_exclude_post_type', 10, 2 );
function sitemap_exclude_post_type( $value, $post_type ) {
$post_type_to_exclude = array('post_type_slug1', 'post_type_slug2', 'post_type_slug3');
if ( in_array( $post_type, $post_type_to_exclude ) ) {
return true;
}
}
Customizing SEO Title Templates
1. Create Custom Title Template:
php
add_filter( 'wpseo_title_template', 'custom_title_template' );
function custom_title_template( $template ) {
// Your custom title template code here
return $template;
}
Adding Custom Data to Page Analysis
1. Add Custom Data to Analysis:
php
if ( typeof YoastSEO !== "undefined" && typeof YoastSEO.app !== "undefined" ) {
new MyCustomDataPlugin();
} else {
jQuery( window ).on(
'YoastSEO:ready',
function() {
new MyCustomDataPlugin();
}
);
}
2. Creating the Plugin:
php
class MyCustomDataPlugin {
constructor() {
// 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;
}
YoastSEO.app.registerPlugin( "MyCustomDataPlugin", { status: "ready" } );
this.registerModifications();
}
registerModifications() {
const callback = this.addContent.bind( this );
// Ensure that the additional data is being seen as a modification to the content.
YoastSEO.app.registerModification( "content", callback, "MyCustomDataPlugin", 10 );
}
addContent( data ) {
data += "Hello, I'm some additional data!";
return data;
}
}
Adding Custom Assessments
1. Registering the Plugin:
php
function loadMyCustomPlugin() {
new MyCustomPlugin();
}
if ( ! wp_installing() ) {
add_action( 'plugins_loaded', 'loadMyCustomPlugin', 20 );
}
2. Creating the Webworker:
php
class CustomWorker {
constructor() {
this._worker = analysisWorker;
}
register() {
this._worker.registerMessageHandler( "initialize", this.initialize.bind( this ), "MyCustomAssessmentPlugin" );
}
initialize( settings ) {
// Your custom assessment code here
}
}
Customizing Snippets
1. Add Custom Template Variables:
php
add_action('wpseo_register_extra_replacements', 'register_custom_yoast_variables');
function get_myname() {
return 'My name is Moses';
}
function register_custom_yoast_variables() {
wpseo_register_var_replacement( '%%myname%%', 'get_myname', 'advanced', 'some help text' );
}
2. Alter the Number of Sitemap Entries:
php
add_filter( 'wpseo_sitemap_entries_per_page', 'max_entries_per_sitemap' );
function max_entries_per_sitemap() {
return 100;
}
These are just a few examples of how to customize the code of Yoast SEO. For more detailed information and specific use cases, refer to the Yoast SEO documentation and the WordPress Codex.
Citations:[1] https://yoast.com/help/how-to-customize-the-sitemap-index/
[2] https://yoast.com/help/how-to-modify-default-snippet-templates-in-yoast-seo/
[3] https://developer.yoast.com/customization/yoast-seo/adding-custom-data-analysis/
[4] https://developer.yoast.com/customization/yoast-seo/adding-custom-assessments/
[5] https://yoast.com/yoast-seo-snippets-to-customize-your-site/