Using actions, hooks and filters in a non-WordPress page

I have a php script that sits outside of wordpress, but loads WP core with wp-load.php.

Here is my basic page:

?php
define('WP_USE_THEMES', false);
global $wp, $wp_query, $wp_the_query, $wp_did_header;
require_once('wp-load.php');
switch_to_blog(1); //make sure we're on the top level site
$author_id = 77; //admin
?

However, this also loads plugins - one of which (FacetWP) is throwing an error. As I don't need it, I thought I'd deactivate it with:

function deactivate_plugin_conditional() {
    deactivate_plugins('facetwp/index.php');    
}
add_action( 'admin_init', 'deactivate_plugin_conditional' );

However, this didn't appear to work, so I thought I'd try a simple echo:

function myDebug() {
    echo "test";
}
add_action( 'init', 'myDebug', 10, 1 );

This didn't work either, so I'm clearly not understanding how to use these API calls. Any help with this problem would be much appreciated.

Topic deactivated-plugin wp-load.php actions api hooks Wordpress

Category Web


It's catch 22 - you need WordPress to use the hook system, but init will have already fired during load (wp-settings.php to be exact).

I would create a MU "Must Use" plugin (wp-content/mu-plugins/any-filename.php) for all your "outside of WordPress" functionality, with something like this at the start:

if ( ! defined( 'LOADED_EXTERNAL' ) || ! LOADED_EXTERNAL )
    return;

/**
 * Better technique for temporarily disabling a plugin on-the-fly.
 * 
 * @param   array   $plugins
 * @return  array
 */
function wpse_147541_active_plugins( $plugins ) {
    if ( $plugin = array_search( 'facetwp/index.php', $plugins ) )
        unset( $plugins[ $plugin ] );
    return $plugins;
}

add_filter( 'option_active_plugins', 'wpse_147541_active_plugins' );

// More awesome code!

And then in your external file:

define( 'LOADED_EXTERNAL', true );
require 'wp-load.php'; // No need for globalising variables, they'll all be in scope

The reason I advocate a MU plugin is that they run before regular plugins, so you'll have time to intercept "FacetWP" (or any other plugins for that matter) from loading.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.