Skip to content

Instantly share code, notes, and snippets.

@ocean90
Last active December 16, 2015 21:39
Show Gist options
  • Select an option

  • Save ocean90/5501516 to your computer and use it in GitHub Desktop.

Select an option

Save ocean90/5501516 to your computer and use it in GitHub Desktop.
#WordPressPlugin: Switch admin color scheme via toolbar
<?php
/**
* Plugin Name: Admin Color Switcher
* Version: 0.1
* Description: Switch admin color scheme via toolbar
* Author: Dominik Schilling
* Author URI: http://wphelper.de/
* Plugin URI: http://wpgrafie.de/
*
*
* License: GPLv2 or later
*
* Copyright (C) 2013 Dominik Schilling
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/**
* Don't call this file directly.
*/
if ( ! class_exists( 'WP' ) ) {
die();
}
/**
* The class which controls the plugin.
*
* Used hooks:
* - wp_before_admin_bar_render
*
* Inits at 'admin_init' hook.
*
*/
class DS_Admin_Color_Switcher {
/**
* A name for the change request
*
* @since 0.1
* @var string
*/
static public $name = 'ds_switch_admin_color';
/**
* Hooks into 'wp_before_admin_bar_render' to register toolbar items.
* Calls update_user_meta() when a change is requested.
*
* @since 0.1
*/
public static function init() {
if ( isset( $_REQUEST[ self::$name ] ) )
self::update_user_meta();
add_action( 'wp_before_admin_bar_render', array( __CLASS__, 'admin_bar' ) );
}
/**
* Renders the toolbar items.
*
* @since 0.1
*/
public static function admin_bar() {
global $wp_admin_bar, $_wp_admin_css_colors;
// Get the current color
$current_color = get_user_option( 'admin_color', get_current_user_id() );
// Add parent item
$wp_admin_bar->add_menu( array(
'id' => 'admin-color-scheme',
'title' => $_wp_admin_css_colors[ $current_color ]->name,
'href' => '#'
) );
// Add sub items
foreach ( $_wp_admin_css_colors as $color => $color_info ) {
$title = $color_info->name;
// Highlight current color
if ( $color == $current_color )
$title = sprintf( '<strong>%s</strong>' , $color_info->name );
$link = add_query_arg( self::$name, $color );
$wp_admin_bar->add_menu( array(
'parent' => 'admin-color-scheme',
'id' => 'admin-color-scheme-' . $color,
'title' => $title,
'href' => $link
) );
}
}
/**
* Updates the user meta.
*
* @since 0.1
*/
public static function update_user_meta() {
update_user_meta(
get_current_user_id(),
'admin_color',
$_REQUEST[ self::$name ]
);
}
}
// Beam me up, Scotty.
add_action( 'admin_init', array( 'DS_Admin_Color_Switcher', 'init' ) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment