Skip to content

Instantly share code, notes, and snippets.

@amboutwe
Last active January 11, 2024 20:48
Show Gist options
  • Star 71 You must be signed in to star a gist
  • Fork 18 You must be signed in to fork a gist
  • Save amboutwe/811e92b11e5277977047d44ea81ee9d4 to your computer and use it in GitHub Desktop.
Save amboutwe/811e92b11e5277977047d44ea81ee9d4 to your computer and use it in GitHub Desktop.
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 ) {
// Search for 'current_domain.com'
// Replace with 'new_domain.com'
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 *********/
/* Use HTTP protocol for 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 ) {
// Search for 'https://'
// Replace with 'http://'
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
@MLowerySTA
Copy link

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?

@theo-gk
Copy link

theo-gk commented Jan 9, 2023

Hi, the priority for the og:image is currently this:

  1. A user-defined image “Facebook image” in the Social tab of the Yoast metabox on the post/page level.
  2. A user-defined “Featured image” for the page (only for posts).
  3. A prominent image from the page’s content.
  4. The “Social default image” from the template in Search Appearance.
  5. The site’s fallback/default social image (set at SEO > Social > Facebook > Default image).

If you want the default social image (4) to have higher priority than (3) A prominent image from the page’s content, then you could use this code:

add_filter( 'wpseo_opengraph_image', 'dc_yoast_prefer_default_image_over_first_content_image', 10, 2 );
function dc_yoast_prefer_default_image_over_first_content_image( $img_url, $presentation ) {

	if ( !empty( $presentation ) && isset( $presentation->model ) && isset( $presentation->model->open_graph_image_source ) ) {

		if ( 'first-content-image' === $presentation->model->open_graph_image_source ) {
			
			$yoast_options = get_option( 'wpseo_social' );

			if ( !empty( $yoast_options['og_default_image'] ) ) {
				return trim( $yoast_options['og_default_image'] );
			} 
		}
	}
	
	return $img_url;
}

@Glen-Dango
Copy link

hi this is great work. if i wanted to simply edit the og: image to a secure_url.. ( facebook wont display an image from my url in the preview post. where would i find the original function/ filter?

been looking for WPSEO-OPENGRAPH_URL for hours in the directory. thx

@amboutwe
Copy link
Author

@Glen-Dango As of Yoast SEO version 14.0 (released in Apr 2020), the plugin no longer outputs the og:image:secure_url.

If you mean that the og:image uses HTTP instead of HTTPS on a site that has an SSL certificate, the plugin uses the WordPress setup to determine HTTP or HTTPS. Please verify that WordPress is configured to use HTTPS under Admin > Settings > General for both the WordPress Address (URL) and Site Address (URL).

If one of these is configured for HTTP, please go to Admin > Tools > Site Health and expand the notification 'Your website does not use HTTPS' and click 'Update your site to use HTTPS'. WordPress will automatically update the database URLs.

If both use HTTPS and you still see HTTP in the source code, you may have something in your setup that overrides the URL. You can use this snippet to change http/https. The example changes one domain for another but you can change the search and replace parameters to fit your need like I've done with this similar example.

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.

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