get_block_theme_folders

The timeline below displays how wordpress function get_block_theme_folders has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.4

/**
 * For backward compatibility reasons,
 * block themes might be using block-templates or block-template-parts,
 * this function ensures we fallback to these folders properly.
 *
 * @since 5.9.0
 *
 * @param string $theme_stylesheet The stylesheet. Default is to leverage the main theme root.
 *
 * @return string[] {
 *     Folder names used by block themes.
 *
 *     @type string $wp_template      Theme-relative directory name for block templates.
 *     @type string $wp_template_part Theme-relative directory name for block template parts.
 * }
 */
function get_block_theme_folders($theme_stylesheet = null)
{
    $theme = wp_get_theme((string) $theme_stylesheet);
    if (!$theme->exists()) {
        // Return the default folders if the theme doesn't exist.
        return array('wp_template' => 'templates', 'wp_template_part' => 'parts');
    }
    return $theme->get_block_template_folders();
}

WordPress Version: 5.9

/**
 * For backward compatibility reasons,
 * block themes might be using block-templates or block-template-parts,
 * this function ensures we fallback to these folders properly.
 *
 * @since 5.9.0
 *
 * @param string $theme_stylesheet The stylesheet. Default is to leverage the main theme root.
 *
 * @return string[] {
 *     Folder names used by block themes.
 *
 *     @type string $wp_template      Theme-relative directory name for block templates.
 *     @type string $wp_template_part Theme-relative directory name for block template parts.
 * }
 */
function get_block_theme_folders($theme_stylesheet = null)
{
    $theme_name = (null === $theme_stylesheet) ? get_stylesheet() : $theme_stylesheet;
    $root_dir = get_theme_root($theme_name);
    $theme_dir = "{$root_dir}/{$theme_name}";
    if (file_exists($theme_dir . '/block-templates') || file_exists($theme_dir . '/block-template-parts')) {
        return array('wp_template' => 'block-templates', 'wp_template_part' => 'block-template-parts');
    }
    return array('wp_template' => 'templates', 'wp_template_part' => 'parts');
}