Make WordPress Core

Opened 11 years ago

Closed 10 years ago

Last modified 10 years ago

#20836 closed defect (bug) (fixed)

wp_add_inline_style does not work with concatenated stylesheets

Reported by: stephenh1988's profile stephenh1988 Owned by: nacin's profile nacin
Milestone: 3.6 Priority: normal
Severity: normal Version: 3.3.2
Component: General Keywords: has-patch commit
Focuses: Cc:

Description

When using wp_add_inline_style with concatenated stylesheets (those loaded through load-styles.php - such as 'wp-admin'), additional styles added with wp_add_inline_style are not printed to the document - but instead the word 'array' is printed.

For non-concatenated scripts wp_add_inline_style works as expected. Also when SCRIPT_DEBUG is set to true it works as expected (scripts are no-longer concatenated).

Attachments (1)

add-inline-style.patch (470 bytes) - added by stephenh1988 11 years ago.
Patch for adding in-line styles to concatenated scripts

Download all attachments as: .zip

Change History (11)

#1 @stephenh1988
11 years ago

  • Cc stephen@… added
  • Keywords has-patch added

The additional scripts are stored in an array in $styles->registered[$handle]->extra. Usually this is imploded before printing to the document however, in the case of concatenated scripts there are not.

See do_item method of the WP_Styles method. In particular line 48-57: https://github.com/WordPress/WordPress/blob/3.3.2/wp-includes/class.wp-styles.php#L48

$this->get_data( $handle, 'after' ); is appended to the code to be printed. However $this->get_data( $handle, 'after' ); either returns an array (if there is additional code) or false otherwise. This needs to be imploded before being appended to the code.

#2 @scribu
11 years ago

  • Cc scribu added

#3 @F J Kaiser
11 years ago

  • Cc 24-7@… added

@stephenh1988
11 years ago

Patch for adding in-line styles to concatenated scripts

#4 @toscho
11 years ago

  • Cc info@… added
  • Component changed from General to Appearance

#5 @toscho
10 years ago

Please set the milestone to 3.6. This is such a tiny and yet important fix.

#6 @scribu
10 years ago

  • Keywords reporter-feedback added; has-patch removed

Given:

  1. load-styles.php doesn't require() any plugins or themes; it only loads wp-includes/script-loader.php

From 1. we can deduce:

  1. Plugins and themes can't use concatenated stylesheets.

Conclusion:

Unless you modify the script-loader.php file and add a wp_add_inline_style() call to it - hacking a core file is bad, mkay - this is a non-issue.

Did I miss something?

Version 2, edited 10 years ago by scribu (previous) (next) (diff)

#7 @stephenh1988
10 years ago

Plug-ins / Themes can use wp_add_inline_style() to add in-line styles to core (or any other) styles. It should be 'attached' to (albeit not compressed, but printed after) the style-sheet you're adding it to.

Currently what is printed is not the attached styles, but instead the word "Array" is printed.

As a contrived example

wp_add_inline_style('admin-bar','#wpadminbar {background: blue;}');

Works when SCRIPT_DEBUG is true, but not if its false.

#8 @scribu
10 years ago

  • Keywords has-patch commit added; reporter-feedback removed
  • Milestone changed from Awaiting Review to 3.6

It should be 'attached' to (albeit not compressed, but printed after) the style-sheet you're adding it to.

Oh, so it wouldn't be concatenated along with the standalone files by load-styles.php, as I had assumed. Thanks, this makes sense now.

Similar discussion for JS concatenation: http://core.trac.wordpress.org/ticket/16494#comment:29 onwards

Last edited 10 years ago by scribu (previous) (diff)

#9 @nacin
10 years ago

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

In 24047:

WP Styles: Correctly print 'after' data when scripts are concatenated. props stephenh1988. fixes #20836.

#10 @SergeyBiryukov
10 years ago

  • Component changed from Appearance to General
Note: See TracTickets for help on using tickets.