WordPress函数:is_wp_error 判断是否存在错误

江河/ 2023年08月10日/ WordPress/ 浏览 682

在 WordPress 中好多函数在失败时都会返回 WP_Error,因此,在 WordPress 中最常用的检查函数是否成功的方法之一就是:is_wp_error。这个函数就是判断传入的参数是不是 WP_Error。


函数原型:


is_wp_error( mixed $thing ): bool


函数源码:


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

	if ( $is_wp_error ) {
		do_action( 'is_wp_error_instance', $thing );
	}

	return $is_wp_error;
}


包含钩子:


do_action( 'is_wp_error_instance', WP_Error $thing )


使用举例:


if ( is_wp_error( $result ) ) {
	$error_string = $result->get_error_message();
	echo '<div id="message" class="error"><p>' . $error_string . '</p></div>';
}


发表评论

暂无评论,抢个沙发...

客服 工单