Make WordPress Core

Opened 9 months ago

Closed 9 months ago

Last modified 9 months ago

#57864 closed enhancement (fixed)

Defer register inline script in WP_Widget

Reported by: spacedmonkey's profile spacedmonkey Owned by: spacedmonkey's profile spacedmonkey
Milestone: 6.3 Priority: normal
Severity: normal Version: 4.8.1
Component: Widgets Keywords: has-patch
Focuses: performance Cc:


In the widget classes, WP_Widget_Custom_HTML and WP_Widget_Text, the method _register_one is called. This method registered a inline script like this.

wp_add_inline_script( 'text-widgets', sprintf( 'wp.textWidgets.idBases.push( %s );', wp_json_encode( $this->id_base ) ) );
wp_add_inline_script( 'custom-html-widgets', sprintf( 'wp.customHtmlWidgets.idBases.push( %s );', wp_json_encode( $this->id_base ) ) );

This results in a script registering. However, this inline script is not used, unless script is loaded in admin_print_scripts-widgets.php hook.

This inline script being registered like this, means that when the widget is registered, this inline script is registered as well. In my use page types, this inline script serves no purpose and hiders performance.

Attachments (2)

Screenshot 2023-03-03 at 17.34.10.png (455.0 KB) - added by spacedmonkey 9 months ago.
Before, see red square
Screenshot 2023-03-03 at 17.33.27.png (526.0 KB) - added by spacedmonkey 9 months ago.

Download all attachments as: .zip

Change History (11)

9 months ago

Before, see red square

This ticket was mentioned in PR #4170 on WordPress/wordpress-develop by @spacedmonkey.

9 months ago

  • Keywords has-patch added

#2 @sakibmd
9 months ago

PR looks good @spacedmonkey and thanks for figuring it out.

#3 @spacedmonkey
9 months ago

  • Milestone changed from Awaiting Review to 6.3

@spacedmonkey commented on PR #4170:

9 months ago

@felixarntz Actioned feedback and these and seeing performance improves still.

#5 @spacedmonkey
9 months ago

  • Owner set to spacedmonkey
  • Status changed from new to assigned

#6 @spacedmonkey
9 months ago

  • Resolution set to fixed
  • Status changed from assigned to closed

In 55553:

Widgets: Defer register inline script in WP_Widget_Custom_HTML and WP_Widget_Text.

In [41376] an inline script was added to push idBases for the custom html and text widgets. However, this script is not used unless the widget script is output in the widget screen / customizer. Deferring registering this script until it is needed, results in a faster server response times.

Props spacedmonkey, sakibmd, flixos90, westonruter.
Fixes #57864.

#7 @spacedmonkey
9 months ago

In 55554:

Build/Test Tools: Revert [55553].

Revert package-lock.json changes in [55553].

See #57864.

@spacedmonkey commented on PR #4170:

9 months ago


#9 @flixos90
9 months ago

As part of investigating #57916, I found that this enhancement is particularly beneficial for widgets_init performance, and it also improves overall performance slightly, which is great to see. Details in

I don't think it should be backported since it doesn't fix any 6.2 regression, but rather is a new performance enhancement, and the win is also not large enough to justify backporting. But it's great that you caught this, it is indeed somehow indirectly contributing to the WP 6.2 slowdown of widgets_init.

Note: See TracTickets for help on using tickets.