admin管理员组文章数量:1332353
I'm trying to get the stylesheet of the currently active admin color scheme to style a plugin to fit into the color context.
As mentioned in this post I had a look at the implementation of admin_color_scheme_picker()
in wp-admin/includes/misc.php:564
. They use a global variable called $_wp_admin_css_colors
but when I try to access (var_dump()
) it, it's NULL
.
Is there any way to access to actual CSS code?
Why is the global variable NULL
? I did a grep -n '$_wp_admin_css_colors' -r .
and couldn't find any line where it gets destroyed...
EDIT
I see it wasn't clear what I exactly want. I don't want to access the actual CSS code, but the color codes that gets parsed into CSS. In wp-includes/general-template.php:2117
the default themes are registered like this:
wp_admin_css_color( 'fresh', _x( 'Default', 'admin color scheme' ),
admin_url( "css/colors$suffix.css" ),
array( '#222', '#333', '#0074a2', '#2ea2cc' ),
array( 'base' => '#999', 'focus' => '#2ea2cc', 'current' => '#fff' )
);
And I'd like to access the variables like this in PHP. Copying the definition is not an option, since other plugins and themes could add their theme themselves.
I'm trying to get the stylesheet of the currently active admin color scheme to style a plugin to fit into the color context.
As mentioned in this post I had a look at the implementation of admin_color_scheme_picker()
in wp-admin/includes/misc.php:564
. They use a global variable called $_wp_admin_css_colors
but when I try to access (var_dump()
) it, it's NULL
.
Is there any way to access to actual CSS code?
Why is the global variable NULL
? I did a grep -n '$_wp_admin_css_colors' -r .
and couldn't find any line where it gets destroyed...
EDIT
I see it wasn't clear what I exactly want. I don't want to access the actual CSS code, but the color codes that gets parsed into CSS. In wp-includes/general-template.php:2117
the default themes are registered like this:
wp_admin_css_color( 'fresh', _x( 'Default', 'admin color scheme' ),
admin_url( "css/colors$suffix.css" ),
array( '#222', '#333', '#0074a2', '#2ea2cc' ),
array( 'base' => '#999', 'focus' => '#2ea2cc', 'current' => '#fff' )
);
And I'd like to access the variables like this in PHP. Copying the definition is not an option, since other plugins and themes could add their theme themselves.
Share Improve this question edited Apr 13, 2017 at 12:37 CommunityBot 1 asked Jan 23, 2014 at 15:59 Julian F. WeinertJulian F. Weinert 6002 gold badges5 silver badges18 bronze badges3 Answers
Reset to default 2I actually made it!
In my script file, at the to I define a new variable and copy the $_wp_admin_css_colors
variable into it using the admin_head
hook, since the variable gets destroyed at some later point:
<?php
global $admin_colors; // only needed if colors must be available in classes
add_action('admin_head', function() {
global $_wp_admin_css_colors;
global $admin_colors; // only needed if colors must be available in classes
$admin_colors = $_wp_admin_css_colors;
});
// more script content...
?>
Both answers are very helpful. Julian's answer grabs all the color schemes in play. tfrommen's answer finds the currently enabled one. If we combine both of those, we can go further and actually use it.
Here's an example that grabs the current color scheme and dynamically inserts a stylesheet with CSS variables corresponding to each color:
class MyTheme
{
static $ADMINCOLS = [];
static function ImprintCols()
{
add_action('admin_head', function()
{
global $_wp_admin_css_colors;
if(!$_wp_admin_css_colors) return;
$color = get_user_meta(get_current_user_id(), 'admin_color', true);
if(key_exists($color,$_wp_admin_css_colors))
self::$ADMINCOLS = $_wp_admin_css_colors[$color];
});
add_action('admin_footer', function()
{
if(!self::$ADMINCOLS) return;
$vars = [''];
foreach(self::$ADMINCOLS->colors as $key=>$col)
$vars[] = "\t--wp-admin-color-$key: $col;";
if(isset(self::$ADMINCOLS->icon_colors))
foreach(self::$ADMINCOLS->icon_colors as $key=>$col)
$vars[] = "\t--wp-admin-color-icon-$key: $col;";
$vars[] = '';
$styles = ":root {".join(PHP_EOL,$vars)."\t}";
echo '<style id="wp-admin-cols">'.PHP_EOL.$styles.PHP_EOL.'</style>'.PHP_EOL;
});
}
MyTheme::ImprintCols();
This will create a style sheet declaration like the following (here I'm using the delectable Ectoplasm color scheme):
<style id="wp-admin-cols">
:root {
--wp-admin-color-0: #413256;
--wp-admin-color-1: #523f6d;
--wp-admin-color-2: #a3b745;
--wp-admin-color-3: #d46f15;
--wp-admin-color-icon-base: #ece6f6;
--wp-admin-color-icon-focus: #fff;
--wp-admin-color-icon-current: #fff;
}
</style>
Then any admin-based stylesheet can use these CSS variables, as so:
h1 { border-bottom: 2px solid var(--wp-admin-color-0); }
The color scheme is stored as user meta:
$color = get_user_meta(get_current_user_id(), 'admin_color', true);
However, the CSS file seems to be generated when having chosen another scheme.
So you just need this:
$admin_css_file = wp_admin_css_uri('css/colors');
本文标签: WordPress 38 get current admin color scheme
版权声明:本文标题:WordPress 3.8 get current admin color scheme 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742321542a2452887.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论