WordPress.org

Make WordPress Core

Opened 4 years ago

Last modified 26 hours ago

#21627 assigned enhancement

Filter for custom-background CSS selector

Reported by: Horttcore Owned by: peterwilsoncc
Milestone: 4.8 Priority: low
Severity: minor Version: 3.4.1
Component: Customize Keywords: needs-testing needs-unit-tests needs-patch
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 (7)

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 20 months ago.
21627.4.diff (3.6 KB) - added by peterwilsoncc 20 months ago.
21627.5.diff (4.6 KB) - added by peterwilsoncc 19 months ago.
Pass settings using WP_Customize_Manager::customize_preview_settings
21627.6.diff (4.2 KB) - added by peterwilsoncc 15 months ago.
21627.2.diff (4.3 KB) - added by peterwilsoncc 8 weeks ago.

Download all attachments as: .zip

Change History (33)

@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
2 years ago

  • Milestone changed from 4.1 to Future Release

#14 follow-up: @peterwilsoncc
20 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
20 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
19 months ago

Pass settings using WP_Customize_Manager::customize_preview_settings

#16 @peterwilsoncc
19 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
15 months ago

  • Keywords needs-refresh added

Patch doesn't apply

#18 @peterwilsoncc
15 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 15 months ago by peterwilsoncc (previous) (diff)

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


3 months ago

#20 @celloexpressions
3 months 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 months 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.


2 months ago

#23 @celloexpressions
2 months ago

  • Owner changed from Horttcore to peterwilsoncc
  • Status changed from new to assigned

Feel free to punt if there isn't time to finish this for 4.7; seems like you're the best person to own this for now @peterwilsoncc.

This ticket was mentioned in Slack in #core by jeffpaul. View the logs.


2 months ago

#25 @peterwilsoncc
8 weeks ago

  • Milestone changed from 4.7 to Future Release

I will bump this for a future release, the old patch is missing a bunch of escaping. The timeline for getting that in and unit tests seems a little tight.

21627.2.diff includes some escaping but I'm after some advice on escaping the selector.

#26 @westonruter
26 hours ago

  • Keywords needs-patch added; has-patch removed
  • Milestone changed from Future Release to 4.8

@peterwilsoncc the background settings being added in WP_Customize_Manager should not be escaped because they are part of an array that will be safely serialized to JSON, and escaping should only be done at printing time.

In terms of escaping, you can look at wp_custom_css_cb() which escapes just by doing strip_tags. The key need there is to ensure that a user doesn't enter </style> or any script tags. It's important to not use esc_html() because that will corrupt descendant selectors, like html &gt; body.

That being said, I don't think escaping is entirely appropriate because the selector is being defined statically in code. It's not coming from user data.

Note: See TracTickets for help on using tickets.