WordPress.org

Make WordPress Core

Opened 23 months ago

Closed 12 months ago

Last modified 9 months ago

#20836 closed defect (bug) (fixed)

wp_add_inline_style does not work with concatenated stylesheets

Reported by: stephenh1988 Owned by: 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 23 months ago.
Patch for adding in-line styles to concatenated scripts

Download all attachments as: .zip

Change History (11)

comment:1 stephenh198823 months 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.

comment:2 scribu23 months ago

  • Cc scribu added

comment:3 F J Kaiser23 months ago

  • Cc 24-7@… added

stephenh198823 months ago

Patch for adding in-line styles to concatenated scripts

comment:4 toscho18 months ago

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

comment:5 toscho15 months ago

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

comment:6 scribu15 months 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
  2. The script-loader.php file doesn't have any wp_add_inline_style() calls

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?

Last edited 15 months ago by scribu (previous) (diff)

comment:7 stephenh198815 months 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.

comment:8 scribu15 months 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 15 months ago by scribu (previous) (diff)

comment:9 nacin12 months 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.

comment:10 SergeyBiryukov9 months ago

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