我有一个处理自定义程序代码的类,我在其中添加了许多颜色选项。我删掉了一堆重复的部分,所以不会太长;)我的问题是,我无法理解为什么默认设置似乎没有被设置,而只是在前端。如果我在customizer视图中打开,则实时预览是正确的,默认值可以正常工作,但在前端视图上则不行。如果我在customizer中更改一个值以点击保存/发布,那么前端看起来是正确的。因此,这让我觉得默认设置并不像我想象的那样,希望能得到一些帮助,找出原因。非常感谢。
final class showcase_Custom_Colors {
private static $instance;
public function __construct() {
/* Output CSS into <head>. */
add_action( \'wp_head\', array( $this, \'wp_head_callback\' ) );
/* Add options to the theme customizer. */
add_action( \'customize_register\', array( $this, \'customize_register\' ) );
}
public function wp_head_callback() {
$stylesheet = get_stylesheet();
/* Get the cached style. */
$style = wp_cache_get( "{$stylesheet}_custom_colors" );
/* If the style is available, output it and return. */
if ( !empty( $style ) ) {
echo $style;
return;
}
$style = $this->get_primary_color();
$style .= $this->get_secondary_color();
$style .= $this->get_copy_color();
$style .= $this->get_headings_color();
/* Put the final style output together. */
$style = "\\n" . \'<style type="text/css" id="custom-colors-css">\' . trim( $style ) . \'</style>\' . "\\n";
/* Cache the style, so we don\'t have to process this on each page load. */
wp_cache_set( "{$stylesheet}_custom_colors", $style );
/* Output the custom style. */
echo $style;
}
public function editor_styles_callback() {
header( \'Content-type: text/css\' );
echo $this->get_primary_color();
echo $this->get_secondary_color();
echo $this->get_copy_color();
echo $this->get_headings_color();
die();
}
/* Primary color */
public function get_primary_color() {
$style = \'\';
$hex = get_theme_mod( \'color_primary\', \'\' );
$rgb = join( \', \', hybrid_hex_to_rgb( $hex ) );
$style .= "
a,
.page-numbers,
.color-primary,
legend,
mark,
.comment-respond .required,
pre,
code,
.form-allowed-tags code,
pre code {
color: #{$hex};
}";
$style .= "
code,
legend,
mark,
pre,
.form-allowed-tags code {
background-color: rgba( {$rgb}, 0.1 );
}";
$style .= "
blockquote {
border-color: rgba( {$rgb}, 0.85 );
}";
return str_replace( array( "\\r", "\\n", "\\t" ), \'\', $style );
}
public function customize_register( $wp_customize ) {
// Add panel for colors
$wp_customize->add_panel( \'site_colors\', array(
\'title\' => \'Site Colors\',
\'description\' => \'Color options for the site\',
) );
// Defines sections for each color area in an array
$color_sections = array(
\'global_colors\' => array(
\'priority\' => 5,
\'title\' => \'Global Colors\',
\'description\' => \'Colors used in various locations\'
),
\'header_colors\' => array(
\'priority\' => 10,
\'title\' => \'Header Colors\',
\'description\' => \'Colors used in the site header\'
),
);
// Defines settings for each color
$color_settings = array(
\'color_primary\' => \'3855a0\',
\'color_secondary\' => \'e09088\',
\'color_copy\' => \'4a4a4a\',
\'color_headings\' => \'3a3a3a\',
);
// Add each color section from array above
foreach ( $color_sections as $color_section_id => $color_args ) {
$wp_customize->add_section(
$color_section_id,
array(
\'priority\' => $color_args[\'priority\'],
\'panel\' => \'site_colors\',
\'title\' => $color_args[\'title\'],
\'description\' => $color_args[\'description\'],
)
);
}
// Add each color setting from array above
foreach ( $color_settings as $color_setting_id => $color_setting_default ) {
$wp_customize->add_setting(
$color_setting_id,
array(
\'default\' => $color_setting_default,
\'transport\' => \'postMessage\',
\'sanitize_callback\' => \'sanitize_hex_color_no_hash\',
\'sanitize_js_callback\' => \'maybe_hash_hex_color\',
)
);
}
/* Add a control for this color. */
$wp_customize->add_control( new WP_Customize_Color_Control(
$wp_customize,
\'custom-colors-primary\',
array(
\'label\' => esc_html__( \'Primary Color\', \'showcase\' ),
\'section\' => \'global_colors\',
\'settings\' => \'color_primary\',
\'priority\' => 5,
)
) );
$wp_customize->add_control( new WP_Customize_Color_Control(
$wp_customize,
\'custom-colors-secondary\',
array(
\'label\' => esc_html__( \'Secondary Color\', \'showcase\' ),
\'section\' => \'global_colors\',
\'settings\' => \'color_secondary\',
\'priority\' => 10,
)
) );
// Lots more....
}
public static function get_instance() {
if ( !self::$instance )
self::$instance = new self;
return self::$instance;
}
}
showcase_Custom_Colors::get_instance();
澄清代码示例:
add_filter( \'theme_mod_color_primary\', array( $this, \'color_primary_default\' ), 95 );
public function color_primary_default( $hex ) {
return $hex ? $hex : \'e74c3c\';
}