Skip to content

Conversation

@okmttdhr
Copy link

@okmttdhr okmttdhr commented Sep 11, 2023

This PR adds a _doing_it_wrong notice if a theme is requested too early when previewing a theme.

Trac ticket: https://core.trac.wordpress.org/ticket/59000
Related: WordPress/gutenberg#53284

Why

This is a follow-up for #5107.

The approach in #5107, “running the filters later”, works in most cases. The one exception is the case when the site has a plugin that runs stylesheet or templates before pluggable.php (which means a plugin is loaded before plugin_loaded), AND when the plugin wants to show the previewing theme. In this case, it gets the active theme’s stylesheet (not the previewing theme), which may lead to inconsistency.

For such a case, this PR adds a _doing_it_wrong notice if a theme is requested too early when previewing a theme.

Testing

  • Add a file in wp-content/plugins/sample-plugin/sample-plugin.php on your site.
<?php
/*
Plugin Name: Sample plugin
*/

$wp_theme = wp_get_theme();
$wp_theme->get_stylesheet();
  • Navigate to /wp-admin/themes.php and click the Live Preview button on any Block theme (e.g. Twenty Twenty-Two).
  • See a notice in your debug.php: Function get_stylesheet() was called <strong>incorrectly</strong>. Calling get_stylesheet() before pluggable.php has loaded can result in getting the current theme instead of the previewed theme. Please ensure that you are using these functions after the plugins_loaded action has fired if this is not intended.

This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

@okmttdhr okmttdhr force-pushed the add/theme_preview_maybe_doing_it_wrong branch 5 times, most recently from 64b64d0 to 5b9f0eb Compare September 11, 2023 07:12
@okmttdhr okmttdhr force-pushed the add/theme_preview_maybe_doing_it_wrong branch from 5b9f0eb to ed39535 Compare September 11, 2023 07:18
@okmttdhr okmttdhr marked this pull request as ready for review September 11, 2023 07:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant