Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
    
What's the error that FacetWP throws? –  TheDeadMedic Jun 4 at 14:42
    
I get Notice: Undefined index: source in C:\UniServerZ\www\wordpress\wp-content\plugins\facetwp\includes\class-indexer.ph‌​p on line 154 –  Chris Jun 4 at 15:20

1 Answer 1

up vote 0 down vote accepted

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.

share|improve this answer
    
Thanks @TheDeadMedic. A really clever solution :) –  Chris Jun 4 at 15:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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