admin管理员组文章数量:1130349
Till now in my Wordpress themes, I was implementing few necessary styling options in theme customizer which would edit the CSS and override it by outputting that CSS to head inside <style> tags, for example here I implemented color picker and I output the styles if the default state is changed:
function dc_get_gradient_colors() {
$first_color = get_theme_mod( 'primary_color_1' );
if ( $link_color != '#000000' ) :
?>
<style type="text/css">
.main-color-1-color{
color: <?php echo $link_color; ?> !Important;
}
.main-color-1-background-color{
background-color: <?php echo $link_color; ?> !Important;
}
.main-color-1-border-color{
border-color: <?php echo $link_color; ?> !important;
}
</style>
<?php
endif;
}
add_action( 'wp_head', 'get_gradient_colors' );
Is there any better way for doing this, including editing CSS file directly. I know that stylesheet can be in form of PHP file, so there I could put the conditionals depending on theme options. But is that the best way, or is there way to write to style.css from theme customizer?
Till now in my Wordpress themes, I was implementing few necessary styling options in theme customizer which would edit the CSS and override it by outputting that CSS to head inside <style> tags, for example here I implemented color picker and I output the styles if the default state is changed:
function dc_get_gradient_colors() {
$first_color = get_theme_mod( 'primary_color_1' );
if ( $link_color != '#000000' ) :
?>
<style type="text/css">
.main-color-1-color{
color: <?php echo $link_color; ?> !Important;
}
.main-color-1-background-color{
background-color: <?php echo $link_color; ?> !Important;
}
.main-color-1-border-color{
border-color: <?php echo $link_color; ?> !important;
}
</style>
<?php
endif;
}
add_action( 'wp_head', 'get_gradient_colors' );
Is there any better way for doing this, including editing CSS file directly. I know that stylesheet can be in form of PHP file, so there I could put the conditionals depending on theme options. But is that the best way, or is there way to write to style.css from theme customizer?
Share Improve this question asked Nov 11, 2018 at 23:33 UsceUsce 4671 gold badge4 silver badges20 bronze badges 3 |1 Answer
Reset to default 0
function my_enqueue_customizer_stylesheet() {
wp_register_style( 'my-custom-css', get_template_directory_uri() . 'assets/css/customizer.css', NULL, NULL, 'all' );
wp_enqueue_style( 'my-custom-css' );
}
add_action( 'customize_controls_print_styles', 'my_enqueue_customizer_stylesheet' );
Till now in my Wordpress themes, I was implementing few necessary styling options in theme customizer which would edit the CSS and override it by outputting that CSS to head inside <style> tags, for example here I implemented color picker and I output the styles if the default state is changed:
function dc_get_gradient_colors() {
$first_color = get_theme_mod( 'primary_color_1' );
if ( $link_color != '#000000' ) :
?>
<style type="text/css">
.main-color-1-color{
color: <?php echo $link_color; ?> !Important;
}
.main-color-1-background-color{
background-color: <?php echo $link_color; ?> !Important;
}
.main-color-1-border-color{
border-color: <?php echo $link_color; ?> !important;
}
</style>
<?php
endif;
}
add_action( 'wp_head', 'get_gradient_colors' );
Is there any better way for doing this, including editing CSS file directly. I know that stylesheet can be in form of PHP file, so there I could put the conditionals depending on theme options. But is that the best way, or is there way to write to style.css from theme customizer?
Till now in my Wordpress themes, I was implementing few necessary styling options in theme customizer which would edit the CSS and override it by outputting that CSS to head inside <style> tags, for example here I implemented color picker and I output the styles if the default state is changed:
function dc_get_gradient_colors() {
$first_color = get_theme_mod( 'primary_color_1' );
if ( $link_color != '#000000' ) :
?>
<style type="text/css">
.main-color-1-color{
color: <?php echo $link_color; ?> !Important;
}
.main-color-1-background-color{
background-color: <?php echo $link_color; ?> !Important;
}
.main-color-1-border-color{
border-color: <?php echo $link_color; ?> !important;
}
</style>
<?php
endif;
}
add_action( 'wp_head', 'get_gradient_colors' );
Is there any better way for doing this, including editing CSS file directly. I know that stylesheet can be in form of PHP file, so there I could put the conditionals depending on theme options. But is that the best way, or is there way to write to style.css from theme customizer?
Share Improve this question asked Nov 11, 2018 at 23:33 UsceUsce 4671 gold badge4 silver badges20 bronze badges 3- 1 Note that writing to a file will involve file system access, which would bypass version control, and also reduce security for those who put WP on a read only file system. Can you ellaborate further on the problem you're trying to solve? Your question mostly focuses on solutions without explaining why it's an issue – Tom J Nowell ♦ Commented Nov 12, 2018 at 0:45
- Thanks @TomJNowell on clarifying such security issues on this. Actually this in this state everything is working, but I am looking for "better" way to implement stylesheet which is generated from theme customizer, then creating output to head of the page. Is outputting to head of the page, the only way to override properties form style.css, with options retrieved from theme customizer? – Usce Commented Nov 12, 2018 at 0:49
-
You can override
style.cssby having CSS inline or in a file appear after it, with a higher specificity, that part isn't a WP thing. Since what you have already works I'd stick with it – Tom J Nowell ♦ Commented Nov 12, 2018 at 0:55
1 Answer
Reset to default 0
function my_enqueue_customizer_stylesheet() {
wp_register_style( 'my-custom-css', get_template_directory_uri() . 'assets/css/customizer.css', NULL, NULL, 'all' );
wp_enqueue_style( 'my-custom-css' );
}
add_action( 'customize_controls_print_styles', 'my_enqueue_customizer_stylesheet' );
本文标签: Edit stylecss via theme customizer
版权声明:本文标题:Edit style.css via theme customizer 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749190374a2330093.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


style.cssby having CSS inline or in a file appear after it, with a higher specificity, that part isn't a WP thing. Since what you have already works I'd stick with it – Tom J Nowell ♦ Commented Nov 12, 2018 at 0:55