Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using wp_set_option_autoload() for theme options #5692

Closed
wants to merge 9 commits into from
7 changes: 7 additions & 0 deletions src/wp-includes/theme.php
Expand Up @@ -866,6 +866,13 @@ function switch_theme( $stylesheet ) {

update_option( 'theme_switched', $old_theme->get_stylesheet() );

// Set autoload=no for the previous theme
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing period after inline comment.

Suggested change
// Set autoload=no for the previous theme
// Set autoload=no for the previous theme.

$old_theme_stylesheet = $old_theme->get_stylesheet();
wp_set_option_autoload( "theme_mods_$old_theme_stylesheet", 'no' );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This adjustment alters the autoload option for the previous theme. If the user had activated multiple themes previously, it becomes essential to verify and modify the autoload value for those themes as well. What do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mukeshpanchal27 I think this is only an issue for existing sites that had multiple themes active before updating to WordPress 6.5. So I think this is better addressed separately via your new ticket https://core.trac.wordpress.org/ticket/59975 and #5711.

Any site that starts with WordPress 6.5 should not have this problem as every time the theme is switched the previous theme's theme mods are set to not autoload. So there should at no point be more than one theme with autoloaded theme mods.

Comment on lines +870 to +871
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to also cover a potential parent theme here (i.e. if the old template is different from the old stylesheet). We could then use wp_set_options_autoload() to reduce the database requests needed.

Suggested change
$old_theme_stylesheet = $old_theme->get_stylesheet();
wp_set_option_autoload( "theme_mods_$old_theme_stylesheet", 'no' );
$old_theme_mods = array_unique(
array(
"theme_mods_{$old_theme->get_stylesheet()}",
"theme_mods_{$old_theme->get_template()}"
)
);
wp_set_options_autoload( $old_theme_mods, 'no' );


// Set autoload=yes for the switched theme if not already set
wp_set_option_autoload( "theme_mods_$stylesheet", 'yes' );
Comment on lines +873 to +874
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here:

Suggested change
// Set autoload=yes for the switched theme if not already set
wp_set_option_autoload( "theme_mods_$stylesheet", 'yes' );
// Set autoload=yes for the switched to theme if not already set.
$new_theme_mods = array_unique(
array(
"theme_mods_{$stylesheet}",
"theme_mods_{$template}"
)
);
wp_set_options_autoload( $new_theme_mods, 'yes' );


/*
* Reset globals to force refresh the next time these directories are
* accessed via `get_stylesheet_directory()` / `get_template_directory()`.
Expand Down