0

I am looking for a way to create a custom PHP file, where WordPress core is loaded, but only with specific plugins activated.

<?php
require_once '../../wp-load.php';
// load specific plugins

// do something

How is this possible?

6
  • Thanks for downvoting my question without posting any relevant comment as to why - extremely helpful!
    – FooBar
    Commented Jun 3, 2020 at 12:12
  • 1
    What's the problem you're trying to solve that this is meant to fix? Loading WP via wp-load.php usually indicates bad practice, and almost always has a much better alternative, and unforeseen negative consequences
    – Tom J Nowell
    Commented Jun 3, 2020 at 12:33
  • No @TomJNowell. Loading wp-load.php CAN have some usecases. For instance; bypassing the extremly slow WP JSON API. See deliciousbrains.com/…
    – FooBar
    Commented Jun 3, 2020 at 13:37
  • Feel free to chip in with better alternatives on how to speed up the REST API, and please don't tell me to deactivate plugins ;)
    – FooBar
    Commented Jun 3, 2020 at 13:38
  • Object caching for a start, not doing unnecessary things, performance testing, varnish, there are a lot of things that can be done, and most of them are general performance, not REST API performance. Otherwise dedicated handler files carry a high security and maintenance cost, and can introduce unforeseen bugs as things can't hook in or filter anymore. REST API performance isn't that much more than baseline WP performance, so if it's extremely slow then every non-REST page will be slow too, in my experience the REST API is quite fast on my sites
    – Tom J Nowell
    Commented Jun 3, 2020 at 14:19

2 Answers 2

0

This seems to be the way:

add_filter( 'option_active_plugins', 'disable_plugins' );

function disable_plugins($plugins){
    return []; // you can define specific plugins you want to load here.
}
0
// Outputs an array of all plugins.
 var_dump( get_option( 'active_plugins' ) );

 add_filter( 'option_active_plugins', function( $plugins ){
    if ( $my_condition ) {
        unset( $plugins['my-plugin-slug'] );
    }
    return $plugins;
});

// Outputs an empty array.
var_dump( get_option( 'active_plugins' ) );

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.