Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plugin only returns fields that were overridden, not the Yoast defaults #3

Open
ashleahhill opened this issue Sep 6, 2017 · 6 comments

Comments

@ashleahhill
Copy link

I know that this plugin hasn't been tested with this version of WP, but I thought I'd post this in case you plan on updating. Yoast itself has vague plans to support the WP REST API themselves, but no timeline that I can see. Yoast/wordpress-seo#3061

  • WP version: 4.8.1
  • Yoast version: 5.4

What's happening

Yoast data is coming back empty for every field.

{
id: 141,
date: "2017-09-06T16:38:35",
date_gmt: "2017-09-06T16:38:35",
...
yoast: {
    focuskw: "",
    title: "",
    metadesc: "",
    linkdex: "",
    metakeywords: "",
    meta-robots-noindex: "",
    meta-robots-nofollow: "",
    meta-robots-adv: "",
    canonical: "",
    redirect: "",
    opengraph-title: "",
    opengraph-description: "",
    opengraph-image: "",
    twitter-title: "",
    twitter-description: "",
    twitter-image: ""
}
}

What's Expected

The Yoast fields should contain the values the plugin would normally add to the HEAD of the document.

<title>Test Page Stuff - name name</title>

<!-- This site is optimized with the Yoast SEO plugin v5.4 - https://yoast.com/wordpress/plugins/seo/ -->
<!-- Admin only notice: this page doesn't show a meta description because it doesn't have one, either write it for this page specifically or go into the SEO -> Titles menu and set up a template. -->
<meta name="robots" content="noindex,follow">
<link rel="canonical" href="http://vccw.dev/test-page-stuff/">
<meta property="og:locale" content="en_US">
<meta property="og:type" content="article">
<meta property="og:title" content="Test Page Stuff - name name">
<meta property="og:description" content="lakjfao;wueoc orjaocnugnzlosio;ohgrhoi">
<meta property="og:url" content="http://vccw.dev/test-page-stuff/">
<meta property="og:site_name" content="name name">
<meta property="article:publisher" content="https://www.facebook.com/namename/">
<meta name="twitter:card" content="summary">
<meta name="twitter:description" content="lakjfao;wueoc orjaocnugnzlosio;ohgrhoi">
<meta name="twitter:title" content="Test Page Stuff - name name">
<meta name="twitter:site" content="@namename">
<meta name="twitter:creator" content="@namename">
<script type="application/ld+json">{"@context":"http:\/\/schema.org","@type":"WebSite","@id":"#website","url":"http:\/\/vccw.dev\/","name":"name name","alternateName":"name name at the name Arcade in Norfolk, VA","potentialAction":{"@type":"SearchAction","target":"http:\/\/vccw.dev\/?s={search_term_string}","query-input":"required name=search_term_string"}}</script>
<script type="application/ld+json">{"@context":"http:\/\/schema.org","@type":"Organization","url":"http:\/\/vccw.dev\/test-page-stuff\/","sameAs":["https:\/\/www.facebook.com\/namename\/","https:\/\/www.instagram.com\/namename\/","https:\/\/twitter.com\/namename"],"@id":"#organization","name":"name name","logo":""}</script>
<!-- / Yoast SEO plugin. -->



</head>

If I override the defaults in the Yoast plugin on the page, then the new value shows up.

@leup
Copy link
Contributor

leup commented Sep 11, 2017

Okay, so I did a really dirty hack to add some metatags values to the REST output. (Only the ones I needed and care about mostly).
You cand find my fork here:
https://github.com/Glasshouse/wp-api-yoast-meta

I had to extend and overrides some Yoast core classes to be able to get some values instead of writing them to HTML document. It seems like there is a lot to do to improve Yoast developer-friendly usability.

@Poliuk
Copy link
Contributor

Poliuk commented Sep 14, 2017

@leup thanks a lot for your contribution. I've been trying to add the yoast title for the categories and the tags, but I haven't been able to make it work yet.

Do you think you could help me? I've been trying here wp-pwa@bbc11e8

screen shot 2017-09-14 at 19 51 32

These are the fields I want to add to the REST API

@leup
Copy link
Contributor

leup commented Sep 14, 2017

@Poliuk I would very much like to help you but I am overflowing with job right now and it could take (a lot of) days before I got any spare time.

@Poliuk
Copy link
Contributor

Poliuk commented Sep 14, 2017

@leup Thanks for your prompt answer 😊 I'll keep trying to finish my part, I'll let you know :)

@Poliuk
Copy link
Contributor

Poliuk commented Sep 19, 2017

Finally found the way, I'll post it here just in case someone lands in this issue:

    // Category
register_rest_field( 'category',
        'yoast',
        array(
            'get_callback'    => array( $this, 'wp_api_encode_yoast_category' ),
            'update_callback' => null,
            'schema'          => null,
        )
    );

  	// Tag
  register_rest_field( 'tag',
        'yoast',
        array(
            'get_callback'    => array( $this, 'wp_api_encode_yoast_tag' ),
            'update_callback' => null,
            'schema'          => null,
        )
    );
function wp_api_encode_taxonomy (){
	include __DIR__ . '/classes/class-frontend.php';

	$wpseo_frontend = WPAPI_WPSEO_Frontend::get_instance();
	$yoastMeta = array(
		'yoast_wpseo_title' => $wpseo_frontend->get_taxonomy_title(),
		'yoast_wpseo_metadesc' => $wpseo_frontend->metadesc(false),
	);
	return (array) $yoastMeta;
}

function wp_api_encode_yoast_category($category) {
	$args=array(
  	'cat' => $category['id'],
	);
	$GLOBALS['wp_query'] = new WP_Query( $args );

	return $this->wp_api_encode_taxonomy();
}

function wp_api_encode_yoast_tag($tag){
	$args=array(
		'tag_id' => $tag['id'],
	);
	$GLOBALS['wp_query'] = new WP_Query( $args );

	return $this->wp_api_encode_taxonomy();
}

@hsimah
Copy link

hsimah commented Apr 10, 2018

See #5 for a nicely laid out solution to this issue.
If you go to the forked repository and download master as a zip you can upload it as a plugin and it functions perfectly with the Yoast generated values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants