Wordpress-seo: [14.0] Howto replace the deprecated WPSEO_OpenGraph class?

Created on 1 Apr 2020  路  4Comments  路  Source: Yoast/wordpress-seo

In Polylang, we use the wpseo_opengraph action and the object $wpseo_og to add og:locale:alternate tags. See: https://github.com/polylang/polylang/blob/2.7-rc1/modules/plugins/wpseo.php#L323-L343

With the deprecation of the class WPSEO_OpenGraph in #13790, this integration between our 2 plugins will not work anymore (the action has been removed and the class is deprecated and doesn't do anything).

I checked the new code and found the class Locale_Presenter. I suppose that we could add our own presenter in Polylang, however I did not find out how we could ask Yoast SEO to use it. Could you provide me some hint?

Most helpful comment

Hi Chouby,

We've returned the wpseo_opengraph filter in a deprecated state to ensure that integrations do not break in 14.0.

The replacement for this is indeed to add your own presenters and for this we have introduced the wpseo_frontend_presenters filter to do so. On this filter you can add any classes of your own which must extend the Abstract_Indexable_Presenter class.

These classes must implement two functions: get and present.

  • The get function should return the raw value of your meta, usually whatever is in the content property of the meta tag.
  • The present function should return the full meta tag.

Every Presenter class has 3 public properties, helpers, replace_vars and the presentation. You can use these to gather all data you need.

The presentation contains all our data that we're outputting ourselves. If you want the value of our open graph locale you can use $this->presentation->open_graph_locale.

The helpers property currently includes all our helper classes which you can use to, for example, determine whether or not something is an article post type in regards to schema using $this->helpers->schema->article->is_article_post_type( $post_type ).

Please do note that we have another change planned before release to limit the number of helpers available this way as several are intended for internal use and to make testing easier by wrapping WordPress functions.

The replace_vars property ensures you can use the $this->replace_vars( $string ) function to make use of our replacement variables in your own string if so desired.

To bring everything together a custom presenter would look something like this:
```

use Yoast\WP\SEOPresentersAbstract_Indexable_Presenter;

class My_Presenter extends Abstract_Indexable_Presenter {
public function present() {
return '';
}

public function get() {
return ( $this->presentation->open_graph_locale === 'nl_NL' ) ? 'Dutch' : 'Not dutch';
}
}

function add_my_presenter( $presenters ) {
$presenters[] = new My_Presenter();
return $presenters;
}

add_filter( 'wpseo_frontend_presenters', 'add_my_presenter' );

All 4 comments

Hi Chouby,

We've returned the wpseo_opengraph filter in a deprecated state to ensure that integrations do not break in 14.0.

The replacement for this is indeed to add your own presenters and for this we have introduced the wpseo_frontend_presenters filter to do so. On this filter you can add any classes of your own which must extend the Abstract_Indexable_Presenter class.

These classes must implement two functions: get and present.

  • The get function should return the raw value of your meta, usually whatever is in the content property of the meta tag.
  • The present function should return the full meta tag.

Every Presenter class has 3 public properties, helpers, replace_vars and the presentation. You can use these to gather all data you need.

The presentation contains all our data that we're outputting ourselves. If you want the value of our open graph locale you can use $this->presentation->open_graph_locale.

The helpers property currently includes all our helper classes which you can use to, for example, determine whether or not something is an article post type in regards to schema using $this->helpers->schema->article->is_article_post_type( $post_type ).

Please do note that we have another change planned before release to limit the number of helpers available this way as several are intended for internal use and to make testing easier by wrapping WordPress functions.

The replace_vars property ensures you can use the $this->replace_vars( $string ) function to make use of our replacement variables in your own string if so desired.

To bring everything together a custom presenter would look something like this:
```

use Yoast\WP\SEOPresentersAbstract_Indexable_Presenter;

class My_Presenter extends Abstract_Indexable_Presenter {
public function present() {
return '';
}

public function get() {
return ( $this->presentation->open_graph_locale === 'nl_NL' ) ? 'Dutch' : 'Not dutch';
}
}

function add_my_presenter( $presenters ) {
$presenters[] = new My_Presenter();
return $presenters;
}

add_filter( 'wpseo_frontend_presenters', 'add_my_presenter' );

Thank you very much! This filter wpseo_frontend_presenters is exactly what I was lacking.

We've returned the wpseo_opengraph filter in a deprecated state to ensure that integrations do not break in 14.0.

@herregroen it looks like filter signature has still been unexpectedly changed - we're crashing with incorrect parameters, where it wasn't a problem before

Traceback: https://sentry.io/share/issue/e0db6101a5754e76bbe9b5206e3e90dd/

@lkraav Yes, the interface has been modified since then. If you want to see what it should look like now, here is the current status of what we plan to do in Polylang: https://github.com/polylang/polylang/pull/491/files

Was this page helpful?
0 / 5 - 0 ratings