Skip to content

Instantly share code, notes, and snippets.

@ccurtin
Created October 27, 2017 17:49
Show Gist options
  • Save ccurtin/eb30f47a7ce7d30311dff9a30b9fdd44 to your computer and use it in GitHub Desktop.
Save ccurtin/eb30f47a7ce7d30311dff9a30b9fdd44 to your computer and use it in GitHub Desktop.
Add 'yoast' SEO data to the REST API w/ og & twitter meta fallbacks...
<?php
/*
Creates and end-point that get Yoast SEO meta tags...
*/
add_action('rest_api_init', 'add_yoast_data');
function add_yoast_data()
{
// Add your post types here...
register_rest_field(array(
'post',
'page',
'directors',
'project_post_type'
), 'yoast', array(
'get_callback' => 'wp_api_add_yoast_to_post_types',
'update_callback' => null,
'schema' => null
));
// Public custom post types
$types = get_post_types(array(
'public' => true,
'_builtin' => false
));
foreach ($types as $key => $type) {
register_rest_field($type, 'yoast', array(
'get_callback' => 'wp_api_add_yoast_to_post_types',
'update_callback' => null,
'schema' => null
));
}
}
function wp_api_add_yoast_to_post_types($post, $field_name, $request)
{
$postID = $post['id'];
return return_yoast_array($postID);
}
function return_yoast_array($postID)
{
$postObject = get_post($postID);
$title = get_post_meta($postID, '_yoast_wpseo_title', true) ? get_post_meta($postID, '_yoast_wpseo_title', true) :
get_the_title($postID);
$canonical = get_post_meta($postID, '_yoast_wpseo_canonical', true) ? get_post_meta($postID, '_yoast_wpseo_canonical', true) :
get_the_permalink($postID);
$focus_keyword = get_post_meta($postID, '_yoast_wpseo_focuskw', true); // this is NOT meta-keywords -- Yoast doesn't have that garbage.
$meta_desc = get_post_meta($postID, '_yoast_wpseo_metadesc', true) ? get_post_meta($postID, '_yoast_wpseo_metadesc', true) :
(has_excerpt($postID) && trim(get_the_excerpt($postID)) !== "" ? get_the_excerpt($postID) :
force_balance_tags(html_entity_decode(wp_trim_words(htmlentities(strip_tags($postObject->post_content)), 50, ""))));
/* Opengraph */
$og_title = get_post_meta($postID, '_yoast_wpseo_opengraph-title', true) ? get_post_meta($postID, '_yoast_wpseo_opengraph-title', true) :
$title;
$og_description = get_post_meta($postID, '_yoast_wpseo_opengraph-description', true) ? get_post_meta($postID, '_yoast_wpseo_opengraph-description', true) :
$meta_desc;
$og_image = get_post_meta($postID, '_yoast_wpseo_opengraph-image', true) ? get_post_meta($postID, '_yoast_wpseo_opengraph-image', true) :
get_the_post_thumbnail_url($postID, 'mediumish-featured-image');
/* Twitter */
$twitter_title = get_post_meta($postID, '_yoast_wpseo_twitter-title', true) ? get_post_meta($postID, '_yoast_wpseo_twitter-title', true) :
$title;
$twitter_description = get_post_meta($postID, '_yoast_wpseo_twitter-description', true) ? get_post_meta($postID, '_yoast_wpseo_twitter-description', true) :
$meta_desc;
$twitter__image = get_post_meta($postID, '_yoast_wpseo_twitter-image', true) ? get_post_meta($postID, '_yoast_wpseo_twitter-image', true) :
get_the_post_thumbnail_url($postID, 'mediumish-featured-image');
/* Return yoast meta to REST API. eg: `route: {content:{yoast:{[FIELDS]}}}` */
$yoastMeta = array(
'focuskw' => $focus_keyword,
'title' => $title,
'metadesc' => $meta_desc,
'opengraph_title' => $og_title,
'opengraph_description' => $og_description,
'opengraph_image' => $og_image,
'opengraph_updated_time' => get_post_modified_time('U', false, $postID, false), // returns Epoch timestamp
'twitter_title' => $twitter_title,
'twitter_description' => $twitter_description,
'twitter_image' => $twitter__image,
'linkdex' => get_post_meta($postID, '_yoast_wpseo_linkdex', true),
'metakeywords' => get_post_meta($postID, '_yoast_wpseo_metakeywords', true),
'meta_robots_noindex' => get_post_meta($postID, '_yoast_wpseo_meta-robots-noindex', true),
'meta_robots_nofollow' => get_post_meta($postID, '_yoast_wpseo_meta-robots-nofollow', true),
'meta_robots_adv' => get_post_meta($postID, '_yoast_wpseo_meta-robots-adv', true),
'canonical' => $canonical,
'redirect' => get_post_meta($postID, '_yoast_wpseo_redirect', true),
'opengraph_url' => get_permalink($postID),
'opengraph_site_name' => get_bloginfo('name'),
'opengraph_type' => get_og_type__custom($postObject)
);
return (array) $yoastMeta;
}
/* helper funcs */
function get_og_type__custom($postObject)
{
if (is_front_page() || is_home() || $postObject->post_name == 'homepage') {
$type = 'website';
} else {
$type = 'article';
}
return $type;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment