Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a notice if a theme is requested too early when previewing a theme #5182

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from

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
1 participant