0

The following working script is placed in the mu-plugins directory. It disables plugins within the $myplugins array if the page being viewed is not /contact-us.

This is great, but I want to check for additional pages too. I made an attempt below titled "Broken Example".

Working Example:

$request_uri = parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH );
$is_admin = strpos( $request_uri, '/wp-admin/' );
if( false === $is_admin ){
    // filter active plugins
    add_filter( 'option_active_plugins', function( $plugins ){
        global $request_uri;
        $is_contact_page = strpos( $request_uri, '/contact-us/' );

        $myplugins = array( 
            "contact-form-7/wp-contact-form-7.php", 
            "other plugins...", 
        );
        if( false === $is_contact_page ){
            $plugins = array_diff( $plugins, $myplugins );
        }
        return $plugins;
    } );
}

Broken Example:

    function conditionalPlugins() {

        $myplugins = array( 
            "contact-form-7/wp-contact-form-7.php", 
            "other plugins..."
        );

        if( ! is_page( array( 'about', 'contact-us' ) ) && ! is_admin() ) { 

            // filter active plugins
            add_filter( 'option_active_plugins', function( $plugins ){

                $plugins = array_diff( $plugins, $myplugins );

                return $plugins;
            } );
        }
    }
    add_action( 'get_header', 'conditionalPlugins' );

The ! is_page( array() ) condition works, but the option_active_plugins filter no longer works.

Any ideas?

  • I'd guess option_active_plugins is called before the get_header hook, so by the time you've added the option_active_plugins handler it's too late. Which is maybe why the working code just acts on the URL. – Rup Apr 3 at 23:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.