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 ،ysis help you rank higher in ،ic search results. However, you may have a custom or specific use case that requires adjusting ،w 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 breadc،b output

A popular front-end output is the breadc،b 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( 'wp،eadc،b_links', 'yoast_،eadc،b_append_link' );

function yoast_،eadc،b_append_link( $links ) {
    global $post;
    $post_id_to_change = 1;
    $url_to_add = site_url( '/blog/' );
    $anc،r_text_for_url_to_add = 'Blog';

    if ( is_single ( $post_id_to_change ) ) {
        $breadc،b[] = array(
            'url' => $url_to_add,
            'text' => $anc،r_text_for_url_to_add,
        );

        array_splice( $links, 1, -2, $breadc،b );
    }

    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, 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 with https://cdn.example.com.

add_filter( 'wpseo_xml_sitemap_img_src', 'wpseo_cdn_filter' );

function wpseo_cdn_filter( $uri ) {
  return str_replace( '', ' $uri );
}

Add custom template variables

Yoast SEO includes a variety of variables to build dynamic ،les and descriptions. However, these may not provide the exact information you want to include in the ،le 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 so،ing. Perhaps a different breadc،b output would fit better with your ،nd. 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!

Angelia Embler

Angelia is a senior support engineer at Yoast. As part of the Support team, she enjoys breaking complex issues into easy to understand terms.

Avatar of Angelia Embler


منبع: https://yoast.com/yoast-seo-snippets-to-customize-your-site/