How to Access Yoast SEO tags in Wordpress API Callback

0 votes

I'm developing a Wordpress website with a ReactJS SPA theme and I'm attempting to collect the necessary SEO head tags from the Yoast SEO plugin during an WP API AJAX call

I added a direct call to wpseo_head() in my header.php file just after the opening body tag

<?php wp_head(); ?>
</head>
<body>
<div id="wpseo_head">
<?php do_action('wpseo_head'); ?>
</div>

Within my api callback to load the post I capture the output of wpseo_head() and return it.

global $post, $wp_query;
$post = $_post;
$wp_query = new WP_Query(['p' => $_post->ID]);
setup_postdata($post);

ob_start();
do_action('wpseo_head');
$yoast_seo_tags = ob_get_contents();
ob_end_clean();
$yoastSeoTags = stripslashes($yoast_seo_tags);
Question is 
please explain this and how I can successfully grab the output of the wpseo_head() call within an API callback?


Feb 10, 2022 in Others by Kichu
• 19,040 points
1,823 views

1 answer to this question.

0 votes
use rest api to request for the meta info but it require user auth so use the below code 
$provider = new WPSEO_Indexable_Service_Post_Provider();
$metainfo = $provider->get($selected_post->ID);
and for more info use this article  https://github.com/Yoast/wordpress-seo/issues/9218 
answered Feb 12, 2022 by narikkadan
• 63,640 points

Related Questions In Others

0 votes
1 answer

How to set meta tags using Angular universal SSR and ngx-seo plug-in?

first Install the plug-in with npm i ngx-seo ...READ MORE

answered Feb 11, 2022 in Others by narikkadan
• 63,640 points
2,190 views
0 votes
1 answer

How to copy title of WP post dynamically to focus keyword field in Yoast SEO?

$title = get_the_title(); $args = array('posts_per_page' => -1, ...READ MORE

answered Feb 24, 2022 in Others by narikkadan
• 63,640 points
1,293 views
0 votes
1 answer

How to echo WP SEO Yoast synonyms in PHP?

Title: "<?php echo WPSEO_Meta::get_value("title"); ?>"<br /> Description: "<?php ...READ MORE

answered Feb 24, 2022 in Others by narikkadan
• 63,640 points
703 views
0 votes
1 answer

how to show the <meta> values of yoast seo with some function?

check this folder : wordpress-seo\src\presenters\open-graph you can ...READ MORE

answered Feb 10, 2022 in Others by narikkadan
• 63,640 points
597 views
0 votes
1 answer

Yoast SEO (WordPress Plugin) - Get plugin generated data manually

get_post_meta(), use this to get the meta ...READ MORE

answered Feb 11, 2022 in Others by narikkadan
• 63,640 points
1,495 views
0 votes
1 answer

Simple question regarding PHP, Wordpress & Yoast SEO

the way the search engine displays your ...READ MORE

answered Feb 14, 2022 in Others by narikkadan
• 63,640 points
494 views
0 votes
1 answer

how can I change yoast seo generated sitemap file URL?

plugins/wordpress-seo/inc/sitemaps/class-sitemaps-router.php go to this directory and change the ...READ MORE

answered Feb 22, 2022 in Others by narikkadan
• 63,640 points
3,553 views
0 votes
1 answer

How to add a class to the <a> tag in Yoast SEO breadcrumbs?

add_filter( 'wpseo_breadcrumb_single_link', 'ss_breadcrumb_single_link', 10, 2 ); function ss_breadcrumb_single_link( ...READ MORE

answered Feb 22, 2022 in Others by narikkadan
• 63,640 points
1,551 views
0 votes
1 answer

How to add meta keyword in wordpress using yoast seo plugin.?

if you dont want to use code ...READ MORE

answered Feb 12, 2022 in Others by narikkadan
• 63,640 points
991 views
0 votes
1 answer

How to echo WP SEO Yoast synonyms in PHP?

 the meta_key in wp_postmeta table is _yoast_wpseo_keyword ...READ MORE

answered Feb 10, 2022 in Others by narikkadan
• 63,640 points
1,124 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP