Email Alert When Key Site Settings Change
Emails the site admin when important settings change, such as the site address, admin email, open registration or the default role for new users, a common sign of a hacked site.
Opens WPIDE on
Code
// Option name => label.
$wpide_watched_options = array(
'siteurl' => 'WordPress address',
'home' => 'Site address',
'admin_email' => 'Admin email',
'users_can_register' => 'Anyone can register',
'default_role' => 'New user default role',
'blog_public' => 'Search engine visibility',
);
add_action( 'updated_option', function ( $option, $old, $new ) use ( $wpide_watched_options ) {
if ( ! isset( $wpide_watched_options[ $option ] ) || $old === $new ) {
return;
}
$user = wp_get_current_user();
$to = 'admin_email' === $option ? array_filter( array( (string) $old, (string) $new ), 'is_email' ) : get_option( 'admin_email' );
wp_mail(
$to,
sprintf( '[%s] Setting changed: %s', wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ), $wpide_watched_options[ $option ] ),
sprintf(
"\"%s\" was changed by %s on %s.\n\nOld value: %s\nNew value: %s",
$wpide_watched_options[ $option ],
$user->exists() ? $user->user_login : 'an automated process or WP-CLI',
wp_date( 'Y-m-d H:i' ),
is_scalar( $old ) ? (string) $old : wp_json_encode( $old ),
is_scalar( $new ) ? (string) $new : wp_json_encode( $new )
)
);
}, 10, 3 );
Notes
Edit the watched options at the top; each change is reported with the old and new value and the user who made it.
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.