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 in Others by Kichu
• 19,040 points
115 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 by narikkadan
• 10,840 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 in Others by narikkadan
• 10,840 points
549 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 in Others by narikkadan
• 10,840 points
79 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 in Others by narikkadan
• 10,840 points
71 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 in Others by narikkadan
• 10,840 points
67 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 in Others by narikkadan
• 10,840 points
37 views
0 votes
1 answer

Simple question regarding PHP, Wordpress & Yoast SEO

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

answered Feb 14 in Others by narikkadan
• 10,840 points
33 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 in Others by narikkadan
• 10,840 points
340 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 in Others by narikkadan
• 10,840 points
144 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 in Others by narikkadan
• 10,840 points
87 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 in Others by narikkadan
• 10,840 points
123 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP