0

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.

2
  • What's the error that FacetWP throws? Commented Jun 4, 2014 at 14:42
  • I get Notice: Undefined index: source in C:\UniServerZ\www\wordpress\wp-content\plugins\facetwp\includes\class-indexer.php on line 154
    – Chris
    Commented Jun 4, 2014 at 15:20

1 Answer 1

0

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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.