2

Im trying to disable a plugin on a Wordpress frontpage, to make it a bit lighter. I only want it disabled when the front page is displayed.

I got this working code where I put in wp-content/mu-plugins/ It disables one plugin on all pages except a specified /subpage/ This is great, but it's not exactly what I am after.

How can I change this code to disable a plugin only when viewing the front page ?

Here is the code :

add_filter( 'option_active_plugins', 'lg_disable_plugin' );
function lg_disable_plugin($plugins){

    if(strpos($_SERVER['REQUEST_URI'], '/subpage/') === FALSE AND strpos($_SERVER['REQUEST_URI'], '/wp-admin/') === FALSE) {

        $key = array_search( 'was-this-helpful-pro/was-this-helpful-pro.php' , $plugins );

        if ( false !== $key ) {
            unset( $plugins[$key] );
        }
    }

    return $plugins;
}

I also found another code which works for other pages.... But its the same story , I cannot figure out how to make it work for the frontpage, only works on subpages**

$listener_term = '/subpage/';
$current_url   = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] . '';

// listener for the thin load
if ( strstr( $current_url, $listener_term ) ) {
 add_filter( 'option_active_plugins', 'api_request_disable_plugin' );
}
function api_request_disable_plugin( $plugins ) {
 $plugins_not_needed = array(
 'backupwordpress/backupwordpress.php',
 'wordfence/wordfence.php',
 'contact-form-7-to-database-extension/contact-form-7-db.php',
 'contact-form-7/wp-contact-form-7.php',
 'wp-piwik/wp-piwik.php',
 'simple-responsive-slider/simple-responsive-slider.php',
 'google-sitemap-plugin/google-sitemap-plugin.php',
 'category-page-icons/menu-compouser.php',
 'easy-fancybox/easy-fancybox.php',
 'business-owner-switch/business-owner-switch.php',
 'wordpress-seo/wp-seo.php'
 );

 foreach ( $plugins_not_needed as $plugin ) {
 $key = array_search( $plugin, $plugins );
 if ( false !== $key ) {
 unset( $plugins[ $key ] );
 }
 }

 return $plugins;
}

Any ideas?

I guess I should mention that:

  1. The first example will disable the plugin on all pages except for /subpage/
  2. The second code will disable all the listed plugins on /subpage/

Neither of them work for frontpage/homepage, no matter what I try.

1
  • Should this kind of questions be migrated to Wordpress.stackexchange?
    – Revious
    Commented Oct 4, 2016 at 14:25

3 Answers 3

5

I didn't realise you ONLY wanted it to happen on the Front Page - the code snippet suggests you want it on some other pages too.

I was previously suggesting you could use is_home or is_front_page, but at the stage we are doing this, they have not been loaded yet - so an alternative here is to load in the REQUEST_URI and see if it is blank. If it is blank, we will assume we are on the homepage - to cater for installations that may not be at the root, we compare the URL to the site's relative homepage location - for added safety, we encapsulate it within htmlspecialcharacters (probably not necessary).

Perhaps try something like this (look, it may not be the best way to do it, but it certainly works for me):

add_filter( 'option_active_plugins', 'lg_disable_plugin' );
function lg_disable_plugin($plugins){
    if (htmlspecialchars(trim(wp_make_link_relative(get_site_url()),'/')) == htmlspecialchars(trim($_SERVER['REQUEST_URI'],'/'))) {
        $key = array_search( 'was-this-helpful-pro/was-this-helpful-pro.php' , $plugins );
        if ( false !== $key ) {
            unset( $plugins[$key] );
        }
    }

    return $plugins;
}

If you wanted to do this for multiple plugins, you could change the code to the following:

    add_filter( 'option_active_plugins', 'lg_disable_plugin' );
    function lg_disable_plugin($plugins){
        if (htmlspecialchars(trim(wp_make_link_relative(get_site_url()),'/')) == htmlspecialchars(trim($_SERVER['REQUEST_URI'],'/'))) {
            $plugins_not_needed = array ('was-this-helpful-pro/was-this-helpful-pro.php',
           'pluginfolder/plugin-name.php');
            foreach ( $plugins_not_needed as $plugin ) {
                $key = array_search( $plugin, $plugins );
                if ( false !== $key ) {
                    unset( $plugins[ $key ] );
                }
            }
        }

        return $plugins;
    }
