Halloween Sale 15% off Ends in See the plans
PHP

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.

Don’t have WPIDE yet? Install it on your site

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

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.