Find out in this tutorial how to automatically change the indexing of a site depending on the WordPress development environment.
Following the tutorial that explains how to enable or disable plugins automatically depending on WordPress environment, a subscriber asked me if it was possible to achieve something similar but for the “Discourage search engines from indexing this site” checkbox.
That is, that when working in staging this option is activated (to avoid a duplicate indexing) and that when going to production it is automatically deactivated.
I thought it was a fantastic suggestion because by automating it we avoid that the new site in production from being left without indexing by an oversight.
Also, if you have already defined a type of environment in your WordPress installation (production, development, staging…), you can put this into practice in a really simple way.
Haven’t you done it yet and you want to take advantage of this tutorial? Define the environment first as I showed you in the previous tutorial.
Already done? Then, when you apply this new strategy, the “Discourage search engines from indexing this site” checkbox located in Settings > Reading will be enabled or disabled depending on the environment you’ve defined in WordPress.
Here are the steps to follow:
Snippet to enable or disable the option to index the site automatically depending on the WordPress environment
Add the following code at the end of functions.php or in your functionality plugin:
With this snippet you are using the
wp_get_environment_type() function to determine what the working environment is and indicate that when it is ‘staging the “Discourage search engines from indexing the site” option should be enabled.
On the other hand, when it detects that it is in production, it will automatically disable it. One less thing to worry about!
You can see that by combining the
wp_get_environment_type() function with the modification of the
blog_public value you can simplify your workflow when moving from one WordPress environment to another.
Now you know how to make sure that a website is indexed correctly when you move it from staging to production in WordPress. Say good bye to the confusion. 😉
Any questions? I read you in the comments.
And if you want to give me any suggestion for future tutorials, leave it in the contact form. Advantages of being a subscriber. 😉