Make WordPress Core


Ignore:
Timestamp:
12/07/2021 05:44:46 PM (3 years ago)
Author:
audrasjb
Message:

Script Loader: Allow for wp_register_script() to be called after wp_enqueue_script().

When a plugin registers styles/scripts on wp_enqueue_scripts (as plugin authors are encouraged to do), and conditionally enqueues their script/style on the_content filter, things "just work". In block themes, the_content is run prior to the header being processed, which results in the above scenario failing.

This change makes a wp_enqueue_script( 'example' ); wp_register_script( 'example' ); work, where as currently the enqueue silently fails (no "doing it wrong" message) and the following register has no impact. Scripts can therefore be enqueued and dequeued (by "handle") before they are registered.

Fixes #54529.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/dependencies.php

    r52010 r52338  
    137137
    138138    }
     139
     140    function test_enqueue_before_register() {
     141        $dep = new WP_Dependencies;
     142
     143        $this->assertArrayNotHasKey( 'one', $dep->registered );
     144
     145        $dep->enqueue( 'one' );
     146
     147        $this->assertNotContains( 'one', $dep->queue );
     148
     149        $this->assertTrue( $dep->add( 'one', '' ) );
     150
     151        $this->assertContains( 'one', $dep->queue );
     152    }
    139153}
Note: See TracChangeset for help on using the changeset viewer.