Showing posts with label wordpress plug-in. Show all posts
Showing posts with label wordpress plug-in. Show all posts

Friday, 30 September 2011

How to prevent update checks for installed plug-ins in wordpress?




Plug-in is the one which is used to add some additional functionality to the blog. We have many free plug-in in the repository to extend the functionality.

If we get any plug-in, for our requirement we can use it directly or else we will customize the plug-in according to our requirements.

Each time the wordpress will check for the updates in the repository by using the function wp_update_plugins( ) in wp_includes/update.php. If there is any new version available we will get a notification at wordpress plugin dashboard.

If anyone clicks on update the plug-in will be updated to latest version, then the changes what we done will be lost and again we have to update the customizations. There is no hook to avoid this problem.

I faced this problem for one of my website. Each time, when the notification comes for the plug-in my client will update it.

To avoid this problem I added the following code to the plug-in for which I want to exclude from update check.

add_filter( 'http_request_args', 'prevent_update_check', 10, 2 );
function  prevent_update_check( $r, $url ) {
    if ( 0 === strpos( $url, 'http://api.wordpress.org/plugins/update-check/' ) ) {
        $my_plugin = plugin_basename( __FILE__ );
        $plugins = unserialize( $r['body']['plugins'] );
        unset( $plugins->plugins[$my_plugin] );
        unset( $plugins->active[array_search( $my_plugin, $plugins->active )] );
        $r['body']['plugins'] = serialize( $plugins );
    }
    return $r;
}

Tuesday, 13 September 2011

How to Activate or Deactivate wordpress plug-in programmatically?


Recently I had a problem with wordpress plug-in display at the dashboard. I searched on the web but did not find a proper solution. At that time I got a requirement to install one plug-in, I uploaded the plug-in from ftp and the uploaded plug-in is not displayed in the dashboard to activate.

For this I tried the following hook and done the activation of the plug-in.

function activate_plugin() {    
         // Absolute path to your specific plugin
$my_plugin = WP_PLUGIN_DIR.'/wp-email/wp-email.php';
 // Here wp-email is the plug-in which i want to activate. Replace it with the plug-in which you want to activate.
    // Check to see if plugin is already active
    if(is_plugin_active($my_plugin)) {
        // Deactivate plugin
        // Note that deactivate_plugins() will also take an
        // array of plugin paths as a parameter instead of
        // just a single string.
        deactivate_plugins($my_plugin);
    }
    else {
        // Activate plugin
        activate_plugin($my_plugin);
    }
}
toggle_plugin();

Add the above code to the functions.php of your current theme .Once the activation is done comment the code, because we are activating the plug-in if It is in deactivated state and we are deactivating the plug-in if it is active state. So if we did not remove the code the plug-in will be deactivated again.
If you don’t need to deactivation functionality just comment the following line and no need of commenting the whole function
        deactivate_plugins($my_plugin);
This code made my work easy that’s why I am sharing it , so it may be useful for someone.