Skip to content

Instantly share code, notes, and snippets.

@michaelbourne
Last active October 13, 2021 23:00
Show Gist options
  • Save michaelbourne/fa371fd4452afdc26b9a1c386c4e32e6 to your computer and use it in GitHub Desktop.
Save michaelbourne/fa371fd4452afdc26b9a1c386c4e32e6 to your computer and use it in GitHub Desktop.
Change the data presented with Yoast's Enhanced Slack Sharing
<?php
// functions.php in a child theme
/**
* Change Enhanced Slack sharing data labels.
*
* @param array $data The default Slack labels + data.
* @param Indexable_Presentation $presentation The indexable presentation object from Yoast.
*
* @return array $data The new Slack labels + data.
*/
function mb_slack_enhanced_pages( $data, $presentation ) {
// $presentation->model->object_id is our $post_id
if ( is_singular( 'event' ) ) {
// These are sample post_meta terms you could reference for a CPT
$date = get_post_meta( $presentation->model->object_id, 'event_date', true );
$location = get_post_meta( $presentation->model->object_id, 'event_location', true );
$data = [
'Location' => $location,
'Date' => $hours,
];
} elseif ( is_singular( 'product' ) ) {
// this would be for Woocommerce
$wcprod = wc_get_product( $presentation->model->object_id );
$price = $wcprod->get_price();
$sku = $wcprod->get_sku();
$data = [
'Price' => $price,
'SKU' => $sku,
];
} elseif ( is_singular( 'page' ) ) {
$data = [
'Who Am I?' => 'Michael Bourne',
'What Do I Write?' => 'Cool Shit',
];
}
return $data;
}
add_filter( 'wpseo_enhanced_slack_data', 'mb_slack_enhanced_pages', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment