Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Code snippet to change or remove OpenGraph output in Yoast SEO. There are multiple snippets in this code.
<?php
/********* DO NOT COPY THE PARTS ABOVE THIS LINE *********/
/* Change size for Yoast SEO OpenGraph image for all content
* Credit: Yoast Development team
* Last Tested: May 19 2020 using Yoast SEO 14.1 on WordPress 5.4.1
* Accepts WordPress reserved image size names: 'thumb', 'thumbnail', 'medium', 'large', 'post-thumbnail'
* Accepts custom image size names: https://developer.wordpress.org/reference/functions/add_image_size/
*/
add_filter( 'wpseo_opengraph_image_size', 'yoast_seo_opengraph_change_image_size' );
function yoast_seo_opengraph_change_image_size() {
return 'medium';
}
<?php
/********* DO NOT COPY THE PARTS ABOVE THIS LINE *********/
/* Change Open Graph image URLs in Yoast SEO
* Credit: Yoast Development team
* Last Tested: Jul 04 2018 using Yoast SEO 7.7.3 on WordPress 4.9.6
*/
add_filter( 'wpseo_opengraph_image', 'change_opengraph_image_url' );
function change_opengraph_image_url( $url ) {
return str_replace('current_domain.com', 'new_domain.com', $url);
}
<?php
/********* DO NOT COPY THE PARTS ABOVE THIS LINE *********/
/* Change Product Condition
* Credit: Yoast team
* Last Tested: Feb 20, 2020 using Yoast SEO WooCommerce 12.6 on WordPress 5.3.2
*********
* DIFFERENT POST TYPES
* Post: Change 123456 to the post ID
* Page: Change is_single to is_page and 123456 to the page ID
* Custom Post Type: Change is_single to is_singular and 123456 to the 'post_type_slug'
Example: is_singular( 'cpt_slug' )
*********
* MULTIPLE ITEMS
* Multiple of the same type can use an array.
Example: is_single( array( 123456, 234567, 345678 ) )
* Multiple of different types can repeat the if statement
*/
add_filter( 'Yoast\WP\Woocommerce\product_condition', 'yoast_seo_opengraph_change_product_condition' );
function yoast_seo_opengraph_change_product_condition( $condition ) {
if ( is_single ( 34 ) ) {
$condition = 'used';
}
/* Use a second if statement here when needed */
return $condition; /* Do not remove this line */
}
<?php
/********* DO NOT COPY THE PARTS ABOVE THIS LINE *********/
/* Enforce HTTP Open Graph URLs in Yoast SEO
* Credit: stodorovic https://github.com/stodorovic
* Last Tested: Feb 06 2017 using Yoast SEO 4.2.1 on WordPress 4.7.2
*/
add_filter( 'wpseo_opengraph_url', 'my_opengraph_url' );
function my_opengraph_url( $url ) {
return str_replace( 'https://', 'http://', $url );
}
<?php
/********* DO NOT COPY THE PARTS ABOVE THIS LINE *********/
/* Change Yoast SEO OpenGraph type
* Credit: Yoast Team
* Last Tested: Jul 11 2017 using Yoast SEO 5.0.1 on WordPress 4.8
*/
add_filter( 'wpseo_opengraph_type', 'yoast_change_opengraph_type', 10, 1 );
function yoast_change_opengraph_type( $type ) {
/* Make magic happen here
* Example below changes the homepage to a book type
*/
if ( is_home() ) {
return 'book';
} else {
return $type;
}
}
<?php
/********* DO NOT COPY THE PARTS ABOVE THIS LINE *********/
/* Remove all OpenGraph tags
* Credit: Alejandro https://github.com/jreviews
* Last Tested: Sep 2 2020 using Yoast SEO 14.9 on WordPress 5.5.1
*/
add_filter( 'wpseo_frontend_presenter_classes', 'wpseo_remove_opengraph' );
function wpseo_remove_opengraph( $classes ) {
$classes = array_filter($classes, function($class) {
return strpos($class, 'Open_Graph') === false;
});
return $classes;
}
<?php
/********* DO NOT COPY THE PARTS ABOVE THIS LINE *********/
/* Remove All Yoast SEO OpenGraph Output
* There is an on/off switch in the plugin Admin > SEO > Social > Facebook
* Credit: Unknown
* Last Tested: Apr 01 2017 using Yoast SEO 4.5 on WordPress 4.7.3
*/
add_action('wp_head', 'remove_all_wpseo_og', 1);
function remove_all_wpseo_og() {
remove_action( 'wpseo_head', array( $GLOBALS['wpseo_og'], 'opengraph' ), 30 );
}
<?php
/********* DO NOT COPY THE PARTS ABOVE THIS LINE *********/
/* Remove one of more Open Graph or Twitter tags
* Credit: Rogério https://github.com/rgllm
* Last Tested: Sep 2 2020 using Yoast SEO 14.9 on WordPress 5.5.1
*/
add_filter( 'wpseo_frontend_presenter_classes', 'filter_presenters' );
function filter_presenters( $filter ) {
if (($key = array_search('Yoast\WP\SEO\Presenters\Twitter\Image_Presenter', $filter)) !== false) {
unset($filter[$key]);
}
if (($key = array_search('Yoast\WP\SEO\Presenters\Open_Graph\Image_Presenter', $filter)) !== false) {
unset($filter[$key]);
}
if (($key = array_search('Yoast\WP\SEO\Presenters\Twitter\Description_Presenter', $filter)) !== false) {
unset($filter[$key]);
}
if (($key = array_search('Yoast\WP\SEO\Presenters\Open_Graph\Site_Name_Presenter', $filter)) !== false) {
unset($filter[$key]);
}
return $filter;
}
<?php
/********* DO NOT COPY THE PARTS ABOVE THIS LINE *********/
/* Remove Yoast SEO OpenGraph Output From One Post/Page
* Credit: Unknown
* Last Tested: Apr 01 2017 using Yoast SEO 4.5 on WordPress 4.7.3
*********
* DIFFERENT POST TYPES
* Post: Change 123456 to the post ID
* Page: Change is_single to is_page and 123456 to the page ID
* Custom Post Type: Change is_single to is_singular and 123456 to the 'post_type_slug'
Example: is_singular( 'cpt_slug' )
*********
* MULTIPLE ITEMS
* Multiple of the same type can use an array.
Example: is_single( array( 123456, 234567, 345678 ) )
* Multiple of different types can repeat the if statement
*/
add_action('wp_head', 'remove_one_wpseo_og', 1);
function remove_one_wpseo_og() {
if ( is_single ( 123456 ) ) {
remove_action( 'wpseo_head', array( $GLOBALS['wpseo_og'], 'opengraph' ), 30 );
}
/* Use a second if statement here when needed */
}
<?php
/********* DO NOT COPY THE PARTS ABOVE THIS LINE *********/
/* Remove Individual Yoast SEO OpenGraph meta tags
* Credit: Yoast Development team
* Last Tested: Jul 28 2017 using Yoast SEO 5.1 on WordPress 4.8
*/
add_filter( 'wpseo_opengraph_url' , '__return_false' );
add_filter( 'wpseo_opengraph_desc', '__return_false' );
add_filter( 'wpseo_opengraph_title', '__return_false' );
add_filter( 'wpseo_opengraph_type', '__return_false' );
add_filter( 'wpseo_opengraph_site_name', '__return_false' );
add_filter( 'wpseo_opengraph_image' , '__return_false' ); // v13.5 or older
add_filter( 'wpseo_og_og_image_width' , '__return_false' ); // v13.5 or older
add_filter( 'wpseo_og_og_image_height' , '__return_false' ); // v13.5 or older
add_filter( 'wpseo_opengraph_author_facebook' , '__return_false' );
add_filter( 'Yoast\WP\Woocommerce\product_condition', '__return_false' );
add_filter( 'Yoast\WP\Woocommerce\og_price', '__return_false' ); // True or False only
@DanielKoohmarey
Copy link

