I would like to create a menu item that activates/deactivates a plugin. Possible?

Greetings Gurus.
So, I thought I had this rocking, but, the link I am creating does not activate nor deactivate the plugin.

Backtrack. Using the Branding tool/plugin, I am removing access to installed plugins for all subsites on my multisite, however,
I would like for these customers to have the ability to activate or deactivate a zopim live chat plugin.

I have successfully created a function to detect if the plugin is active or not. In the If statement I create the links as:
(if active - deactivate)

$zopim_plugin =  wp_nonce_url(admin_url('plugins.php?action=deactivate&plugin=zopim-live-chat/zopim.php&plugin_status=all&paged=1&s'));
$zopim_url = '<a href="' . $zopim_plugin . '">DEactivate Zopim Live Chat</a>';

or (if NOT active, activate)

$zopim_plugin =   wp_nonce_url(admin_url('plugins.php?action=activate&plugin=zopim-live-chat/zopim.php&plugin_status=all&paged=1&s'));
$zopim_url = '<a href="' . $zopim_plugin . '">Activate Zopim Live Chat</a>';

which then creates a link exactly like the one I see in the installed plugins section for activate and deactivate (except the wp_nonce_url function creates a unique &wnonce=######)

But this URL does not work. It does not activate, nor deactivate the plugin - even though the same link works from the installed plugins page.

Can someone explain?

Thanks.

  • Milan

    Hello Deryk

    Hope you are well today and thanks for asking us. :slight_smile:

    Instead of going by link activation and deactivation, can't we use these simple functions on some button/link click ?
    https://codex.wordpress.org/Function_Reference/activate_plugin
    https://codex.wordpress.org/Function_Reference/deactivate_plugins

    I mean here is that you could add some button markup for plugin activation and deactivation and then on button click you can handle the click event and perform necessary steps for your user.

    Isn't this way better than using core wp plugin activation and deactivation links ?
    Cheers,
    Milan

  • Adam Czajczyk

    Hey Deryk,

    I hope you're well today! I've got a piece of code for you that should do the trick by adding a link to a toolbar letting you enable/disable chat. Take a look here please:

    <?php
    /*
    if user can "manage options" add a link to an admin toolbar
    "Turn chat on" if a plugin is currently off
    "Turn chat off" if a plugin is currently on"
    */
    
    if (is_admin()) { // all this should work in dashboard only
    
    add_action('admin_bar_menu', 'add_toolbar_items', 100);
    function add_toolbar_items($admin_bar){   
    
    	if (current_user_can('manage_options')) {
    
    		if ( !is_plugin_active( 'akismet/akismet.php' ) ) {
    			$admin_bar->add_menu( array(
    				'id'    => 'chat-on',
    				'title' => 'Turn chat on',
    				'href'  => 'admin.php?page=toggle-the-chat-plugin-page',
    				'meta'  => array(
    					'title' => __('Turn chat on'),
    					),
    				)
    			);
    		}
    		else {
    			$admin_bar->add_menu( array(
    				'id'    => 'chat-off',
    				'title' => 'Turn chat off',
    				'href'  => 'admin.php?page=toggle-the-chat-plugin-page',
    				'meta'  => array(
    					'title' => __('Turn chat off'),
    					),
    				)
    			);
    		}
    	}
    }	
    
    /* create a page in dashboard for plugin;
    "null" means that page will not be available from any menu
    */
    add_action('admin_menu', 'wpdocs_register_my_custom_submenu_page');
    function wpdocs_register_my_custom_submenu_page() {
        add_submenu_page(
            null,
            'Toggle the chat plugin',
            'toggle the caht plugin',
            'manage_options', // only if user is allowed to toggle plugins
            'toggle-the-chat-plugin-page',
            'toggle_the_chat_plugin_callback' // that's a callback function that actually toggles plugin
        );
    }
    
    /* when page loads check plugin status
    and turn it on/off accordingly
    then display a message
    */
    function toggle_the_chat_plugin_callback() {
    
    	if (current_user_can('manage_options')) {
    
    		if ( !is_plugin_active( 'akismet/akismet.php' ) ) {
    			$result = activate_plugin( 'akismet/akismet.php' );
    			if ( is_wp_error( $result ) ) {
    				echo 'Ooops! Something went wrong!';
    			}
    			else echo 'Chat is enabled now!';
    		} else {
    			$result = deactivate_plugins( 'akismet/akismet.php' );
    			if ( is_wp_error( $result ) ) {
    				echo 'Ooops! Something went wrong!';
    			}
    			else echo 'Chat is disabled now!';
    		}
    	}
    }
    
    }

    You'll probably want to customize messages and/or menu item text but that's cosmetics :slight_smile: I think you should be able to get familiar with the above code in no time. I tested this on my test setup with Akismet plugin and it seems to be working fine so all you really need to do is to replace all the occurrences of

    akismet/akismet.php

    with folder name and main file of your chat plugin, following this scheme:

    plugin_folder/plugin_main_file.php

    There may be some slight adjustments needed but I guess you'll be able to manage them :slight_smile:

    To use the code please create an empty file with a .php extension (e.g. "toggle-chat-plugin.php") and then:

    - paste the code into it
    - make necessary changes as explained above
    - save file and upload it to "/wp-content/mu-plugins" folder on your server

    If there's no "mu-plugins" folder inside "/wp-content", create it.

    I hope that helps and if you have any further questions, just let me know.

    Best regards,
    Adam

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.