Yoast SEO snippets to customize your site • Yoast


Yoast SEO automatically handles all sorts of things for your site. Features like meta tags, schema, sitemaps, and content analysis help you rank higher in organic search results. However, you may have a custom or specific use case that requires adjusting how our plugin works for your site. Yoast SEO includes filters that allow you or your developer to customize many of the features in our plugin. Let’s dive into some of the popular Yoast SEO snippets and real-world examples of times you may need to use these snippets.

Modify breadcrumb output

A popular front-end output is the breadcrumb trail that themes can use at the top of content items, like posts and pages. Yoast SEO includes common path options, but customization can benefit some content types. The code snippet below adds a custom link between the Home and Post Name that appears by default. Thus, Home » Hello world! becomes Home » Blog » Hello world!. You can find more examples here.

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;
}

Update image URLs for CDN

Some sites use a CDN to help images load faster by serving them from many servers worldwide. A CDN provides you with a different domain for images.

To ensure social media platforms use the CDN permalink for social media images, you need to add this small Yoast SEO code snippet after replacing current_example.com and new_example.com with your WordPress and CDN domains, respectively. Thus, https://current_example.com/wp-content/uploads/2024/02/image.png becomes https://new_example.com/wp-content/uploads/2024/02/image.png. You can find more examples here.

add_filter( 'wpseo_opengraph_image', 'change_opengraph_image_url' );

function change_opengraph_image_url( $url ) {
    return str_replace('current_example.com', 'new_example.com', $url);
}

To update the sitemap image URLs, the following code will replace https://www.example.com with https://cdn.example.com.

add_filter( 'wpseo_xml_sitemap_img_src', 'wpseo_cdn_filter' );

function wpseo_cdn_filter( $uri ) {
  return str_replace( 'https://www.example.com', 'https://cdn.example.com', $uri );
}

Add custom template variables

Yoast SEO includes a variety of variables to build dynamic titles and descriptions. However, these may not provide the exact information you want to include in the title or description. The following snippet creates a custom variable %%myname%% that outputs a sentence My name is Moses in the front-end output.

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

Yoast SEO sitemaps include up to 1000 entries the individual sitemaps. The limit is acceptable for most server configurations, but your server may have more or less resources available. The filter helps you be in control of your server resources.

add_filter( 'wpseo_sitemap_entries_per_page', 'max_entries_per_sitemap' );

function max_entries_per_sitemap() {
    return 100;
}

Conclusion

While Yoast SEO does a lot of the heavy lifting, sometimes you may need to tweak something. Perhaps a different breadcrumb output would fit better with your brand. Luckily, Yoast SEO offers a range of filters so that you can fine-tune our plugin. In this post, we’ve looked at a few, but there are many more filters in our developer portal that you can try! Good luck!



Source link