Make WordPress Core

Opened 2 months ago

Closed 2 months ago

Last modified 2 months ago

#64241 closed defect (bug) (invalid)

wp_is_block_theme should detect block theme if the contains a theme.json file.

Reported by: ifatwp's profile ifatwp Owned by:
Milestone: Priority: normal
Severity: normal Version: 6.9
Component: Themes Keywords: has-patch needs-user-docs reporter-feedback dev-feedback close
Focuses: Cc:

Description

Previously, block theme detection checked for the existence of:

/templates/index.html

/block-templates/index.html

This update adds a new path check for theme.json located in the theme’s root folder. The presence of this file is now also used to identify block themes.

PR: https://github.com/WordPress/wordpress-develop/pull/10509

Let me know what more guidelines I need to follow since this was my first time here.

Change History (8)

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


2 months ago
#1

Trac ticket: 64241

#2 @westonruter
2 months ago

  • Keywords reporter-feedback added

#3 @ifatwp
2 months ago

  • Keywords dev-feedback added

When testing block theme detection, I initially verified that it worked correctly in most cases. However, after testing additional themes from the WordPress.org theme repository, I noticed that some themes (for example, Blocksy
) are not detected as block themes by wp_is_block_theme().

Blocksy includes a theme.json file in its root directory and provides several block-related features, yet the function still returns false.
You can check the theme’s structure and configuration here:

Theme page: https://wordpress.org/themes/blocksy/

theme.json: https://themes.trac.wordpress.org/browser/blocksy/2.1.20/theme.json?rev=297960

This suggests that the current detection logic — which only checks for /templates/index.html or /block-templates/index.html — might not be sufficient to reliably identify all block or hybrid block themes.

To improve this, we could make the is_block_theme() function more strict and extensible by incorporating additional checks such as:

The presence of a theme.json file in the root directory.

Whether the theme explicitly declares support for block-templates via add_theme_support().

Using a filter (e.g., is_block_theme) so developers can customize or extend detection logic when needed.

/**
         * Returns whether this theme is a block-based theme or not.
         *
         * @since 5.9.0
         *
         * @return bool
         */
        public function is_block_theme(): bool {
                if ( isset( $this->block_theme ) ) {
                        return $this->block_theme;
                }

                $paths_to_index_block_template = array(
                        $this->get_file_path( '/templates/index.html' ),
                        $this->get_file_path( '/block-templates/index.html' ),
                );

                $this->block_theme = false;

                foreach ( $paths_to_index_block_template as $path_to_index_block_template ) {
                        if ( is_file( $path_to_index_block_template ) && is_readable( $path_to_index_block_template ) ) {
                                $this->block_theme = true;
                                break;
                        }
                }

                 // Additional strict and extendable checks
                 $has_theme_json = is_readable( $this->get_file_path( '/theme.json' ) );
                $supports_block_templates = current_theme_supports( 'block-templates' );

              $this->block_theme = $this->block_theme || ( $has_theme_json && $supports_block_templates );

               // Allow filtering for flexibility
               $this->block_theme = apply_filters( 'is_block_theme', $this->block_theme, $this );



                return $this->block_theme;
        }


Last edited 2 months ago by ifatwp (previous) (diff)

#4 @westonruter
2 months ago

  • Keywords close added

It doesn't look like Blocksy is actually a "block theme"? There are a bunch of PHP template files in there. When I activate the theme, it doesn't use the Site Editor but rather the Customizer. This indicates it is a classic theme which is using blocks. It's probably is a hybrid theme, not a block theme.

#5 @greenshady
2 months ago

Blocksy is a classic theme with some block-era features like theme.json (i.e., a "hybrid" theme).

The most fundamental thing that a block theme requires is the index.html template. Otherwise, it's not a block theme.

The addition of a theme.json in a theme tells us nothing of whether it's a block theme.

Last edited 2 months ago by greenshady (previous) (diff)

#6 @wildworks
2 months ago

  • A theme.json is not required for block themes. Adding a theme.json check will prevent block themes without a theme.json from being considered block themes.
  • 'block-templates' theme support is also available in the classic theme.

First of all, I would like to know why and what the purpose of updating the logic for determining block themes is.

#7 @ifatwp
2 months ago

  • Resolution set to invalid
  • Status changed from new to closed

Thanks everyone for the collaboration — @westonruter, @greenshady and @wildworks !

After reviewing this further, I've understand that hybrid themes (those that include theme.json but don’t rely fully on block templates) are not intended to be detected as block themes. In that case, the current function is returning the correct result.

Appreciate all the input and clarification from everyone involved.

#8 @westonruter
2 months ago

  • Milestone Awaiting Review deleted
Note: See TracTickets for help on using tickets.