Useful Snippets

Welcome!


This blog is used to collect useful snippets related to Linux, PHP, MySQL and more. Feel free to post comments with improvements or questions!

RSS Latest posts from my personal blog


Most viewed posts


Subscribe to RSS feed


Override default Yoast SEO settings in WordPress

Stanislav KhromovStanislav Khromov

Sometimes the default Yoast SEO settings aren’t quite the way you want. For example, I wanted to enable twitter cards using the “Summary with large image” card type by default on a multisite network. Below you’ll find a snippet that does that, and can also be adapted to change any default option while still letting users override them if desired.

add_filter('wpseo_defaults', function($defaults, $option_name) {
  if($option_name === 'wpseo_social') {
    $defaults['twitter'] = true;
    $defaults['twitter_card_type'] = 'summary_large_image';
  }
  return $defaults;
}, 10, 2);

Tip

Looking for the proper $option_name ? If you enable the WP_DEBUG constant in your wp-config.php, it’ll show up on the relevant tab in the Yoast SEO admin:

seo-screengrab

This snippet was tested with Yoast SEO 2.3.5, but is likely to work for both older and newer versions.

PHP

Web Developer at Aftonbladet (Schibsted Media Group)
Any opinions on this blog are my own and do not reflect the views of my employer.
LinkedIn
Twitter
WordPress.org Profile
Visit my other blog

Comments 2