Make WordPress Core

Opened 13 years ago

Closed 11 years ago

Last modified 11 years ago

#18753 closed enhancement (fixed)

Add data to stylesheets

Reported by: kobenland's profile kobenland Owned by: ryan's profile 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 13 years ago.
Patch
18753.twentythirteen.diff (910 bytes) - added by obenland 11 years ago.
This is how Twenty Thirteen could benefit from it.
18753.diff (1.5 KB) - added by obenland 11 years ago.
18753.1.diff (1.1 KB) - added by obenland 11 years ago.
18753.twentythirteen.1.diff (910 bytes) - added by obenland 11 years ago.
18753.twentytwelve.diff (1.0 KB) - added by obenland 11 years ago.
18753.2.diff (2.2 KB) - added by nacin 11 years ago.

Download all attachments as: .zip

Change History (24)

#1 @nacin
13 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.

#2 @kobenland
12 years ago

  • Keywords 2nd-opinion added

#3 @bitacre
12 years ago

I concur.

#4 @WraithKenny
12 years ago

  • Cc Ken@… added

#5 @westi
12 years ago

This is really nice.

Maybe a 3.5 candidate.

#6 follow-up: @ryanve
12 years 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 12 years ago by ryanve (previous) (diff)

#7 in reply to: ↑ 6 @obenland
12 years 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?

#8 @philiparthurmoore
11 years ago

  • Cc philip@… added

@obenland
11 years ago

This is how Twenty Thirteen could benefit from it.

#9 @obenland
11 years ago

Added patch for Twenty Thirteen.

#10 @dd32
11 years 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.

@obenland
11 years ago

#11 @obenland
11 years ago

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

#12 @DrewAPicture
11 years ago

  • Cc xoodrew@… added

+1 for adding the wrapper(s).

#13 @obenland
11 years 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.

@obenland
11 years ago

@nacin
11 years ago

#14 @nacin
11 years 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.

#15 @nacin
11 years 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.

#16 @ryan
11 years 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

#17 @DrewAPicture
11 years ago

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