From f499691a3ce5e6cc8e654e104edcbcf582ef4fd1 Mon Sep 17 00:00:00 2001 From: sanketio Date: Fri, 6 Mar 2026 11:04:06 +0530 Subject: [PATCH] Themes: Show Preview button for block themes on Add Themes screen --- src/js/_enqueues/wp/updates.js | 8 ++++- src/wp-admin/includes/ajax-actions.php | 50 ++++++++++++++++++-------- src/wp-admin/theme-install.php | 4 ++- 3 files changed, 45 insertions(+), 17 deletions(-) diff --git a/src/js/_enqueues/wp/updates.js b/src/js/_enqueues/wp/updates.js index ef4b47e66093e..330579cf76944 100644 --- a/src/js/_enqueues/wp/updates.js +++ b/src/js/_enqueues/wp/updates.js @@ -1818,8 +1818,14 @@ if ( response.customizeUrl ) { - // Transform the 'Preview' button into a 'Live Preview' button. + // Transform the 'Preview' button into a 'Live Preview' or 'Preview' (block theme) button. $message.siblings( '.preview' ).replaceWith( function () { + if ( response.blockTheme ) { + return $( '' ) + .attr( 'href', response.customizeUrl ) + .addClass( 'button' ) + .text( __( 'Preview' ) ); + } return $( '' ) .attr( 'href', response.customizeUrl ) .addClass( 'button load-customize' ) diff --git a/src/wp-admin/includes/ajax-actions.php b/src/wp-admin/includes/ajax-actions.php index f52f5f5c1d80f..da952db8f4a60 100644 --- a/src/wp-admin/includes/ajax-actions.php +++ b/src/wp-admin/includes/ajax-actions.php @@ -3709,15 +3709,25 @@ function wp_ajax_query_themes() { // We only care about installed themes. $theme->block_theme = $is_theme_installed && wp_get_theme( $theme->slug )->is_block_theme(); - if ( ! is_multisite() && current_user_can( 'edit_theme_options' ) && current_user_can( 'customize' ) ) { - $customize_url = $theme->block_theme ? admin_url( 'site-editor.php' ) : wp_customize_url( $theme->slug ); + if ( ! is_multisite() && current_user_can( 'edit_theme_options' ) && ( $theme->block_theme || current_user_can( 'customize' ) ) ) { + if ( $theme->block_theme ) { + $customize_url = add_query_arg( + array( + 'wp_theme_preview' => urlencode( $theme->slug ), + 'return' => urlencode( network_admin_url( 'theme-install.php', 'relative' ) ), + ), + admin_url( 'site-editor.php' ) + ); + } else { + $customize_url = add_query_arg( + array( + 'return' => urlencode( network_admin_url( 'theme-install.php', 'relative' ) ), + ), + wp_customize_url( $theme->slug ) + ); + } - $theme->customize_url = add_query_arg( - array( - 'return' => urlencode( network_admin_url( 'theme-install.php', 'relative' ) ), - ), - $customize_url - ); + $theme->customize_url = $customize_url; } $theme->name = wp_kses( $theme->name, $themes_allowedtags ); @@ -4262,13 +4272,23 @@ function wp_ajax_install_theme() { $theme = wp_get_theme( $slug ); $status['blockTheme'] = $theme->is_block_theme(); - if ( ! is_multisite() && current_user_can( 'edit_theme_options' ) && current_user_can( 'customize' ) ) { - $status['customizeUrl'] = add_query_arg( - array( - 'return' => urlencode( network_admin_url( 'theme-install.php', 'relative' ) ), - ), - wp_customize_url( $slug ) - ); + if ( ! is_multisite() && current_user_can( 'edit_theme_options' ) && ( $status['blockTheme'] || current_user_can( 'customize' ) ) ) { + if ( $status['blockTheme'] ) { + $status['customizeUrl'] = add_query_arg( + array( + 'wp_theme_preview' => urlencode( $slug ), + 'return' => urlencode( network_admin_url( 'theme-install.php', 'relative' ) ), + ), + admin_url( 'site-editor.php' ) + ); + } else { + $status['customizeUrl'] = add_query_arg( + array( + 'return' => urlencode( network_admin_url( 'theme-install.php', 'relative' ) ), + ), + wp_customize_url( $slug ) + ); + } } /* diff --git a/src/wp-admin/theme-install.php b/src/wp-admin/theme-install.php index e9d217c0f7525..6f23039728134 100644 --- a/src/wp-admin/theme-install.php +++ b/src/wp-admin/theme-install.php @@ -415,7 +415,9 @@ <# } #> <# if ( data.customize_url ) { #> <# if ( ! data.active ) { #> - <# if ( ! data.block_theme ) { #> + <# if ( data.block_theme ) { #> + + <# } else { #> <# } #> <# } else { #>