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

I wanted to get the Yoast SEO generated data manually, see example code below

This data is being generated by Yoast and automatically add it inside the head tag.

<!-- This site is optimized with the Yoast SEO plugin v4.2.1 - https://yoast.com/wordpress/plugins/seo/ -->
<meta name="description" content="bla bla bla"/>
<meta name="robots" content="noodp"/>
<link rel="canonical" href="http://example.localhost.com/" />
<meta property="og:locale" content="en_US" />
<meta property="og:type" content="website" />
<meta property="og:title" content="bla bla bla" />
<meta property="og:url" content="http://example.localhost.com/" />
<meta property="og:site_name" content="Example.com" />
<meta property="og:image" content="http://example.com.au/wp-content/uploads/2016/11/example.png" />
<meta name="twitter:card" content="summary" />
<meta name="twitter:description" content="bla bla bla." />
<meta name="twitter:title" content="bla bla bla" />
<meta name="twitter:image" content="http://example.com.au/wp-content/uploads/2016/11/example.png" />
<script type='application/ld+json'>{"@context":"http:\/\/schema.org","@type":"WebSite","@id":"#website","url":"http:\/\/example.localhost.com\/","name":"Example","potentialAction":{"@type":"SearchAction","target":"http:\/\/example.localhost.com\/?s={search_term_string}","query-input":"required name=search_term_string"}}</script>
<!-- / Yoast SEO plugin. -->

I do not want to use wp_head(); because it also generates other scripts, styles and whatever plugin or code you have in your wordpress website.

I do not need all those codes. I only want to get the Yoast SEO generated code as shown above. any ideas how could i do that?



Solution 1:[1]

You can get the yoast meta in any page by get_post_meta(),

get the meta values by following,

echo get_post_meta(get_the_ID(), '_yoast_wpseo_metadesc', true); 
echo get_post_meta(get_the_ID(), '_yoast_wpseo_title', true); 

Check the post_meta table to get all the values related to each page/post.

Solution 2:[2]

Use this below:

do_action( 'wpseo_head' );

It will print all SEO header in your head. No need to add through post meta.

Solution 3:[3]

It's four years later, so things have changed here. The data can now be retrieved via API in a structured way: Example:

echo YoastSEO()->meta->for_current_page()->description;

See: https://developer.yoast.com/customization/apis/surfaces-api/

Also via REST a access is possible: https://developer.yoast.com/customization/apis/rest-api

May that sometimes helps. To get the meta at all (unstruktured), the both other ways are sufficient.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ahmed Ginani
Solution 2 Sandeep Kumar
Solution 3 mkours