/ Articles

Disabling WordPress Plugins on the Frontend

I love the Page Analysis admin feature of Yoast’s WordPress SEO, but I didn’t need the overhead of all the stuff it adds to to the frontend. (I prefer to add my own custom fields using Pods, and code the rest of what I need into my theme).

After trying to disable various filters and actions one by one, I found this much more elegant solution.

First, deactivate the plugin.

Then, create a “Must-Use Plugin”, and drop in this code:

add_filter( 'option_active_plugins', 'enable_plugins_selectively' );

function enable_plugins_selectively( $plugins ) {

     $currentPage = add_query_arg( array() );

     $pagesToActivate = array('post.php','post-new.php');
 
     foreach ($pagesToActivate as $p) {
          if (strpos($currentPage,$p)) {
               $isAdmin = 1;
          } 
     }

     if ($isAdmin) {
          $plugins[] = 'wordpress-seo/wp-seo.php';
     }
     return $plugins;
}

This will make your plugin of choice only activated when you’re on any of the pages you define in $pagestoActivate.

Note: do not look for a blanket “/wp-admin/” in the URL to cover all admin pages. If you do, visiting the Plugins page will have the side effect of reactivating the plugin site-wide.

Leave a Reply

Your email will not be published.