The MU forums have moved to WordPress.org

Enable plugin for specific blog as admin (6 posts)

  1. DailyTestimony
    Member
    Posted 15 years ago #

    I have disabled the plugins tab and use plugin commander, some plugins I allow users to manage but some are paid upgrades only. What would be the best way for me as an admin to enable a plugin for a sepcific blog, is there a function I can call in a script or can I make a db query?

  2. tdjcbe
    Member
    Posted 15 years ago #

    Easiest way we've found is to move the plug over to mu-plugins subdirectory and wrap the add_action calls or whatever drives the plugin with a check to see if the blog in question has a option set to allow it to run the plugin.

    example:

    $tmp_check = get_option("run_special_plugins");
    
    if ($tmp_check = true) {
         run_my_plugin();
    }
  3. DailyTestimony
    Member
    Posted 15 years ago #

    Nice workaround, thanks for the idea.

  4. awarner20
    Member
    Posted 15 years ago #

    @ tdjcbe,

    I understand wrapping the add_action call with the check you've listed, but please forgive my following question...

    Where would I set the option to allow a blog to "run special plugins"?

  5. tdjcbe
    Member
    Posted 15 years ago #

    I'd just go into the database for that blog and create a new option under the blog's option table. Use the "run_special_plugins" or whatever you want to use as the option name and set the value to true or 1.

    Since you're going to only do this for a "specific blog" as noted up above, why go through the trouble of writing a plugin or form when it's just as easy to make one small edit in the database directly?

  6. DailyTestimony
    Member
    Posted 15 years ago #

    for refrence if anyone finds this via search I found some functions to enable and disable plugins

    function activate_plugin($blog_id, $plugin) {
    	if (empty($plugin)) return;
    	if (validate_file($plugin)) return;
    	if (!file_exists(ABSPATH . PLUGINDIR . '/' . $plugin)) return;
    	switch_to_blog($blog_id);
    	$current = get_option('active_plugins');
    	ob_start();
    	include_once(ABSPATH . PLUGINDIR . '/' . $plugin);
    	$current[] = $plugin;
    	sort($current);
    	update_option('active_plugins', $current);
    	do_action('activate_' . $plugin);
    	$res = ob_get_clean();
    	if (!empty($res)) echo "Error activating $plugin for blog id=$blog_id: $res<br />";
    	restore_current_blog();
    }
    
    function deactivate_plugin($blog_id, $plugin) {
    	if (empty($plugin)) return;
    	if (validate_file($plugin)) return;
    	if (!file_exists(ABSPATH . PLUGINDIR . '/' . $plugin)) return;
    
    	switch_to_blog($blog_id);
    	$current = get_option('active_plugins');
    	array_splice($current, array_search($plugin, $current), 1 ); // Array-fu!
    	update_option('active_plugins', $current);
    	ob_start();
    	do_action('deactivate_'.$plugin);
    	$res = ob_get_clean();
    	if (!empty($res)) echo "Error deactivating $plugin for blog id=$blog_id: $res<br />";
    	restore_current_blog();
    }

    It takes the blog id for the blog you want to do it on and the relative path to the plugin file from the plugins directory.

About this Topic

  • Started 15 years ago by DailyTestimony
  • Latest reply from DailyTestimony