is_wp_error( mixed $thing ): bool

Checks whether the given variable is a WordPress Error.

Description

Returns whether $thing is an instance of the WP_Error class.

Parameters

$thingmixedrequired
The variable to check.

Return

bool Whether the variable is an instance of WP_Error.

Source

function is_wp_error( $thing ) {
	$is_wp_error = ( $thing instanceof WP_Error );

	if ( $is_wp_error ) {
		/**
		 * Fires when `is_wp_error()` is called and its parameter is an instance of `WP_Error`.
		 *
		 * @since 5.6.0
		 *
		 * @param WP_Error $thing The error object passed to `is_wp_error()`.
		 */
		do_action( 'is_wp_error_instance', $thing );
	}

	return $is_wp_error;
}

Hooks

do_action( ‘is_wp_error_instance’, WP_Error $thing )

Fires when is_wp_error() is called and its parameter is an instance of WP_Error.

Changelog

VersionDescription
2.1.0Introduced.

User Contributed Notes

  1. Skip to note 5 content

    example with wp_insert_user

    $email = 'test@test.com';
    $userdata = array(
    	'user_login'  	=> $email,
    	'user_email'    => $email,
    );
    
    $user_id = wp_insert_user( $userdata );
    
    if ( is_wp_error( $user_id ) ) {
    	$error_code = array_key_first( $user_id->errors );
    	$error_message = $user_id->errors[$error_code][0];
    }

You must log in before being able to contribute a note or feedback.