Skip to content

Block Theme Previews does not work if we call the get_template or get_stylesheet functions early #53284

@okmttdhr

Description

@okmttdhr

Description

Block Theme Previews does not work if we call the get_template or get_stylesheet functions before the pluggable functions are not ready. So, if a 3rd party plugin (e.g. CoBlocks, Elementor, Classic Editor, and WooCommerce) needs to access the template or stylesheet before the pluggable functions are ready, then the site with such a plugin will break (see the reproduction instruction below).

The reason this error happens is;

A possible solution would be one of the following;

  • Load the pluggable functions early
  • Load theme-previews.php after the pluggable functions
  • Load the pluggable functions on demand in theme-previews.php
  • Tell developers for the 3rd party plugins not to access the template or stylesheet directly when loading plugins (a developer guide should be covered, maybe)
  • Something else

Step-by-step reproduction instructions

  1. 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();
  1. Go to /wp-admin/themes.php and click Live Preview button on your installed Block theme.
  2. See the error: Uncaught Error: Call to undefined function wp_get_current_user()

Screenshots, screen recording, code snippet

Screen Shot 2023-08-03 at 16 25 19

Environment info

  • WordPress: 6.3-RC3-56344
  • Gutenberg: 16.3.0

Please confirm that you have searched existing issues in the repo.

Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

Yes

Metadata

Metadata

Assignees

Labels

[Type] BugAn existing feature does not function as intended

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions