Update the setting index/noindex of Yoast SEO in PHP for each post

Logo PHP PHP
Logo Wordpress Wordpress

Par ,
Publié le May 17, 2020

If you are using the CMS WordPress, you might know the plugin Yoast SEO. It is one of the best plugin to manage your SEO and be indexed correctly by search engines like Google.

The basis of SEO is set in the tag <meta name="robots" content="index, follow" />., in the header of each page of your website (within the tag <head>. If your pages don’t get this tag with the attribute “index”, don’t worry. Google considers every pages can be followed and indexed, except if they get the attribute “noindex”. The attribute “follow” tells search engines they can analyse this specific page. The attribute “index” tells this page have to appear in rearch engine results.

To change this attribute with yoast, there are different ways :

  1. Change the general settings of the plugin, for each type of content (posts, pages, taxonomies…).
  2. Change this setting on specific pages or posts, with the yoast metabox > Advanced > “Allow search engines to index this page ?”.
  3. It is also possible to change this parameter in PHP, by adding some lines of code on your website.

Here we will deal with the third solution. First, what is the matter of this method ? It will help you save time. In my case, my editors are editing contents on front-end. I want to change this setting when the editor write some elements on a post. Then, when the content of the page is complete, I will put the Yoast setting to “index”.

Update the parameter index / noindex in PHP.

This setting is stored in a custom field of each post or page, named _yoast_wpseo_meta-robots-noindex. (Be careful, this is different for taxonomies,  but we’ll see that in a second part). So we’ll just need to update this specific post meta (custom field).

  • If _yoast_wpseo_meta-robots-noindex is NULL (or not defined), then the page will use the default settings for the page.
  • If _yoast_wpseo_meta-robots-noindex is equal to ‘1’, then your post will be set to “noindex”.
  • If _yoast_wpseo_meta-robots-noindex is equal to ‘2’, your post will be set to “index”.

Pour update the Yoast SEO setting “index / noindex” in PHP, I am going to add this line of code (at the place I want to edit this setting.

update_post_meta($post_ID,'_yoast_wpseo_meta-robots-noindex', '2');

Example 1 : On my template single.php, I will update this setting, when an editor update the content of the post.

// If the editor submit content on this post
if(isset($_POST['submit'])){
  // Then, I will update the yoast parameter to "index"
  update_post_meta($post_ID,'_yoast_wpseo_meta-robots-noindex', '2');
}

Example 2 : On my admin pages, I want to automatically edit this parameter to “INDEX” when an editor complete a post.

Note : If you want to edit this parameter when you edit a post on ADMIN, you need to deactivate the metabox for this post. Otherwise, yoast will reset the setting, just after you update it.

// In my file functions.php
// If a user edit a post, a page or a custom post (change the tag {custom_post}
function save_{custom_post}_metaboxes($post_ID){
  // If the user added the subtitle and the video of my post, then "INDEX" this post
  if(isset($_POST['sous_titre']) || $_POST['video']){
    update_post_meta($post_ID,'_yoast_wpseo_meta-robots-noindex', '2');
  }
add_action('save_post','save_{custom_post}_metaboxes');

Specific case for taxonomies

Unfortunately, it won’t work for tanonomies, because Yoast SEO is not using the term meta (custom field). Instead, it will store this setting in its own tables. So it will be necessary to edit the tables of yoast. Hopefully Yoast SEO proides the function (hook) WPSEO_Taxonomy_Meta::set_value() to do it easily.

// Je règle le paramètre de mataxonomie sur INDEX.
WPSEO_Taxonomy_Meta::set_value((int) $term_id, $term->taxonomy, 'index');

If you have a question, just let a comment :).

Subscribe
Notify of
0 Commentaires
Inline Feedbacks
View all comments