Make WordPress Core

Opened 9 years ago

Closed 9 years ago

Last modified 9 years ago

#33471 closed defect (bug) (invalid)

wp_enqueue_style() doesn't register it in parallel wp_enqueue_scripts

Reported by: looimaster's profile Looimaster Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.3
Component: General Keywords:
Focuses: Cc:


Possibly a bug:

function my_scripts2() {

	//global $wp_styles;

	var_dump(wp_style_is( 'my-style', 'enqueued' )); // true
	var_dump(wp_style_is( 'my-style', 'registered' )); // false (probably wrong)
	var_dump(wp_style_is( 'my-style', 'to_do' )); // false
	var_dump(wp_style_is( 'my-style', 'done' )); // false

	wp_add_inline_style( 'my-style', $some_css ); // This fails because of this issue.
add_action( 'wp_enqueue_scripts', 'my_scripts2' );

function my_scripts() {

	// Loads our main stylesheet.
	wp_enqueue_style( 'my-style', get_stylesheet_uri() );

	var_dump(wp_style_is( 'my-style', 'enqueued' )); // true
	var_dump(wp_style_is( 'my-style', 'registered' )); // true
	var_dump(wp_style_is( 'my-style', 'to_do' )); // false
	var_dump(wp_style_is( 'my-style', 'done' )); // false
add_action( 'wp_enqueue_scripts', 'my_scripts' );

If I reverse add_action(...) lines then both show true/true/false/false.

Change History (6)

#1 @johnbillion
9 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed

This is because wp_add_inline_style( 'my-style'...) is being called before wp_enqueue_style( 'my-style'...).

I don't think there's a bug here. You're trying to add inline data to an asset that isn't registered.

#2 @Looimaster
9 years ago

  • Resolution invalid deleted
  • Status changed from closed to reopened

What I was trying to say is that the script is "enqueued" but not "registered". Shouldn't it always be "registered" when it's already "enqueued"?

I think it shouldn't be possible to enqueue something that isn't registered. My understanding is that the enqueuing function should first register a script and then enqueue it.

#3 @Frank Klein
9 years ago

  • Resolution set to invalid
  • Status changed from reopened to closed

Enqueued scripts are indeed registered. But in the sample code provided, the call to wp_add_inline_style() happens in a function that is hooked before the function enqueuing the script that you want to add the inline styles to.

This means that the inline styles can't be added, because at the moment at which you want to add them, the script handle doesn't exist yet, because that function is called later.

If you use priorities to ensure that my_scripts() is called before my_scripts2(), then the code will work.

#4 @Looimaster
9 years ago

I understand why inline styles can't be added.

I'll leave it as invalid but still I don't understand the following: "[...] the script handle doesn't exist yet, because that function is called later. [...]". If the handle doesn't exist then why it has status of ENQUEUED (true) in the first function?

I would expect to get only the following outcomes: false/false/false/false (not registered, not enqueued), false/true/false/false (regsitered but not enqueued), true/true/false/false (regsitered and enqueued). I don't ever expect to get true/false/false/false (enqueued but not registered) and yet I get this result.

#5 @TobiasBg
9 years ago

From what I can see in the code of wp_enqueue_style(), the "registered=false, but enqueued=true" state can only happen if the $src parameter were false. In your example, that would mean that get_stylesheet_uri() returns false. Could that be the case?

#6 @Looimaster
9 years ago

I use get_stylesheet_uri() in functions.php (it's even accompanied by the same "Loads our main stylesheet." comment like in Twenty Fifteen theme).

The other function I'm using in /inc/customizer.php which was for a short while included at the top of functions.php, before the place where I enqueue style.css.

get_stylesheet_uri() in both functions returns the following: string '' (length=55)

I copied the first function to my functions.php again and the outcome is still:

boolean true (enqueued)
boolean false (registered)
boolean false (to_do)
boolean false (done)

When I uncomment the following in the first function:

global $wp_styles;

then interestingly "my-style" is neither registered nor enqueued (like expected).

Note: See TracTickets for help on using tickets.