DanielKoohmarey commented Jan 4, 2018

Do these go at the end of functions.php? Perhaps you could update https://kb.yoast.com/kb/custom-open-graph-tags/ to include the answer as well, thanks!

@amboutwe
Copy link
Author

amboutwe commented Jan 4, 2018

The code snippets can go anywhere in your functions.php file after the opening <?php tag as long as they are not within another piece of code. The easiest option is to paste it at the end of the file.

If you're not familiar with using code snippets, these articles have wonderful advice:
http://www.wpbeginner.com/plugins/how-to-easily-add-custom-code-in-wordpress-without-breaking-your-site/
http://www.wpbeginner.com/beginners-guide/beginners-guide-to-pasting-snippets-from-the-web-into-wordpress/

@heroaex
Copy link

heroaex commented Feb 2, 2018

Is there a code snippet that I can use to tell Yoast to use a different image for the OG image tags? I'd like to show one image on the website, but when someone shares the blog post I'd like it to be a different image. Basically the default WordPress featured image would be for the image on the website, and a secondary featured image would be for social shares. I'd like Yoast to use that secondary image (which I'll add with Advanced Custom Fields).

@amboutwe
Copy link
Author

amboutwe commented Feb 2, 2018

@heroaex You or your developer can use the wpseo_opengraph_image filter to customize the OG image tags.

