Force Slim SEO default open graph image

tdrayson

On a client project I’m working on, I wanted to set one open graph image for the entire site.

Slim SEO has a built in feature to set the default social sharing image. This is great, however this only gets applied if the page or post does not have a featured image.

In my situation, I am using the featured image for the page header background. This causes a random background image to be set for my open graph on every page.

Luckily, we can hook into Slim SEO and force it to override every page with the default image set in Slim SEO settings.

Snippet

Add the code below to functions.php or code snippet plugin. This snippet will set the image to the default Facebook image set in Slim SEO.

PHP

        function tct_default_og($image){
	$option = get_option( 'slim_seo' );
	return $option['default_facebook_image'] 
	    ? $option['default_facebook_image']
	    : $value;
}

function tct_twitter_og($image){
    return tct_default_og($image);
}
add_filter('slim_seo_twitter_card_image', 'tct_twitter_og', 10);

function tct_facebook_og($image, $tag){
    return tct_default_og($image);
}
add_filter('slim_seo_open_graph_image', 'tct_facebook_og', 10, 2);
      
Copy

👋🏻 Weekly Tutorial Digest

I send out a weekly newsletter with links to new tutorials written in the last week, you can subscribe below.

Newsletter

🔒I won't send you spam, I promise