[wp-trac] [WordPress Trac] #59732: Theme live preview is broken
WordPress Trac
noreply at wordpress.org
Wed Oct 25 13:44:53 UTC 2023
#59732: Theme live preview is broken
--------------------------+------------------------------
Reporter: karl94 | Owner: (none)
Type: defect (bug) | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Themes | Version: trunk
Severity: normal | Resolution:
Keywords: | Focuses:
--------------------------+------------------------------
Comment (by karl94):
@flixos90 can you give a look at this?
It appears that WP is invoking `get_stylesheet_directory()` before that
`WP_Customize_Manager` attaches its filters for previewing the theme.
`WP_Customize_Manager` [https://github.com/WordPress/wordpress-
develop/blob/9352b2a/src/wp-includes/class-wp-customize-
manager.php#L683-L693 adds its preview filters] in
`WP_Customize_Manager::start_previewing_theme()`, which is called from
`WP_Customize_Manager::setup_theme()`, which [https://github.com/WordPress
/wordpress-develop/blob/9352b2a/src/wp-includes/class-wp-customize-
manager.php#L369 is run on action] `setup_theme` at default priority (10).
The first invocation of `get_stylesheet_directory()` appears to be from
`wp_theme_has_theme_json()` (https://github.com/WordPress/wordpress-
develop/blob/9352b2a/src/wp-includes/global-styles-and-settings.php#L399)
which is called by `wp_enable_block_templates()`
(https://github.com/WordPress/wordpress-develop/blob/9352b2a/src/wp-
includes/theme-templates.php#L227) yet again during the hook `setup_theme`
at priority 10 (https://github.com/WordPress/wordpress-
develop/blob/9352b2a/src/wp-includes/default-filters.php#L721).
So, both `wp_enable_block_templates()` and
`WP_Customize_Manager::setup_theme()` are attached to the same hook at the
same priority, therefore they get invoked in the same order they were
attached to the hook.
`default-filters.php` is executed before, then the callback order is:
first `wp_enable_block_templates()`, then
`WP_Customize_Manager::setup_theme()`.
Because `get_stylesheet_directory()` is used inside
`wp_enable_block_templates()`, the global variable `$wp_stylesheet_path`
is set, caching `$stylesheet_dir`. Any following
`get_stylesheet_directory()` call will always return the same value.
The [https://github.com/WordPress/wordpress-develop/blob/9352b2a/src/wp-
includes/theme.php#L215-L216 cache-bypass logic] doesn't help in this case
since when `get_stylesheet_directory()` is first invoked (via
`wp_enable_block_templates()`) the customizer hasn't added its filter yet.
`get_template_directory()` has the same issue.
Maybe a `doing_it_wrong()` should be added to both
`get_stylesheet_directory()` and `get_template_directory()` to warn of
every usage before or during the hook `setup_theme`? And bypass their
cache until that hook completes?
--
Ticket URL: <https://core.trac.wordpress.org/ticket/59732#comment:1>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list