Yoast SEO snippets to customize your site

1 month ago 18
ARTICLE AD BOX
  1. SEO blog
  2. Development
  3. Yoast SEO snippets

Yoast SEO automatically handles all sorts of things for your site. Features similar meta tags, schema, sitemaps, and contented investigation assistance you fertile higher successful integrated hunt results. However, you whitethorn person a customized oregon circumstantial usage lawsuit that requires adjusting however our plugin works for your site. Yoast SEO includes filters that let you oregon your developer to customize galore of the features successful our plugin. Let’s dive into immoderate of the fashionable Yoast SEO snippets and real-world examples of times you whitethorn request to usage these snippets.

Modify breadcrumb output

A fashionable front-end output is the breadcrumb way that themes tin usage astatine the apical of contented items, similar posts and pages. Yoast SEO includes communal way options, but customization tin payment immoderate contented types. The codification snippet beneath adds a customized nexus betwixt the Home and Post Name that appears by default. Thus, Home » Hello world! becomes Home » Blog » Hello world!. You tin find more examples here.

add_filter( 'wpseo_breadcrumb_links', 'yoast_seo_breadcrumb_append_link' ); function yoast_seo_breadcrumb_append_link( $links ) { planetary $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 ); } instrumentality $links; }

Update representation URLs for CDN

Some sites usage a CDN to assistance images load faster by serving them from galore servers worldwide. A CDN provides you with a antithetic domain for images.

To guarantee societal media platforms usage the CDN permalink for societal media images, you request to adhd this tiny Yoast SEO codification snippet aft 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 tin find more examples here.

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

To update the sitemap representation URLs, the following code volition regenerate https://www.example.com with https://cdn.example.com.

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

Add customized template variables

Yoast SEO includes a assortment of variables to physique dynamic titles and descriptions. However, these whitethorn not supply the nonstop accusation you privation to see successful the rubric oregon description. The pursuing snippet creates a customized adaptable %%myname%% that outputs a condemnation My sanction is Moses successful the front-end output.

add_action('wpseo_register_extra_replacements', 'register_custom_yoast_variables'); function get_myname() { instrumentality 'My sanction is Moses'; } function register_custom_yoast_variables() { wpseo_register_var_replacement( '%%myname%%', 'get_myname', 'advanced', 'some assistance text' ); }

Alter the fig of sitemap entries

Yoast SEO sitemaps see up to 1000 entries the idiosyncratic sitemaps. The bounds is acceptable for astir server configurations, but your server whitethorn person much oregon little resources available. The filter helps you beryllium successful power of your server resources.

add_filter( 'wpseo_sitemap_entries_per_page', 'max_entries_per_sitemap' ); function max_entries_per_sitemap() { instrumentality 100; }

Conclusion

While Yoast SEO does a batch of the dense lifting, sometimes you whitethorn request to tweak something. Perhaps a antithetic breadcrumb output would acceptable amended with your brand. Luckily, Yoast SEO offers a scope of filters truthful that you tin fine-tune our plugin. In this post, we’ve looked astatine a few, but determination are galore much filters successful our developer portal that you tin try! Good luck!

Angelia Embler

Angelia is simply a elder enactment technologist astatine Yoast. As portion of the Support team, she enjoys breaking analyzable issues into casual to recognize terms.

Avatar of Angelia Embler