Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/js/_enqueues/wp/customize/preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -671,12 +671,16 @@
settings[ prop ] = api( 'background_' + prop );
} );

var bgElement = ( api.settings.theme && api.settings.theme.bgElement ) ? api.settings.theme.bgElement : document.body;
var bgClass = ( api.settings.theme && api.settings.theme.bgClass ) ? api.settings.theme.bgClass : 'custom-background';
var bgSelector = ( api.settings.theme && api.settings.theme.bgSelector ) ? api.settings.theme.bgSelector : 'body.custom-background';

/*
* The body will support custom backgrounds if either the color or image are set.
*
* See get_body_class() in /wp-includes/post-template.php
*/
$( document.body ).toggleClass( 'custom-background', !! ( settings.color() || settings.image() ) );
$( bgElement ).toggleClass( bgClass, !! ( settings.color() || settings.image() ) );

if ( settings.color() ) {
css += 'background-color: ' + settings.color() + ';';
Expand All @@ -690,7 +694,7 @@
css += 'background-attachment: ' + settings.attachment() + ';';
}

$( '#custom-background-css' ).text( 'body.custom-background { ' + css + ' }' );
$( '#custom-background-css' ).text( bgSelector + ' { ' + css + ' }' );
}
};

Expand Down
14 changes: 14 additions & 0 deletions src/wp-includes/class-wp-customize-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2151,6 +2151,17 @@ public function customize_preview_settings() {
restore_previous_locale();
}

/*
* Export custom-background element, class, and selector into the preview frame.
* bgElement and bgClass are exported separately so the JS can toggle the class
* on the correct DOM element; bgSelector is the final CSS selector used in the
* injected <style> tag.
*/
$has_custom_bg = current_theme_supports( 'custom-background' );
$bg_element = $has_custom_bg ? tag_escape( get_theme_support( 'custom-background', 'background-element' ) ) : 'body';
$bg_class = $has_custom_bg ? sanitize_html_class( get_theme_support( 'custom-background', 'background-class' ) ) : 'custom-background';
$bg_selector = $has_custom_bg ? sprintf( get_theme_support( 'custom-background', 'background-selector' ), $bg_element, $bg_class ) : 'body.custom-background';

$settings = array(
'changeset' => array(
'uuid' => $this->changeset_uuid(),
Expand All @@ -2164,6 +2175,9 @@ public function customize_preview_settings() {
'stylesheet' => $this->get_stylesheet(),
'active' => $this->is_theme_active(),
'isBlockTheme' => wp_is_block_theme(),
'bgElement' => $bg_element,
'bgClass' => $bg_class,
'bgSelector' => $bg_selector,
),
'url' => array(
'self' => $self_url,
Expand Down
3 changes: 2 additions & 1 deletion src/wp-includes/post-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -802,7 +802,8 @@ function get_body_class( $css_class = '' ) {

if ( current_theme_supports( 'custom-background' )
&& ( get_background_color() !== get_theme_support( 'custom-background', 'default-color' ) || get_background_image() ) ) {
$classes[] = 'custom-background';

$classes[] = sanitize_html_class( get_theme_support( 'custom-background', 'background-class' ) );
}

if ( has_custom_logo() ) {
Expand Down
9 changes: 8 additions & 1 deletion src/wp-includes/theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -1948,7 +1948,11 @@ function _custom_background_cb() {
$processor = new WP_HTML_Tag_Processor( '<style id="custom-background-css"></style>' );
$processor->next_tag();

$style_tag_content = 'body.custom-background { ' . trim( $style ) . ' }';
$element = tag_escape( get_theme_support( 'custom-background', 'background-element' ) );
$class = sanitize_html_class( get_theme_support( 'custom-background', 'background-class' ) );
$selector = sprintf( get_theme_support( 'custom-background', 'background-selector' ), $element, $class );

$style_tag_content = $selector . ' { ' . trim( $style ) . ' }';
$processor->set_modifiable_text( "\n{$style_tag_content}\n" );
echo "{$processor->get_updated_html()}\n";
}
Expand Down Expand Up @@ -2893,6 +2897,9 @@ function add_theme_support( $feature, ...$args ) {
'wp-head-callback' => '_custom_background_cb',
'admin-head-callback' => '',
'admin-preview-callback' => '',
'background-element' => 'body',
'background-class' => 'custom-background',
'background-selector' => '%1$s.%2$s', // sprintf template: %1$s = background-element, %2$s = background-class.
);

$jit = isset( $args[0]['__jit'] );
Expand Down
Loading