I would like to add a tab with an Activate/Deactivate plugin section

Ok. I spent a full day on this. forum after forum. And (as a 25% programmer) I can’t figure this one out.

I have created a new page within admin using Admin Page Framework. Good stuff, I think, but their documentation sucks.

So I created a page with 4 tabs. Each tab has a form to fill out with the users personal information. I then use this information (via shortcodes) to personalize the users sub site in this multisite.

Great.

I want to create another tab now. In this tab I want to give the user the ability to activate/deactivate a few select plugins. I want to do this because I want to remove the menu items which goes to the installed plugins page (removing the ability for the user to see ALL plugins).

I wrote the following code that I think LOOKS right, but when upload to add this new tab, the page goes blank.

Any suggestions?

public function load_customize_my_mcc_tab_zopim( $oAdminPage ) {

// Create Function to activate/deactivate plugin

function toggle_plugin() {

// Full path to WordPress from the root

$wordpress_path = ‘/nas/content/live/webstorme/’;

// Absolute path to plugins dir

$plugin_path = $wordpress_path.’wp-content/plugins/’;

// Absolute path to your specific plugin

$my_plugin = $plugin_path.’zopim-live-chat/zopim.php’;

// 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);

}

}

// End Activation Plugin

toggle_plugin();

  • Ivan Shulev
    • The Bug Hunter

    Hey Deryk ,

    I hope you are having a nice day so far!

    I have a few suggestions for the code you posted:

    1. No need to hardcode the plugins path. You can get it with the following https://developer.wordpress.org/reference/functions/plugin_dir_path/

    2. There is a missing closing } symbol.

    To investigate the blank page, I would advise you to enable WordPress error logging – https://codex.wordpress.org/Debugging_in_WordPress#Example_wp-config.php_for_Debugging

    Then you can try loading the plugin again and see the error that comes up.

    I am not sure in which context your code is supposed to work. You are more than welcome to provide more details, paste the contents of your debug.log file and we will assist you further!

    I hope this helps and I wish you an awesome day ahead!

    Ivan

  • Deryk
    • The Incredible Code Injector

    Thanks Ivan :slight_smile:

    So, the “missing final }” helped me with the blank page issue.

    From there, I realized that I was not really writing what I wanted to happen anyway. I needed the function to create a link, either activate or deactivate – depending on the state of the plugin.

    So, I searched again and found enough assistance to rewrite the code.

    Problem I have now is a bit weird.

    I got my link to show, but, the content is OUTSIDE of the Tabs area? And in the upper left hand corner of the admin frame.

    I’ve include the entire code here, along with an image of the upper left hand corner. I suspect I am missing something simple that places my echo code inside the tab container?

    Everything in this code works except the final tab, which starts on line 359 (the function starts on line 19)


    UPDATE. Thanks all. I have figured this one out (I think).

    Change 1. The function now returns a variable which is the activate or deactivate link

    function load_zopim_plugin() {

    // Absolute path to your specific plugin
    $my_plugin = plugin_dir_path.'zopim-live-chat/zopim.php';

    // Check to see if plugin is already active
    if(is_plugin_active($my_plugin)) {
    // Deactivate Zopim
    $zopim_plugin = '<a href="/wp-admin/admin.php?page=customize_my_mcc&tab=tab_zopim&action=activate&plugin=zopim-live-chat/zopim.php">Deactivate Zopim Live Chat</a>';
    }
    else {

    // Activate Zopim
    $zopim_plugin = '<a href="/wp-admin/admin.php?page=customize_my_mcc&tab=tab_zopim&action=deactivate&plugin=zopim-live-chat/zopim.php">Activate Zopim Live Chat</a>';
    }
    return $zopim_plugin = $zopim_plugin;
    }

    Next, instead of an oAdminPage function for this particular tab, I created an sContent function, then used return $sContent and loaded the function

    return $sContent

    . ‘Activate Zopim Live Chat ‘ . load_zopim_plugin() . ‘</p>’;

    Now I should check that it actually activates the plugin!

  • Ivan Shulev
    • The Bug Hunter

    Hey Deryk ,

    I hope you are having a nice day so far!

    I am a bit confused.

    UPDATE. Thanks all. I have figured this one out (I think).

    Do you need further assistance?

    Now I should check that it actually activates the plugin!

    Did you figure this out?

    Thank you for clarifying and I wish you an awesome day ahead!

    Ivan

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.