Programmatically control WordPress’ Search Engine Visibility

I’m writing a simple script & plugin to move a “Staging” WordPress site over to a production site. The approach is simple: mysql dump, check everything into source control, adjust database name, restore in production.

The one problem I’ve run across is that I can’t figure out how to toggle the Search Engine Visibility setting under Settings > Reading. I’d like to have staging be ignored by search engines, but not overwrite that setting in production (after the restore).

Read More

Any ideas on how to do this? Quick and dirty (using sed / find & replace on the sql dump file, etc) is fine by me.

Thanks

Related posts

Leave a Reply

3 comments

  1. WordPress Search Engine Visibility status stored on table option with option name 'blog_public', with value '1' if site visible by search engine, and '0' if site not visible by search engine,,

    So,maybe you can toggle it using update_option from wordpress function,

    update_option('blog_public', '1');
    

    or just direct replace it on database

  2. I wouldn’t use the WordPress site visibility setting for this at all.

    I’d suggest using some/all of these:

    1. Use robots.txt on your staging site (just remember not to copy to live)
       User-agent: *
       Disallow: /
    

    (But you have to trust search engines to respect it, as you do the WordPress setting)

    1. Password protect the staging site using something like .htaccess and .htpasswd (assuming you use an Apache server).

    2. Use .htaccess to limit access to your staging site from any but a small set of IP addresses.

    That way you make the changes once, and you don’t need to do a dangerous sed operation on your database every time you do a sync to live.

  3. TL;DR

    Straightforward solution

    #!/bin/bash
    
    # WordPress > Settings > Reading > Search Engine Visibility
    # 0 - discourages search engines from indexing the site (private)
    # 1 - encourages search engines from indexing the site (public)
    wp_production_blog_public="1"
    
    BLOG_PRIVATE="'blog_public','0'"
    BLOG_PUBLIC="'blog_public','1'"
    BLOG_PUBLICITY_VALUE="'blog_public','$wp_production_blog_public'"
    
    # Use sed to execute search-and-replace
    # These lines ensure the production site will follow the value of
    # the `wp_production_blog_public` even if the staging site is either private or public
    sed "s#$BLOG_PRIVATE#$BLOG_PUBLICITY_VALUE#g" "database.staging.sql" > "database.pre-production.sql"
    sed "s#$BLOG_PUBLIC#$BLOG_PUBLICITY_VALUE#g" "database.pre-production.sql" > "database.production.sql"
    

    Long version

    I am building a similar project as you. With the above script, you can generate a new production database where you can set the value of WordPress’ Search Engine Visibility.

    Input

    You just need a dump file generated by mysqldump or a backup file from phpMyAdmin

    database.staging.sql
    

    Output

    After running the script you would be able to have a database that has been searched-and-replaced.

    database.production.sql