@heroaex
Copy link

heroaex commented Feb 2, 2018

Thanks, that worked! I also used the wpseo_twitter_image filter to replace that image as well.

@davemoz
Copy link

davemoz commented Mar 15, 2018

@amboutwe The last two filters mentioned above, wpseo_opengraph_image & wpseo_twitter_image, are exactly what I've been looking for. I'm trying to utilize them to get an image from a category-image plugin I have installed. I already use that plugin to assign and show different header images on each category archive page. I've added this to my functions.php file, but it seems only to remove the og_image & twitter_image tags altogether:

add_filter('wpseo_opengraph_image', 'custom_category_og_image', 1);
    function custom_category_og_image() {
    if ( is_post_type_archive ( 'product' ) ) {
	        $term = get_queried_object();
        $image_attributes = wp_get_attachment_image_src( $term->term_image, 'full' );

	return $image_attributes;
    }
}

The variable $image_attributes contains the URL of the image in question. Is that all I need to return for these filters? Thanks in advance for any help you can offer.

EDIT: For anyone who comes across this looking for the specifics of how to implement the wpseo_opengraph_image & wpseo_twitter_image filters, this site has instructions for exactly that: https://anythinggraphic.net/add-custom-social-share-images-descriptions-for-yoast-wordpress-seo/

EDIT Deux: This still wasn't working for my particular case—I needed custom OG & Twitter images for product category pages, not the Store page—so I finally discovered and swapped out is_post_type_archive( 'product' ) for is_tax( 'product_cat' ) and that worked perfectly. Hopefully that helps anyone who comes across this in the future.

@jnz31
Copy link

jnz31 commented Apr 25, 2018

@davemoz wp_get_attachment_image_src() returns an array (url, width, height, is_intermediate), or false, if no image is available. [docs]
and an array results in no image output, guess there is some check in yoast.
following function works for me:

function custom_category_og_image() {
    if ( is_post_type_archive ( 'product' ) ) {
	$term = get_queried_object();
        $image_attributes = wp_get_attachment_image_src( $term->term_image, 'full' );
        if ( $image_attributes !== false ) {
            return $image_attributes[0];
        }
    }
}
add_filter('wpseo_opengraph_image', 'custom_category_og_image', 10, 0);

@tcladin
Copy link

tcladin commented Apr 28, 2018

Hi please, can You help me with this ?
I would like add part of title, desc etc. to Yoast OG tags from other one plugin, from Asgaros forum plugin.
Because Yoast SEO have not specific category tags from forum. Yoast SEO have only main forum fags like:
Title = "Forum". But specific title I would like Title = "sub menu | Forum"
Please help me to make it with Snipest.

@Chillifish
Copy link

Chillifish commented May 3, 2018

I've been trying to get a custom twitter image and thankfully found this page. It would have saved me a lot of time if wpseo_twitter_image was in the API docs! https://yoast.com/wordpress/plugins/seo/api/

@unclebodey
Copy link

unclebodey commented May 21, 2018

Hi,

The url to my site recently changed from http to https. I added the code below to my function.php files to get my previous shares showing up again but now I have lost the new shares since we added the ssl. Is there code to be able to show both the http shares before the change and the https shares after the change?

add_filter( 'wpseo_opengraph_url', 'my_opengraph_url' );
function my_opengraph_url( $url ) {
return str_replace( 'https://', 'http://', $url );
}

@MarcoWPuser
Copy link

MarcoWPuser commented Jun 19, 2018

Hi there, thanks for your work!

...is possible that yoast_seo_opengraph_change_protocol.php doesn't works with WP 4.9.6?

Thanks for your answer

@jptomanelli
Copy link

jptomanelli commented Jul 12, 2018

@amboutwe

So I'm able to change a URL when there is an existing image uploaded (using wpseo_opengraph_image filter) but I would like to add a social image with a URL without having to upload via the meta-box. Is there any way to do this?

@velvetinkmedia
Copy link

velvetinkmedia commented Jul 23, 2018

Can someone please confirm that this works with WP 4.9.7 and Yoast SEO Premium 7.8.1?

I added the code to remove_all and added JSON-LD through GTM, but when running the test in Google Structured Data, it detects 2 "Organizations", one of which is produced by Yoast.

Thank you!

@changuelsami
Copy link

changuelsami commented Aug 6, 2018

Hi,
How can I change the og:image:alt value ? I've noticied that the code below remove all og:image:* tags : add_filter('wpseo_opengraph_image' , '__return_false' );
But I wanna keep these tags and change only og:image:alt one
Thanks :)

@MrMegamind
Copy link

MrMegamind commented Aug 9, 2018

