WordPress.org

Make WordPress Core

Opened 3 years ago

Closed 9 months ago

Last modified 9 months ago

#18753 closed enhancement (fixed)

Add data to stylesheets

Reported by: kobenland Owned by: ryan
Milestone: 3.6 Priority: normal
Severity: normal Version:
Component: Template Keywords: has-patch commit 2nd-opinion needs-codex
Focuses: Cc:

Description

In my humble opinion it would be awesome to provide theme developers an easy function to add extra data to registered stylesheets.

This way they could use the 'wp_head' action to cleanly make stylesheets conditional for IE, make them alternate or add RTL support.

Like wp_add_inline_style(), this function could really contribute to decluttering template files.

Attachments (7)

functions.wp-styles.php.patch (1.8 KB) - added by kobenland 3 years ago.
Patch
18753.twentythirteen.diff (910 bytes) - added by obenland 10 months ago.
This is how Twenty Thirteen could benefit from it.
18753.diff (1.5 KB) - added by obenland 10 months ago.
18753.1.diff (1.1 KB) - added by obenland 9 months ago.
18753.twentythirteen.1.diff (910 bytes) - added by obenland 9 months ago.
18753.twentytwelve.diff (1.0 KB) - added by obenland 9 months ago.
18753.2.diff (2.2 KB) - added by nacin 9 months ago.

Download all attachments as: .zip

Change History (24)

kobenland3 years ago

Patch

comment:1 nacin3 years ago

This exists currently, but you have to dig into $wp_styles and use add_data(), such as add_data( 'ie', 'conditional', 'lte IE 7' ) and add_data( $handle, 'rtl', true );.

It's complex (I didn't provide a code sample as I'd have to look it up beyond what I provided) and I wouldn't mind some wrappers here.

comment:2 kobenland2 years ago

  • Keywords 2nd-opinion added

comment:3 bitacre2 years ago

I concur.

comment:4 WraithKenny22 months ago

  • Cc Ken@… added

comment:5 westi21 months ago

This is really nice.

Maybe a 3.5 candidate.

comment:6 follow-up: ryanve18 months ago

@kobenland I like the goal of this idea. I also feel that the script/style system is more complex that it needs to be and it needs to be simplified. I think it would be better to implement a more general data method that could be used for many purposes rather than adding a function that only gets used a few times. A function like the example below would be capable of storing data related to scripts/styles and practically anything else. Also (loosely related) see #22249

// Get or set data via key/value pair.
function wp_data ($k = null, $v = null) {

    static $hash; // php.net/manual/en/language.variables.scope.php
    isset($hash) or $hash = array(); // only initiates once

    if ( func_num_args() > 1 )
        return $hash[$k] = $v; // set

    if ( is_scalar($k) ) 
        return $hash[$k]; // get
        
    if ( is_null($k) )
        return $hash; // get all

    return $hash = array_merge($hash, (array) $k); // set multi

}
Last edited 18 months ago by ryanve (previous) (diff)

comment:7 in reply to: ↑ 6 obenland18 months ago

Replying to ryanve:

@kobenland I like the goal of this idea. I also feel that the script/style system is more complex that it needs to be and it needs to be simplified. I think it would be better to implement a more general data method that could be used for many purposes rather than adding a function that only gets used a few times.

I appreciate your feedback! I merely proposed an abstraction of existing functionality though. Maybe it would be a good idea to open a new ticket with what you came up with?

comment:8 philiparthurmoore12 months ago

  • Cc philip@… added

obenland10 months ago

This is how Twenty Thirteen could benefit from it.

comment:9 obenland10 months ago

Added patch for Twenty Thirteen.

comment:10 dd3210 months ago

  • Keywords 2nd-opinion removed

Going by the function names in that file, and the add_data() method, the function name here should probably be wp_style_add_data.

Suffice to say, I'm rather surprised that we don't already have the helper function, even more surprised that we used the exact same global access method in Twenty Twelve.

obenland10 months ago

comment:11 obenland10 months ago

Updated patch with dd32's feedback. It also applies cleanly now.

comment:12 DrewAPicture10 months ago

  • Cc xoodrew@… added

+1 for adding the wrapper(s).

comment:13 obenland9 months ago

  • Milestone changed from Awaiting Review to 3.6

Nacin said yesterday it may not be to late for this - I'll move it in 3.6 for commit or punt decision.
I'm available for any alterations or additional patches that are need.

obenland9 months ago

nacin9 months ago

comment:14 nacin9 months ago

  • Keywords commit 2nd-opinion added

18753.2.diff is good to go. Per IRC, leaving Twenty Twelve out of this. It's just more code, not really needed. If it were legitimately easier, I'd argue sure. but it's a function wrapping a method, not even a function rename or something.

comment:15 nacin9 months ago

Pointing out that it avoids the did_action() and new WP_Styles stuff because this is a new function. The original functions simply set up WP_Styles on demand, at any time — we needed to dress it up when we wanted to add_doing_it_wrong() and such. But if this new function gets called too early, it'll simply trigger a fatal error.

comment:16 ryan9 months ago

  • Owner set to ryan
  • Resolution set to fixed
  • Status changed from new to closed

In 24740:

Introduce wp_style_add_data() as a wrapper for $wp_styles->add_data().

Props obenland, nacin
fixes #18753

comment:17 DrewAPicture9 months ago

  • Keywords needs-codex added
Note: See TracTickets for help on using tickets.