Opened 20 months ago

Last modified 3 weeks ago

#18753 new enhancement

Add data to stylesheets

Reported by: kobenland Owned by:
Priority: normal Milestone: Awaiting Review
Component: Template Version:
Severity: normal Keywords: has-patch 2nd-opinion
Cc: Ken@…, philip@…

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 (1)

functions.wp-styles.php.patch (1.8 KB) - added by kobenland 20 months ago.
Patch

Download all attachments as: .zip

Change History (9)

Patch

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.

  • Keywords 2nd-opinion added

I concur.

  • Cc Ken@… added

This is really nice.

Maybe a 3.5 candidate.

comment:6 follow-up: ↓ 7   ryanve7 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 7 months ago by ryanve (previous) (diff)

comment:7 in reply to: ↑ 6   obenland7 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?

  • Cc philip@… added
Note: See TracTickets for help on using tickets.