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!

Are your smart devices spying on you? Make better purchasing choices and find products that respect your privacy at Unwanted.cloud

RSS Latest posts from my personal blog


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

Full-stack impostor syndrome sufferer & Software Engineer at Schibsted Media Group

Comments 2