WordPress.org

Make WordPress Core

Opened 4 years ago

Last modified 3 days ago

#21627 new enhancement

Filter for custom-background CSS selector

Reported by: Horttcore Owned by: Horttcore
Milestone: 4.7 Priority: low
Severity: minor Version: 3.4.1
Component: Customize Keywords: has-patch needs-testing needs-unit-tests
Focuses: Cc:

Description

There should be an easier way for changing the css selector from body to html or any other then making your own callback.

Attachments (6)

theme.diff (426 bytes) - added by Horttcore 4 years ago.
Filter applied to css selector
21627.diff (848 bytes) - added by westonruter 2 years ago.
Use theme feature attribute instead of filter for selector
21627.3.patch (4.1 KB) - added by peterwilsoncc 17 months ago.
21627.4.diff (3.6 KB) - added by peterwilsoncc 17 months ago.
21627.5.diff (4.6 KB) - added by peterwilsoncc 17 months ago.
Pass settings using WP_Customize_Manager::customize_preview_settings
21627.6.diff (4.2 KB) - added by peterwilsoncc 13 months ago.

Download all attachments as: .zip

Change History (28)

@Horttcore
4 years ago

Filter applied to css selector

#1 @nacin
4 years ago

Why might modifying this selector be necessary?

#2 @Horttcore
4 years ago

With the filter you could set it to any other element i.e. html which could be better for the theme.

Here is a sample code:

body {
   margin: auto;
   width: 965px;
}

With the current selector just a part of the site will have the custom background.
The background should be applied to the highest element in the DOM by default and that would be html.

We could add this filter or just change the selector to the html element.

What do you prefer?

#3 @cais
4 years ago

  • Cc edward.caissie@… added

#4 @rulatir
4 years ago

Very much seconded.

#5 @ocean90
3 years ago

#25379 was marked as a duplicate.

#6 @ocean90
3 years ago

Having a filter in a custom callback doesn't sound like a good plan, since we cannot ensure that plugin/theme authors are including the filter in a custom callback too.

#7 @dreamwhisper
3 years ago

  • Cc dreamwhisper added

#8 @nacin
3 years ago

  • Component changed from Themes to Appearance

@westonruter
2 years ago

Use theme feature attribute instead of filter for selector

#9 @westonruter
2 years ago

  • Milestone changed from Awaiting Review to Future Release

Replying to ocean90:

Having a filter in a custom callback doesn't sound like a good plan, since we cannot ensure that plugin/theme authors are including the filter in a custom callback too.

Right, when the custom-background theme support is added, the theme may supply a customwp-head-callback callback which would not be required to the proposed filter. However, if there was a filter, then themes would have much less cause to have to override wp-head-callback's default callback of _custom_background_cb to begin with. I would imagine that providing a custom callback for wp-head-callback is usually due to a different selector being needed.

As an alternative to providing a filter, there could be a new default element-selector property of the custom-background theme support. A custom theme callback would still need to honor element-selector, but they also have to do this already for background repeats, positions, and attachments. See 21627.diff.

#10 @celloexpressions
2 years ago

  • Milestone changed from Future Release to 4.1

+1 for 21627.diff.

#11 follow-up: @SergeyBiryukov
2 years ago

The selector also appears in customize-preview.js. Should it be updated there as well?

#12 in reply to: ↑ 11 @celloexpressions
2 years ago

  • Keywords needs-patch added; has-patch removed
  • Priority changed from normal to low
  • Severity changed from trivial to minor

Replying to SergeyBiryukov:

The selector also appears in customize-preview.js. Should it be updated there as well?

Yes.

#13 @ocean90
23 months ago

  • Milestone changed from 4.1 to Future Release

#14 follow-up: @peterwilsoncc
17 months ago

Sorry, ignore .3

21627.4.diff builds on @westonruter's patch to include:

  • customize-preview.js
  • take theme authors preference in get_body_class()

customize-preview.js requires the element be selectable without the class, so I've split the element and the selector into two options applied to the theme-support array.

#15 in reply to: ↑ 14 @westonruter
17 months ago

Replying to peterwilsoncc:

Sorry, ignore .3

21627.4.diff builds on @westonruter's patch to include:

  • customize-preview.js
  • take theme authors preference in get_body_class()

customize-preview.js requires the element be selectable without the class, so I've split the element and the selector into two options applied to the theme-support array.

Thanks, but accessing settings from the parent frame via window.parent._wpCustomizeSettings isn't going to make it into Core. So either you need to use postMessage passing, or export the settings directly into the preview instead via WP_Customize_Manager::customize_preview_settings().

@peterwilsoncc
17 months ago

Pass settings using WP_Customize_Manager::customize_preview_settings

#16 @peterwilsoncc
17 months ago

Replying to westonruter:

Replying to peterwilsoncc:
So either you need to use postMessage passing, or export the settings directly into the preview instead via WP_Customize_Manager::customize_preview_settings().

Thanks for the feedback, I've updated in 21627.5.diff to use the correct method.

Whilst this code allows theme authors to customise the element and the class, it still enforces the overloaded element.classname selector, is it worth including a third setting and defaulting to the overloaded selector?

#17 @wonderboymusic
13 months ago

  • Keywords needs-refresh added

Patch doesn't apply

#18 @peterwilsoncc
13 months ago

  • Keywords has-patch added; needs-patch needs-refresh removed

Refreshed against trunk in 21627.6.diff

Renamed the theme support options and added the ability to customise the selector in the CSS, default values are:

add_theme_support( 'custom-background', array(
        'background-element'     => 'body',
        'background-class'       => 'custom-background',
        'background-selector'    => '%1$s.%2$s', // background-element.background-class
) );

An example customisation in a theme's functions.php might be:

add_theme_support( 'custom-background', array(
        'background-element'     => 'html',
        'background-class'       => 'has-custom-background',
        'background-selector'    => '.has-custom-background',
) );
Last edited 13 months ago by peterwilsoncc (previous) (diff)

This ticket was mentioned in Slack in #core-customize by celloexpressions. View the logs.


3 weeks ago

#20 @celloexpressions
3 weeks ago

  • Keywords needs-testing added
  • Milestone changed from Future Release to 4.7

Quickly testing 21627.6.diff, I was not able to get it to work with Twenty Fifteen, adding 'background-selector' => '#main'. However, that was likely an error on my end and I think we're close to being ready with this improvement. Moving for 4.7 for consideration.

#21 @peterwilsoncc
3 weeks ago

  • Keywords needs-unit-tests added

Tests required:

  • various selectors
    • element
    • class
    • id
    • descendant
    • child
    • pseudo classes & elements

I'll need to go back and check the reason behind background-class to consider whether theme authors ought to be able to set an ID instead.

This ticket was mentioned in Slack in #core-customize by celloexpressions. View the logs.


3 days ago

Note: See TracTickets for help on using tickets.