The get_block_theme_folders() WordPress PHP function ensures backward compatibility by properly falling back to block-templates or block-template-parts folders when needed.
Usage
Here’s a generic example showing how to use the function:
$block_theme_folders = get_block_theme_folders($theme_stylesheet);
Custom example:
$block_theme_folders = get_block_theme_folders('my-custom-theme');
Parameters
$theme_stylesheet
(string) (Optional) The stylesheet used for the theme. Default is to leverage the main theme root. Default:null
More information
See WordPress Developer Resources: get_block_theme_folders()
Examples
Getting block theme folders for the active theme
This example retrieves the block theme folders for the currently active theme:
$active_theme = wp_get_theme(); $block_theme_folders = get_block_theme_folders($active_theme->get_stylesheet());
Using the function in a plugin
This example shows how to use the function within a plugin to retrieve the block theme folders:
function my_plugin_get_block_theme_folders() { $theme_stylesheet = 'my-plugin-theme'; return get_block_theme_folders($theme_stylesheet); }
Customizing block theme folder paths
This example demonstrates customizing the block theme folder paths by filtering the output of the function:
function my_custom_block_theme_folders($folders) { array_push($folders, 'my-custom-folder'); return $folders; } add_filter('block_theme_folders', 'my_custom_block_theme_folders');
Printing block theme folders
This example prints the block theme folders to the browser:
$block_theme_folders = get_block_theme_folders(); echo 'Block theme folders: ' . implode(', ', $block_theme_folders);
Verifying if a specific folder exists
This example checks if a specific folder (e.g., ‘block-templates’) exists within the block theme folders:
$block_theme_folders = get_block_theme_folders(); $specific_folder = 'block-templates'; if (in_array($specific_folder, $block_theme_folders)) { echo "The '{$specific_folder}' folder exists."; } else { echo "The '{$specific_folder}' folder does not exist."; }