Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How to customize Yoast SEO?
    WordPress SEO


How to customize Yoast SEO?


To customize Yoast SEO, you can use various filters and snippets to modify different aspects of the plugin. Here are some examples:

Modify Breadcrumb Output

python
add_filter( 'wpseo_breadcrumb_links', 'yoast_seo_breadcrumb_append_link' );

function yoast_seo_breadcrumb_append_link( $links ) {
    global $post;
    $post_id_to_change = 1;
    $url_to_add = site_url( '/blog/' );
    $anchor_text_for_url_to_add = 'Blog';
    if ( is_single( $post_id_to_change ) ) {
        $breadcrumb[] = array(
            'url' => $url_to_add,
            'text' => $anchor_text_for_url_to_add,
        );
        array_splice( $links, 1, -2, $breadcrumb );
    }
    return $links;
}

Add Custom Template Variables

python
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' );
}

Alter the Number of Sitemap Entries

python
add_filter( 'wpseo_sitemap_entries_per_page', 'max_entries_per_sitemap' );

function max_entries_per_sitemap() {
    return 100;
}

Update Image URLs for SEO

python
add_filter( 'wpseo_image_urls', 'update_image_urls' );

function update_image_urls( $urls ) {
    // Update image URLs here
    return $urls;
}

Add Custom Data to Page Analysis

python
class MyCustomDataPlugin {
    constructor() {
        if ( typeof YoastSEO !== "undefined" && typeof YoastSEO.app !== "undefined" ) {
            new this();
        } else {
            jQuery( window ).on(
                "YoastSEO:ready",
                function() {
                    new this();
                }
            );
        }
    }

    registerModifications() {
        const callback = this.addContent.bind( this );
        YoastSEO.app.registerModification( "content", callback, "MyCustomDataPlugin", 10 );
    }

    addContent( data ) {
        data += "Hello, I'm some additional data!";
        return data;
    }
}

Registering the Plugin with WordPress

python
wp_enqueue_script( 'my-custom-data-plugin', plugins_url( 'js/MyCustomDataPlugin.js', __FILE__ ), array( 'yoast-seo' ) );

Changing SEO Title Templates

python
add_filter( 'wpseo_title_template', 'custom_title_template' );

function custom_title_template( $template ) {
    // Customize the title template here
    return $template;
}

Customizing the Sitemap Index

python
add_filter( 'wpseo_sitemap_index', 'custom_sitemap_index' );

function custom_sitemap_index( $sitemap_index ) {
    // Customize the sitemap index here
    return $sitemap_index;
}

These snippets can be used to customize various aspects of Yoast SEO, such as modifying breadcrumb output, adding custom template variables, altering the number of sitemap entries, updating image URLs, adding custom data to page analysis, changing SEO title templates, and customizing the sitemap index.

Citations:
[1] https://yoast.com/yoast-seo-snippets-to-customize-your-site/
[2] https://developer.yoast.com/customization/yoast-seo/adding-custom-data-analysis/
[3] https://www.youtube.com/watch?v=nKtt9qnJV7I
[4] https://yoast.com/help/how-to-modify-default-snippet-templates-in-yoast-seo/
[5] https://yoast.com/help/how-to-customize-the-sitemap-index/

To add custom data to the Yoast SEO page analysis, you can follow these steps:

1. Create a Custom JavaScript Plugin:
- Create a new JavaScript file, e.g., `MyCustomDataPlugin.js`, in your plugin's `js/` directory.
- In this file, define a class that will handle the custom data integration with Yoast SEO.

javascript
   /* global YoastSEO */
   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) {
       // Add your custom data to the content
       data += "Hello, I'm some additional data!";
       return data;
     }
   }
   

2. Register the Plugin with WordPress:
- Enqueue the custom plugin script in your WordPress plugin:

php
   wp_enqueue_script('my-custom-data-plugin', plugins_url('js/MyCustomDataPlugin.js', __FILE__), array('yoast-seo'));
   

This ensures that the custom plugin is loaded after Yoast SEO's scripts.

3. Customize the Content Analysis:
- In the `addContent()` method of your custom plugin, you can modify the content that Yoast SEO will analyze.
- For example, you can add custom data from custom fields, shortcodes, or other sources to the content.
- The modified content will then be included in Yoast SEO's analysis, and the results will reflect the additional data.

By following these steps, you can successfully integrate your custom data with the Yoast SEO page analysis. This allows you to extend the analysis to include information that is specific to your plugin or website.

Remember to test your implementation thoroughly to ensure that the custom data is being properly added and analyzed by Yoast SEO.

Citations:
[1] https://developer.yoast.com/customization/yoast-seo/adding-custom-data-analysis/
[2] https://yoast.com/help/customize-your-yoast-seo-experience/
[3] https://developer.yoast.com/customization/yoast-seo/adding-custom-assessments/
[4] https://docs.metabox.io/tutorials/analyze-content-yoast-seo/
[5] https://developer.yoast.com/features/analysis/overview/