9
  • Perhaps try is_home() instead of is_front_page() - Do you have a static page set as the front page in Reading Settings?
    – Damien
    Commented Mar 17, 2016 at 21:07
  • Are you sure you are loading in wp_head() somewhere in your template (header / or page etc)?
    – Damien
    Commented Mar 17, 2016 at 22:27
  • If you are altering the query, you could possibly try wp_reset_query(); (on the homepage or after you have altered the query - prior to the check... I'm flying in the dark here.. not in the function!
    – Damien
    Commented Mar 17, 2016 at 22:29
  • Have you tried adding echo is_home(); as the first line of code - curious if it outputs 1.
    – Damien
    Commented Mar 17, 2016 at 22:38
  • haha! I am silly. I should re-read your code.. Let me adjust my answer. I see the problem.
    – Damien
    Commented Mar 17, 2016 at 22:44
0

you could write an mu-plugin, if you don't have it, create a folder called "mu-plugins" inside the "wp-content" folder, then create a PHP file where you can write and try this code:

if( !is_admin() && empty( $_POST ) ){
    $uri = $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
    $uriArr = explode( '?',$uri );
    $uri = $uriArr[0];
    $home_uri = str_replace( 'https://','',str_replace( 'http://','',home_url( '/' ) ) );
    if ( $uri === $home_uri ) {
        $paths = array(
            'plugin_folder1/plugin1.php'
            'plugin_folder2/plugin2.php'
        );
        global $paths;
        add_filter( 'option_active_plugins', 'my_option_active_plugins' );
    }
}

function my_option_active_plugins( $plugins ){
    global $paths;
    foreach( $paths as $path ){
        $k = array_search( $path, $plugins );
        if( false !== $k ){
            unset( $plugins[$k] );
        }
    }

    return $plugins;
}

Just replace "plugin_folder1/plugin1.php" ... with the plugins you want to deactivate

Another option is to install the free plugin Freesoul Deactivate Plugins and you will be able to easily deactivate the plugins you want for each page, post, custom post, and archive.

0

Here is an abstraction I wrote that allows you to specify a route and pass it a list of plugins. Print out the plugins to a log or the screen to get their names then just add them to the array and they will get filtered.

Another approach to consider would be replacing targets with a whitelist, so you have to define less things per route (if you have a lot of plugins going).

/**
 * strpos() returns the start position of the substring if found, which 
 * if 0 will trigger false, so prepend something (in this case 'x') 
 * to offset it to return 1 (true).
 */
add_filter( 'option_active_plugins', function( $plugins ){

    $needle = '/some-route/path';
    $haystack = 'x' . $_SERVER['REQUEST_URI']; // prepend something to string 
    if (strpos($haystack, $needle) !== false) {
        $targets = [
            'wecreativez-woo-cart-share-and-save/wecreativez-woo-cart-share-and-save.php',
            'what-the-file/what-the-file.php',
            'woo-address-book/woocommerce-address-book.php',
            'ubermenu/ubermenu.php',
            'flexible-checkout-fields-pro/flexible-checkout-fields-pro.php',
            'ewww-image-optimizer/ewww-image-optimizer.php',
            'svg-support/svg-support.php',
            'gravityforms/gravityforms.php',
            'all-in-one-seo-pack-pro/all_in_one_seo_pack.php',
            'js_composer/js_composer.php',
        ];

        echo "Before Filter: Active Plugins for ($needle):";
        echo '<pre>';
        print_r($plugins);

        foreach( $targets as $target ) {
            array_splice($plugins, array_search($target, $plugins), 1);
        }

        echo "After Filter: Active Plugins for ($needle):";
        print_r($plugins);
        die;

    }

    return $plugins;

}, PHP_INT_MAX);

This could be expanded to use an array of needles (routes) that have an array of plugins to remove for even more control.

Something like:

$needles = [
    '/route-1/' => [
        'gravityforms/gravityforms.php',
    ],
    '/shop/' => [
        'gravityforms/gravityforms.php',
        'ewww-image-optimizer/ewww-image-optimizer.php',
        'flexible-checkout-fields-pro/flexible-checkout-fields-pro.php',
    ],
    'search' [
        'ewww-image-optimizer/ewww-image-optimizer.php',
        'flexible-checkout-fields-pro/flexible-checkout-fields-pro.php',
    ],
];



$haystack = 'x' . $_SERVER['REQUEST_URI']; // prepend something to string 

foreach ($needles as $needle => $targets) {

    if (strpos($haystack, $needle) !== false) {

         foreach( $targets as $target ) {
               array_splice($plugins, array_search($target, $plugins), 1);
         }

    }

}

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.