Using WordPress ‘pre_wp_update_https_detection_errors’ PHP filter

 ‘pre_wp_update_https_detection_errors’ is a WordPress PHP filter that allows you to short-circuit the default process of detecting errors related to HTTPS support. By returning a WP_Error from this filter, you can bypass the default logic and store the errors array from the returned WP_Error instead.

Usage

To use this filter, add a custom function to your theme’s functions.php file or your plugin file. Then, hook the function to the ‘pre_wp_update_https_detection_errors’ filter. Here’s a simple code example:

function my_custom_https_detection_errors( $pre ) {
    // Your custom logic here
    return $pre;
}
add_filter( 'pre_wp_update_https_detection_errors', 'my_custom_https_detection_errors' );

Parameters

  • $pre (null|WP_Error): Error object to short-circuit detection, or null to continue with the default behavior.

Examples

Bypass HTTPS error detection

function bypass_https_error_detection( $pre ) {
    return new WP_Error( 'bypassed', 'Bypassing HTTPS error detection.' );
}
add_filter( 'pre_wp_update_https_detection_errors', 'bypass_https_error_detection' );

This code will bypass the default HTTPS error detection by returning a WP_Error object with the message “Bypassing HTTPS error detection.”

Custom error message

function custom_https_error_message( $pre ) {
    if ( ! is_ssl() ) {
        return new WP_Error( 'no_ssl', 'This site does not have SSL enabled.' );
    }
    return $pre;
}
add_filter( 'pre_wp_update_https_detection_errors', 'custom_https_error_message' );

In this example, we check if the site is running on SSL. If not, we return a custom WP_Error message, “This site does not have SSL enabled.”

Allow specific errors

function allow_specific_https_errors( $pre ) {
    if ( is_wp_error( $pre ) ) {
        $allowed_errors = array( 'error_code_1', 'error_code_2' );
        $error_codes = $pre->get_error_codes();

        foreach ( $error_codes as $code ) {
            if ( in_array( $code, $allowed_errors ) ) {
                $pre->remove( $code );
            }
        }
    }
    return $pre;
}
add_filter( 'pre_wp_update_https_detection_errors', 'allow_specific_https_errors' );

This code allows specific HTTPS errors to be ignored by removing them from the WP_Error object.

Log errors to a custom file

function log_https_errors_to_file( $pre ) {
    if ( is_wp_error( $pre ) ) {
        $log_file = WP_CONTENT_DIR . '/https_errors.log';
        $error_data = $pre->get_error_messages();

        file_put_contents( $log_file, implode( "n", $error_data ), FILE_APPEND );
    }
    return $pre;
}
add_filter( 'pre_wp_update_https_detection_errors', 'log_https_errors_to_file' );

This code will log any detected HTTPS errors to a custom log file in the wp-content folder.

Send email notification for errors

function email_https_errors( $pre ) {
    if ( is_wp_error( $pre ) ) {
        $to = '[email protected]';
        $subject = 'HTTPS Errors Detected';
        $message = implode( "n", $pre->get_error_messages() );

        wp_mail( $to, $subject, $message );
}
return $pre;
}
add_filter( 'pre_wp_update_https_detection_errors', 'email_https_errors' );

This code sends an email notification to the specified email address when HTTPS errors are detected. It includes the error messages in the email content.