Last active
November 21, 2017 20:04
-
-
Save jchristopher/ad99aa5a25eca6dbaf09 to your computer and use it in GitHub Desktop.
Piggyback Yoast SEO's output of OpenGraph data to output Pinterest Rich Pin OpenGraph data for WooCommerce products
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Piggyback Yoast SEO OpenGraph output to add Pinterest | |
* Rich Pin OpenGraph data for WooCommerce products | |
*/ | |
add_filter( 'wpseo_opengraph_type', function( $og_type ) { | |
if ( 'product' == get_post_type() ) { | |
$og_type = 'product'; | |
} | |
return $og_type; | |
} ); | |
add_action( 'wp_head', function() { | |
global $post; | |
// make sure it's a WooCommerce product on display | |
if ( 'product' !== get_post_type() || ! class_exists( 'WC_Product' ) ) { | |
return; | |
} | |
$product = new WC_Product( $post ); | |
$product_cost = number_format( floatval( $product->get_price() ), 2 ); ?> | |
<meta property="og:price:amount" content="<?php echo esc_attr( $product_cost ); ?>" /> | |
<meta property="og:price:currency" content="<?php echo esc_attr( get_woocommerce_currency() ); ?>" /><?php | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Does this still work? I tried but no OG meta tags for price were added to the site.