Email Alert When the Theme Is Changed
Emails the site admin whenever someone switches the active theme, with who did it and the old and new theme, so unexpected design changes are caught immediately.
Opens WPIDE on
Code
add_action( 'switch_theme', function ( $new_name, $new_theme, $old_theme ) {
$user = wp_get_current_user();
wp_mail(
get_option( 'admin_email' ),
sprintf( '[%s] Theme changed to %s', wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ), $new_name ),
sprintf(
"The active theme was changed from \"%s\" to \"%s\" by %s on %s.\n\nThemes: %s",
$old_theme instanceof WP_Theme ? $old_theme->get( 'Name' ) : 'unknown',
$new_name,
$user->exists() ? $user->user_login : 'an automated process',
wp_date( 'Y-m-d H:i' ),
admin_url( 'themes.php' )
)
);
}, 10, 3 );
Notes
Theme updates do not trigger it, only switching to a different theme.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.