Finally found something in this thread that works , thank you so much saved me tons of time

add_filter('wpseo_opengraph_url' , '__return_false' );
add_filter('wpseo_opengraph_desc', '__return_false' );
add_filter('wpseo_opengraph_title', '__return_false' );
add_filter('wpseo_opengraph_type', '__return_false' );
add_filter('wpseo_opengraph_site_name', '__return_false' );
add_filter('wpseo_opengraph_image' , '__return_false' );

@DrLightman
Copy link

DrLightman commented Sep 2, 2018

Any filter or option to disable the auto-discovery of images in the post content when the featured and user supplied images are missing? Because it's too much cpu and database intensive when it uses the WPSEO_Image_Utils::get_attachment_by_url and then the attachment_url_to_postid function since my wp_postmeta table is quite big.

@luandavidn
Copy link

luandavidn commented Jun 5, 2019

@DrLightman

Did you ever figure something out for this? I've been having similar troubles with trying to stop this behaviour too – though I'm trying to have every page across the site just default to the featured Facebook image set in Yoast's social setting.

@leefroese
Copy link

leefroese commented Jun 10, 2019

Also looking to disable image auto-discovery, but have been unsuccessful so far.

@DrLightman
Copy link

DrLightman commented Jun 10, 2019

@luandavidn

I just comment out some lines of code in the plugin files every time they release a new version in order to disable the autodiscovery. The pieces of code are, ref latest version 11.3:

at line 320 of \frontend\class-opengraph-image.php:

// $this->add_first_usable_content_image( $post_id );

from line 431 to 436 of the same \frontend\class-opengraph-image.php:

// $attachment_id = WPSEO_Image_Utils::get_attachment_by_url( $url );

// if ( $attachment_id > 0 ) {
	// $this->add_image_by_id( $attachment_id );
	// return $attachment_id;
// }

I have some filters setup elsewhere though to let the wp api return a predefined default featured image if the post hasn't one setup by the author.

ps: I'm still on plugin verison 9.7 but I checked and that code is mostly identical on latest 11.3

@kalevskis
Copy link

kalevskis commented Sep 25, 2019

Is there possibility to enable published and modified date og tags for pages and other post types?

@BaapJaan
Copy link

BaapJaan commented Apr 29, 2020

I am wondering after I post an article with a featured image then when I share the link to Facebook It should preview my post featured image.
is it possible to do something like this?

@amboutwe
Copy link
Author

amboutwe commented Apr 29, 2020

@BaapJaan This is not the proper place to request support. Please check out our extensive help section or visit the free support forum. If you require further support, upgrading to our premium version provides you with access to our support team.

@heyjoecampbell
Copy link

heyjoecampbell commented Nov 4, 2020

What code snippet can I use to disable the video Open Graph output.
Any assistance would be most appreciated.

Note: I tried contacting, to no avail.

@oelcommits
Copy link

oelcommits commented Jul 29, 2021

Hello, but how to add webp support for images?

@farinspace
Copy link

farinspace commented Aug 31, 2021

keep in mind that wpseo_opengraph_image WILL NOT be called if no appropriate images are found (e.g. a featured images is not present, or an in-page IMG tag is not present).

see wordpress-seo/src/presenters/open-graph/image-presenter.php for reference, the function signature is:

protected function filter( $image ) {
	/**
	 * Filter: 'wpseo_opengraph_image' - Allow changing the Open Graph image.
	 *
	 * @api string - The URL of the Open Graph image.
	 *
	 * @param Indexable_Presentation $presentation The presentation of an indexable.
	 */
	$image_url = \trim( \apply_filters( 'wpseo_opengraph_image', $image['url'], $this->presentation ) );
	if ( ! empty( $image_url ) && \is_string( $image_url ) ) {
		$image['url'] = $image_url;
	}

	return $image;
}

Something like add_filter('wpseo_opengraph_image' , '__return_false' ); WILL NOT work because, unless you are changing the url into a NON-EMPTY STRING value you are unable to effect the output.

@jazir5
Copy link

jazir5 commented Feb 15, 2022

How can I change the OG Title to pull from the yoast meta title field instead of the product title?

@imth3walrus
Copy link

imth3walrus commented May 23, 2022

Hey Angi @amboutwe , do you know any filter I could use to change the output of product:availability and og:availability ? I have a few pre order products and in-stock would be the wrong availability for me. I was able to succesfully change it on schema but I'm at a loss in metadata and open graph

@MLowerySTA
Copy link

MLowerySTA commented Jun 28, 2022

I'm trying to edit the og:url tag in the WordPress pagination for one of my sites, but I'm having trouble figuring it out. Can someone help me out?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment