Fix theme installation and activation buttons by using proper WordPress URLs and event handlers

This commit is contained in:
Marcus Quinn
2025-03-17 01:02:01 +00:00
parent e0d2904fbc
commit dabc9a3eee
2 changed files with 56 additions and 43 deletions

View File

@ -306,24 +306,34 @@ jQuery(document).ready(function($) {
e.preventDefault(); e.preventDefault();
var $button = $(this); var $button = $(this);
var slug = $button.data('slug'); var slug = $button.data('slug');
var $themeCard = $button.closest('.theme-card'); var themeName = $button.data('name') || slug;
console.log('Installing theme:', slug); console.log('Installing theme:', slug);
$button.addClass('updating-message').text('Installing...'); $button.addClass('updating-message').text('Installing...');
// Use the WordPress core update API
wp.updates.installTheme({ wp.updates.installTheme({
slug: slug, slug: slug,
success: function(response) { success: function(response) {
console.log('Theme installed successfully:', response); console.log('Theme installed successfully:', response);
$button.removeClass('updating-message').addClass('updated-message').text('Installed!'); $button.removeClass('updating-message').addClass('updated-message').text('Installed!');
setTimeout(function() { setTimeout(function() {
// Replace the button with an activate button // Replace the button with an activate button
var $parent = $button.closest('.theme-actions'); var $parent = $button.closest('.theme-actions');
$button.remove(); $button.remove();
var activateButton = $('<button type="button" class="button button-primary activate-now">Activate</button>');
activateButton.attr('data-slug', slug); // Create a proper activation link
activateButton.attr('data-nonce', wpAllstars.nonce); var activateUrl = response.activateUrl ||
$parent.prepend(activateButton); wp.updates.adminUrl + 'themes.php?action=activate&stylesheet=' + slug +
'&_wpnonce=' + wp.updates.data.nonce;
var $activateButton = $('<a class="button button-primary activate-now" href="' + activateUrl + '">Activate</a>');
$activateButton.attr('data-slug', slug);
$activateButton.attr('data-name', themeName);
$activateButton.attr('data-nonce', wpAllstars.nonce);
$parent.prepend($activateButton);
// Re-initialize the event handlers // Re-initialize the event handlers
initThemeHandlers(); initThemeHandlers();
@ -331,8 +341,8 @@ jQuery(document).ready(function($) {
}, },
error: function(response) { error: function(response) {
console.error('Theme installation failed:', response); console.error('Theme installation failed:', response);
$button.removeClass('updating-message').text('Install Now'); $button.removeClass('updating-message').text('Install');
alert(response.errorMessage); alert(response.errorMessage || 'Failed to install theme. Please try again.');
} }
}); });
}); });
@ -341,38 +351,33 @@ jQuery(document).ready(function($) {
$('.theme-actions .activate-now').on('click', function(e) { $('.theme-actions .activate-now').on('click', function(e) {
e.preventDefault(); e.preventDefault();
var $button = $(this); var $button = $(this);
var url = $button.attr('href');
var slug = $button.data('slug'); var slug = $button.data('slug');
var nonce = $button.data('nonce') || wpAllstars.nonce;
console.log('Activating theme:', slug, 'with nonce:', nonce); console.log('Activating theme:', slug, 'with URL:', url);
$button.addClass('updating-message').text('Activating...'); $button.addClass('updating-message').text('Activating...');
// Use AJAX to activate the theme // Use standard link-based activation like plugins
$.ajax({ $.ajax({
url: ajaxurl, url: url,
type: 'POST', type: 'GET',
data: { dataType: 'html',
action: 'wp_allstars_activate_theme',
theme: slug,
_wpnonce: nonce
},
success: function(response) { success: function(response) {
console.log('Theme activation response:', response); console.log('Theme activation successful');
if (response.success) { $button.removeClass('updating-message').addClass('updated-message').text('Activated!');
$button.removeClass('updating-message').addClass('updated-message').text('Activated!');
setTimeout(function() { setTimeout(function() {
// Replace the button with an active button // Replace the button with an active button
var $parent = $button.closest('.theme-actions'); var $parent = $button.closest('.theme-actions');
$button.remove(); $button.remove();
$parent.prepend('<button type="button" class="button button-disabled" disabled="disabled">Active</button>'); $parent.prepend('<button type="button" class="button button-disabled" disabled="disabled">Active</button>');
}, 1000);
} else { // Optionally reload the page to show the active theme
$button.removeClass('updating-message').text('Activate'); // window.location.reload();
alert(response.data || 'Failed to activate theme'); }, 1000);
}
}, },
error: function(jqXHR, textStatus, errorThrown) { error: function(jqXHR, textStatus, errorThrown) {
console.error('Theme activation AJAX error:', textStatus, errorThrown, jqXHR.responseText); console.error('Theme activation error:', textStatus, errorThrown);
$button.removeClass('updating-message').text('Activate'); $button.removeClass('updating-message').text('Activate');
alert('Failed to activate theme. Please try again or activate from the Themes page.'); alert('Failed to activate theme. Please try again or activate from the Themes page.');
} }

View File

@ -31,22 +31,30 @@ if (!defined('ABSPATH')) {
<?php esc_html_e('Active'); ?> <?php esc_html_e('Active'); ?>
</button> </button>
<?php elseif ($installed_theme->exists()): ?> <?php elseif ($installed_theme->exists()): ?>
<button type="button" class="button button-primary activate-now" <a href="<?php echo esc_url(wp_nonce_url(admin_url('themes.php?action=activate&stylesheet=kadence'), 'switch-theme_kadence')); ?>"
data-slug="kadence" class="button button-primary activate-now"
data-nonce="<?php echo esc_attr($nonce); ?>"> data-slug="kadence"
data-name="Kadence"
data-nonce="<?php echo esc_attr($nonce); ?>">
<?php esc_html_e('Activate'); ?> <?php esc_html_e('Activate'); ?>
</button> </a>
<script>
console.log('Theme activation button initialized with:', {
slug: 'kadence',
nonce: '<?php echo esc_js($nonce); ?>'
});
</script>
<?php else: ?> <?php else: ?>
<button type="button" class="button button-primary install-now" data-slug="kadence"> <a href="#"
class="button button-primary install-now"
data-slug="kadence"
data-name="Kadence">
<?php esc_html_e('Install'); ?> <?php esc_html_e('Install'); ?>
</button> </a>
<?php endif; ?> <?php endif; ?>
<script>
jQuery(document).ready(function($) {
// Make sure theme handlers are initialized
if (typeof initThemeHandlers === 'function') {
console.log('Initializing theme handlers from template');
initThemeHandlers();
}
});
</script>
<?php endif; ?> <?php endif; ?>
<a class="button button-secondary preview install-theme-preview" href="<?php echo esc_url($theme_data->preview_url); ?>" target="_blank"> <a class="button button-secondary preview install-theme-preview" href="<?php echo esc_url($theme_data->preview_url); ?>" target="_blank">
<?php esc_html_e('Preview'); ?> <?php esc_html_e('Preview'); ?>