Halloween Sale 15% off Ends in See the plans
PHP

Daily Core File Integrity Check

Compares your WordPress core files with the official checksums once a day and emails the administrator if any file was changed or is missing.

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

Code

add_action( 'init', function () {
    if ( ! wp_next_scheduled( 'wpide_core_integrity_check' ) ) {
        wp_schedule_event( time() + HOUR_IN_SECONDS, 'daily', 'wpide_core_integrity_check' );
    }
} );

add_action( 'wpide_core_integrity_check', function () {
    global $wp_version, $wp_local_package;

    require_once ABSPATH . 'wp-admin/includes/update.php';

    $checksums = get_core_checksums( $wp_version, isset( $wp_local_package ) ? $wp_local_package : 'en_US' );

    if ( ! is_array( $checksums ) ) {
        return; // The checksum service could not be reached; try again tomorrow.
    }

    $problems = array();
    $optional = array( 'readme.html', 'license.txt', 'wp-config-sample.php', 'xmlrpc.php' ); // Often removed on purpose.

    foreach ( $checksums as $file => $md5 ) {
        if ( 0 === strpos( $file, 'wp-content/' ) || ( in_array( $file, $optional, true ) && ! file_exists( ABSPATH . $file ) ) ) {
            continue;
        }

        $path = ABSPATH . $file;

        if ( ! file_exists( $path ) ) {
            $problems[] = 'Missing: ' . $file;
        } elseif ( md5_file( $path ) !== $md5 ) {
            $problems[] = 'Modified: ' . $file;
        }
    }

    if ( $problems ) {
        wp_mail(
            get_option( 'admin_email' ),
            sprintf( '[%s] WordPress core files changed', wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ) ),
            "These core files differ from the official WordPress " . $wp_version . " release:\n\n" . implode( "\n", array_slice( $problems, 0, 100 ) ) . "\n\nIf you did not change them, reinstall WordPress from Dashboard > Updates and scan the site."
        );
    }
} );

Notes

Contacts api.wordpress.org once a day, and the scheduled event stays registered but idle after deactivation.

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.