joeguilmette / wp-local-toolbox

A simple plugin to set different defaults for local, staging and production servers.
GNU General Public License v3.0
78 stars 13 forks source link

How to override wp-local-toolbox.php without modifying the plugin files itself #16

Closed onlyskyguy closed 8 years ago

onlyskyguy commented 8 years ago

How can I modify some of the functions in wp-local-toolbox.php without having to edit the actual plugin files from the plugins/wp-local-toolbox directory themselves? Should I add an override in the theme's functions.php?

From the instructions in the readme:

You can add code that will be executed depending on server name by modifying the following in wp-local-toolbox.php.

But I'd like to not have to modify the plugin files themselves so I don't need to redo mods when I update the plugin.

Specifically I'd like to change when the "discourage search engines" option is enabled, because I'm setting up some SEO stuff on the staging server and the SEO plugins don't like that option being off. I have the entire install password protected so it shouldn't be a big deal for now.

I've added the following function to my functions.php:

/**
 * Don't disable search engine on staging
 */

function wp_local_toolbox_enable_search() {

    if (defined('WPLT_SERVER') && WPLT_SERVER) {

        if (strtoupper(WPLT_SERVER) == 'LOCAL' && strtoupper(WPLT_SERVER) == 'local'  && strtoupper(WPLT_SERVER) == 'DEV') {
            /**
             * LOCAL/DEV Environment Everything except PRODUCTION/LIVE/STAGING Environment
             */

            // Hide from robots
            add_filter('pre_option_blog_public', '__return_zero');

        } else {
            /**
             * PRODUCTION/LIVE/STAGING Environment
             */

            // Don't hide from robots
            add_filter('pre_option_blog_public', '__return_true');
        }
    }
}
add_action( 'wp_loaded', 'wp_local_toolbox_enable_search' );

Is this the right approach?

Any help would be appreciated! Thanks for this cool plugin, looking forward to trying out airplane mode on the plane ride home!

joeguilmette commented 8 years ago

Well, if it works for you then it's the right approach. This isn't really a 'plugin', it's more of a tool that people should probably fork and make their own.

What we could do is run an include from wp-local-toolbox.php to somewhere in the wp-content/uploads/ directory. That way you can dump your code there if you have any and update the plugin later without losing your changes. I also personally dislike using functions.php for this kind of stuff, but, again - this is a pretty basic plugin that I put together with the intent of people ripping it apart and making their own.

If you come up with a better way to do this a pull request would be awesome.

jb510 commented 8 years ago

See above simpler patch (untested). That said I'm totally in sync with Joe, this is/was an mu-plugin. It's not something anyone should "regularly" update. Devs should feel free to fork and customize.

joeguilmette commented 8 years ago

Awesome, @jb510, much better solution.

@onlyskyguy, if you pull down the master branch you can use WPLT_FORCE_PUBLIC and the plugin will no longer interfere with your "discourage search engines" settings.