Yoast wpseo_title visible in page Yoast SEO section

Yoast wpseo_title visible in page Yoast SEO section

Problem Description:

I want to populate the Yoast SEO title with a custom generated text string (functions.php).

The following add_filter function does provide the right output in my page’s sourcecode.

function seoTitelHomepage() {
  return is_page(get_option('page_on_front')) ? jet_engine()->listings->data->get_option( 'pwp-options::seo-titel-homepage' ) : false;
}
add_filter('wpseo_title', 'seoTitelHomepage');

But since I use a filter function, I only change the sourcecode on output. Therefore the data is not written to the Database.

What I would like is to have my custom text being populated in the wpseo_title database field, in order to see this text also in my Yoast SEO section in my page details.
That way, the Yoast ‘analytics / scoring’ will also work and I can see the nice preview displayed above of the Google result overview.

Any help is greatly appreciated!

Solution – 1

I fixed it myself, below answer is for anyone who needs it.

function seoHomepageTitle() {
  $homepageId = get_option('page_on_front');
  $meta_key = "_yoast_wpseo_title";
  $meta_value = jet_engine()->listings->data->get_option( 'pwp-options::seo-titel-homepage' );
  isset($homepageId , $meta_value) ? update_post_meta($homepageId , $meta_key, $meta_value) : false;
 }

add_action('updated_postmeta', 'seoHomepageTitle');

The trick was the right action hook; updated_postmeta.

A list of Yoast meta keys can be found here: https://shellcreeper.com/yoast-seo-meta-keys-table/

I am open for feedback on the above code, to make it more robust.

Rate this post
We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.
Accept
Reject