Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Jan 2nd, 2013  |  syntax: PHP  |  size: 15.47 KB  |  views: 26  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2. /*
  3. Plugin Name: Admin Bar Disabler
  4. Plugin URI: http://scottkclark.com/wordpress/admin-bar-disabler/
  5. Description: Disable the WP Admin Bar in 3.1+ entirely, or only for roles and capabilities which aren't in the 'whitelist' or 'blacklist'.
  6. Version: 1.0.3
  7. Author: Scott Kingsley Clark
  8. Author URI: http://scottkclark.com/
  9. Text Domain: admin-bar-disabler
  10. */
  11.  
  12. load_plugin_textdomain('admin-bar-disabler', false, basename(dirname(__FILE__)) . 'languages/');
  13.  
  14. add_action('init', 'admin_bar_disabler_network_disable', 8);
  15. add_action('init', 'admin_bar_disabler_disable', 9);
  16.  
  17. function admin_bar_disabler_network_activated ()
  18. {
  19.     if (!function_exists('is_plugin_active_for_network')) {
  20.         $plugins = get_site_option('active_sitewide_plugins', array());
  21.         if (isset($plugins[plugin_basename(__FILE__)])) {
  22.             return true;
  23.         }
  24.         return false;
  25.     }
  26.     return is_plugin_active_for_network(plugin_basename(__FILE__));
  27. }
  28.  
  29. function admin_bar_disabler_network_disable ()
  30. {
  31.     if (is_multisite() && admin_bar_disabler_network_activated()) {
  32.         $disable = false;
  33.         $disable_all = get_site_option('admin_bar_disabler_disable_all', 0);
  34.         if (1 == $disable_all)
  35.             $disable = true;
  36.         $whitelist_roles = get_site_option('admin_bar_disabler_whitelist_roles', array());
  37.         if (false === $disable && !empty($whitelist_roles)) {
  38.             $disable = true;
  39.             if (!is_array($whitelist_roles))
  40.                 $whitelist_roles = array($whitelist_roles);
  41.             foreach ($whitelist_roles as $role) {
  42.                 if (current_user_can($role))
  43.                     return;
  44.             }
  45.         }
  46.         $whitelist_caps = get_site_option('admin_bar_disabler_whitelist_caps', '');
  47.         $whitelist_caps = explode(',', $whitelist_caps);
  48.         if (false === $disable && !empty($whitelist_caps)) {
  49.             $disable = true;
  50.             foreach ($whitelist_caps as $cap) {
  51.                 if (current_user_can($cap))
  52.                     return;
  53.             }
  54.         }
  55.         $blacklist_roles = get_site_option('admin_bar_disabler_blacklist_roles', array());
  56.         if (false === $disable && !empty($blacklist_roles)) {
  57.             if (!is_array($blacklist_roles))
  58.                 $blacklist_roles = array($blacklist_roles);
  59.             foreach ($blacklist_roles as $role) {
  60.                 if (!current_user_can($role))
  61.                     $disable = true;
  62.             }
  63.         }
  64.         $blacklist_caps = get_site_option('admin_bar_disabler_blacklist_caps', '');
  65.         $blacklist_caps = explode(',', $blacklist_caps);
  66.         if (false === $disable && !empty($blacklist_caps)) {
  67.             foreach ($blacklist_caps as $cap) {
  68.                 if (!current_user_can($cap))
  69.                     $disable = true;
  70.             }
  71.         }
  72.         if (false !== $disable) {
  73.             add_filter('show_admin_bar', '__return_false');
  74.             add_action('admin_head', 'admin_bar_disabler_hide');
  75.             remove_action('personal_options', '_admin_bar_preferences');
  76.         }
  77.     }
  78. }
  79.  
  80. function admin_bar_disabler_disable ()
  81. {
  82.     $disable = false;
  83.     $disable_all = get_option('admin_bar_disabler_disable_all', 0);
  84.     if (1 == $disable_all)
  85.         $disable = true;
  86.     $whitelist_roles = get_option('admin_bar_disabler_whitelist_roles', array());
  87.     if (false === $disable && !empty($whitelist_roles)) {
  88.         $disable = true;
  89.         if (!is_array($whitelist_roles))
  90.             $whitelist_roles = array($whitelist_roles);
  91.         foreach ($whitelist_roles as $role) {
  92.             if (current_user_can($role))
  93.                 return;
  94.         }
  95.     }
  96.     $whitelist_caps = get_option('admin_bar_disabler_whitelist_caps', '');
  97.     $whitelist_caps = explode(',', $whitelist_caps);
  98.     if (false === $disable && !empty($whitelist_caps)) {
  99.         $disable = true;
  100.         foreach ($whitelist_caps as $cap) {
  101.             if (current_user_can($cap))
  102.                 return;
  103.         }
  104.     }
  105.     $blacklist_roles = get_option('admin_bar_disabler_blacklist_roles', array());
  106.     if (false === $disable && !empty($blacklist_roles)) {
  107.         if (!is_array($blacklist_roles))
  108.             $blacklist_roles = array($blacklist_roles);
  109.         foreach ($blacklist_roles as $role) {
  110.             if (!current_user_can($role))
  111.                 $disable = true;
  112.         }
  113.     }
  114.     $blacklist_caps = get_option('admin_bar_disabler_blacklist_caps', '');
  115.     $blacklist_caps = explode(',', $blacklist_caps);
  116.     if (false === $disable && !empty($blacklist_caps)) {
  117.         foreach ($blacklist_caps as $cap) {
  118.             if (!current_user_can($cap))
  119.                 $disable = true;
  120.         }
  121.     }
  122.     if (false !== $disable) {
  123.         add_filter('show_admin_bar', '__return_false');
  124.         add_action('admin_head', 'admin_bar_disabler_hide');
  125.         remove_action('personal_options', '_admin_bar_preferences');
  126.     }
  127. }
  128.  
  129. function admin_bar_disabler_hide ()
  130. {
  131. ?>
  132. <style type="text/css">
  133. .show-admin-bar { display: none; }
  134. </style>
  135. <?php
  136. }
  137.  
  138. add_action('admin_menu', 'admin_bar_disabler_create_menu');
  139. function admin_bar_disabler_create_menu ()
  140. {
  141.     add_options_page(__('Admin Bar Disabler', 'admin-bar-disabler'), __('Admin Bar Disabler', 'admin-bar-disabler'), 'administrator', __FILE__, 'admin_bar_disabler_settings_page');
  142.     add_action('admin_init', 'admin_bar_disabler_register_settings');
  143. }
  144.  
  145. add_action('network_admin_menu', 'admin_bar_disabler_create_network_menu');
  146. function admin_bar_disabler_create_network_menu ()
  147. {
  148.     if (is_multisite() && admin_bar_disabler_network_activated()) {
  149.         add_submenu_page('settings.php', 'Admin Bar Disabler', 'Admin Bar Disabler', 'administrator', __FILE__, 'admin_bar_disabler_network_settings_page');
  150.         add_action('network_admin_edit_admin_bar_disabler', 'admin_bar_disabler_network_settings_save');
  151.     }
  152. }
  153.  
  154. function admin_bar_disabler_register_settings ()
  155. {
  156.     register_setting('admin-bar-disabler-settings-group', 'admin_bar_disabler_disable_all');
  157.     register_setting('admin-bar-disabler-settings-group', 'admin_bar_disabler_whitelist_roles');
  158.     register_setting('admin-bar-disabler-settings-group', 'admin_bar_disabler_whitelist_caps');
  159.     register_setting('admin-bar-disabler-settings-group', 'admin_bar_disabler_blacklist_roles');
  160.     register_setting('admin-bar-disabler-settings-group', 'admin_bar_disabler_blacklist_caps');
  161. }
  162.  
  163. function admin_bar_disabler_network_settings_save ()
  164. {
  165.     $fields = array('admin_bar_disabler_disable_all',
  166.                     'admin_bar_disabler_whitelist_roles',
  167.                     'admin_bar_disabler_whitelist_caps',
  168.                     'admin_bar_disabler_blacklist_roles',
  169.                     'admin_bar_disabler_blacklist_caps');
  170.     foreach ($fields as $field) {
  171.         if (isset($_POST[$field]) && !empty($_POST[$field])) {
  172.             update_site_option($field, $_POST[$field]);
  173.         }
  174.         else
  175.             delete_site_option($field);
  176.     }
  177.     wp_redirect('settings.php?page=' . $_POST['page'] . '&updated=true');
  178.     die();
  179. }
  180.  
  181. function admin_bar_disabler_network_settings_page ()
  182. {
  183.     global $wp_roles;
  184.     if (!isset($wp_roles))
  185.         $wp_roles = new WP_Roles();
  186.     $roles = $wp_roles->get_names();
  187.     if (isset($_GET['updated'])) {
  188. ?>
  189.     <div id="message" class="updated"><p><?php _e('Options saved.', 'admin-bar-disabler'); ?></p></div>
  190. <?php
  191.     }
  192. ?>
  193. <div class="wrap">
  194.     <h2><?php _e('Admin Bar Disabler', 'admin-bar-disabler'); ?></h2>
  195.     <form method="post" action="edit.php?action=admin_bar_disabler">
  196.         <?php wp_nonce_field('admin_bar_disabler'); ?>
  197.         <input type="hidden" name="page" value="<?php echo esc_attr($_GET['page']); ?>"/>
  198.         <table class="form-table">
  199.             <tr valign="top">
  200.                 <th scope="row"><?php _e('Disable for Everyone?', 'admin-bar-disabler'); ?></th>
  201.                 <td><input type="checkbox" name="admin_bar_disabler_disable_all"
  202.                            value="1"<?php echo (1 == get_site_option('admin_bar_disabler_disable_all', 0) ? ' CHECKED' : ''); ?> />
  203.                 </td>
  204.             </tr>
  205.             <tr valign="top">
  206.                 <th scope="row"><?php _e('Roles Whitelist', 'admin-bar-disabler'); ?></th>
  207.                 <td>
  208.                     <select name="admin_bar_disabler_whitelist_roles[]" size="10" style="height:auto;" MULTIPLE>
  209. <?php
  210.     $whitelist_roles = get_site_option('admin_bar_disabler_whitelist_roles', array());
  211.     if (!is_array($whitelist_roles))
  212.         $whitelist_roles = array($whitelist_roles);
  213.     foreach ($roles as $role => $name) {
  214. ?>
  215.                             <option value="<?php echo esc_attr($role); ?>"<?php echo (in_array($role, $whitelist_roles) ? ' SELECTED' : ''); ?>><?php echo $name; ?></option>
  216. <?php
  217.     }
  218. ?>
  219.                     </select>
  220.                     <br/><em><?php _e('ONLY show the Admin Bar for Users with these Role(s) - CTRL + Click for multiple selections', 'admin-bar-disabler'); ?></em>
  221.                 </td>
  222.             </tr>
  223.             <tr valign="top">
  224.                 <th scope="row"><?php _e('Capabilities Whitelist<br />(comma-separated)', 'admin-bar-disabler'); ?></th>
  225.                 <td>
  226.                     <input type="text" name="admin_bar_disabler_whitelist_caps" value="<?php echo get_site_option('admin_bar_disabler_whitelist_caps', ''); ?>"/>
  227.                     <br/><em><?php _e('ONLY show the Admin Bar for Users with these Capabilies', 'admin-bar-disabler'); ?></em>
  228.                 </td>
  229.             </tr>
  230.             <tr valign="top">
  231.                 <th scope="row"><?php _e('Roles Blacklist', 'admin-bar-disabler'); ?></th>
  232.                 <td>
  233.                     <select name="admin_bar_disabler_blacklist_roles[]" size="10" style="height:auto;" MULTIPLE>
  234. <?php
  235.     $blacklist_roles = get_site_option('admin_bar_disabler_blacklist_roles', array());
  236.     if (!is_array($blacklist_roles))
  237.         $blacklist_roles = array($blacklist_roles);
  238.     foreach ($roles as $role => $name) {
  239. ?>
  240.                                 <option value="<?php echo esc_attr($role); ?>"<?php echo (in_array($role, $blacklist_roles) ? ' SELECTED' : ''); ?>><?php echo $name; ?></option>
  241. <?php
  242.     }
  243. ?>
  244.                     </select>
  245.                     <br/><em><?php _e('DO NOT show the Admin Bar for Users with these Role(s) - CTRL + Click for multiple selections', 'admin-bar-disabler'); ?></em>
  246.                 </td>
  247.             </tr>
  248.             <tr valign="top">
  249.                 <th scope="row"><?php _e('Capabilities Blacklist<br />(comma-separated)', 'admin-bar-disabler'); ?></th>
  250.                 <td>
  251.                     <input type="text" name="admin_bar_disabler_blacklist_caps" value="<?php echo get_site_option('admin_bar_disabler_blacklist_caps', ''); ?>"/>
  252.                     <br/><em><?php _e('DO NOT show the Admin Bar for Users with these Capabilies', 'admin-bar-disabler'); ?></em>
  253.                 </td>
  254.             </tr>
  255.         </table>
  256.         <p class="submit">
  257.             <input type="submit" class="button-primary" value="<?php _e('Save Changes', 'admin-bar-disabler'); ?>"/>&nbsp;&nbsp;
  258.             <small>
  259.                 <strong><?php _e('Do not use Blacklist in combination with Whitelist, in all cases Whitelist overrides Blacklist', 'admin-bar-disabler'); ?></strong>
  260.             </small>
  261.         </p>
  262.     </form>
  263. </div>
  264. <?php
  265. }
  266.  
  267. function admin_bar_disabler_settings_page ()
  268. {
  269.     global $wp_roles;
  270.     if (!isset($wp_roles))
  271.         $wp_roles = new WP_Roles();
  272.     $roles = $wp_roles->get_names();
  273.     if (isset($_GET['settings-updated'])) {
  274. ?>
  275.     <div id="message" class="updated"><p><?php _e('Options saved', 'admin-bar-disabler'); ?></p></div>
  276. <?php
  277.     }
  278. ?>
  279. <div class="wrap">
  280.     <h2><?php _e('Admin Bar Disabler', 'admin-bar-disabler'); ?></h2>
  281.     <form method="post" action="options.php">
  282.         <?php settings_fields('admin-bar-disabler-settings-group'); ?>
  283.         <?php do_settings_sections('admin-bar-disabler-settings-group'); ?>
  284.         <table class="form-table">
  285.             <tr valign="top">
  286.             <tr valign="top">
  287.                 <th scope="row"><?php _e('Disable for Everyone?', 'admin-bar-disabler'); ?></th>
  288.                 <td><input type="checkbox" name="admin_bar_disabler_disable_all" value="1"<?php echo (1 == get_option('admin_bar_disabler_disable_all', 0) ? ' CHECKED' : ''); ?> />
  289.                 </td>
  290.             </tr>
  291.             <th scope="row"><?php _e('Roles Whitelist', 'admin-bar-disabler'); ?></th>
  292.             <td>
  293.                 <select name="admin_bar_disabler_whitelist_roles[]" size="10" style="height:auto;" MULTIPLE>
  294. <?php
  295.     $whitelist_roles = get_option('admin_bar_disabler_whitelist_roles', array());
  296.     if (!is_array($whitelist_roles))
  297.         $whitelist_roles = array($whitelist_roles);
  298.     foreach ($roles as $role => $name) {
  299. ?>
  300.                         <option value="<?php echo esc_attr($role); ?>"<?php echo (in_array($role, $whitelist_roles) ? ' SELECTED' : ''); ?>><?php echo $name; ?></option>
  301. <?php
  302.     }
  303. ?>
  304.                 </select>
  305.                 <br/><em><?php _e('ONLY show the Admin Bar for Users with these Role(s) - CTRL + Click for multiple selections', 'admin-bar-disabler'); ?></em>
  306.             </td>
  307.             </tr>
  308.             <tr valign="top">
  309.                 <th scope="row"><?php _e('Capabilities Whitelist<br />(comma-separated)', 'admin-bar-disabler'); ?></th>
  310.                 <td>
  311.                     <input type="text" name="admin_bar_disabler_whitelist_caps" value="<?php echo get_option('admin_bar_disabler_whitelist_caps', ''); ?>"/>
  312.                     <br/><em><?php _e('ONLY show the Admin Bar for Users with these Capabilies', 'admin-bar-disabler'); ?></em>
  313.                 </td>
  314.             </tr>
  315.             <tr valign="top">
  316.                 <th scope="row"><?php _e('Roles Blacklist', 'admin-bar-disabler'); ?></th>
  317.                 <td>
  318.                     <select name="admin_bar_disabler_blacklist_roles[]" size="10" style="height:auto;" MULTIPLE>
  319. <?php
  320.     $blacklist_roles = get_option('admin_bar_disabler_blacklist_roles', array());
  321.     if (!is_array($blacklist_roles))
  322.         $blacklist_roles = array($blacklist_roles);
  323.     foreach ($roles as $role => $name) {
  324. ?>
  325.                             <option value="<?php echo esc_attr($role); ?>"<?php echo (in_array($role, $blacklist_roles) ? ' SELECTED' : ''); ?>><?php echo $name; ?></option>
  326. <?php
  327.     }
  328. ?>
  329.                     </select>
  330.                     <br/><em><?php _e('DO NOT show the Admin Bar for Users with these Role(s) - CTRL + Click for multiple selections', 'admin-bar-disabler'); ?></em>
  331.                 </td>
  332.             </tr>
  333.             <tr valign="top">
  334.                 <th scope="row"><?php _e('Capabilities Blacklist<br />(comma-separated)', 'admin-bar-disabler'); ?></th>
  335.                 <td>
  336.                     <input type="text" name="admin_bar_disabler_blacklist_caps" value="<?php echo get_option('admin_bar_disabler_blacklist_caps', ''); ?>"/>
  337.                     <br/><em><?php _e('DO NOT show the Admin Bar for Users with these Capabilies', 'admin-bar-disabler'); ?></em>
  338.                 </td>
  339.             </tr>
  340.         </table>
  341.         <p class="submit">
  342.             <input type="submit" class="button-primary" value="<?php _e('Save Changes', 'admin-bar-disabler'); ?>"/>&nbsp;&nbsp;
  343.             <small>
  344.                 <strong><?php _e('Do not use Blacklist in combination with Whitelist, in all cases Whitelist overrides Blacklist', 'admin-bar-disabler'); ?></strong>
  345.             </small>
  346.         </p>
  347.     </form>
  348. </div>
  349. <?php
  350. }
clone this paste RAW Paste Data