php 将'yoast'SEO数据添加到REST API w / og&twitter meta fallbacks ...
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 将'yoast'SEO数据添加到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;
}
?>
以上是关于php 将'yoast'SEO数据添加到REST API w / og&twitter meta fallbacks ...的主要内容,如果未能解决你的问题,请参考以下文章
php 将Yoast SEO优先级更改为“低”以在Yoast metabox之前获得ACF字段。