WordPress拥有WP_Error
类,用于检查自版本2.1.0以来的WordPress错误和错误消息。WordPress使用对象WP_Error
用于报告多个WP函数的错误的类。然而,我们可以在插件或主题中使用此对象来处理WordPress中的错误。此类包含用于管理错误的非常有用的方法。
所有方法
<?php
//Creating instance of error class
$error = new WP_Error( \'not_found\', \'Page Not Found\', \'Page Data\' );
//Add new error to object
$error->add( \'not_match\', \'Field Not Match\' );
//Return all error codes from object
$data = $error->get_error_codes();
print_r( $data );
//Output: Array ( [0] => not_found [1] => not_match )
//Return first error code
echo $error->get_error_code();
//Output: not_found
//Return all error message
$data = $error->get_error_messages();
print_r( $data );
//Output: Array ( [0] => Page Not Found [1] => Field Not Match )
//Return error message by error code
$data = $error->get_error_messages( \'not_match\' );
print_r( $data );
//Output: Array ( [0] => Field Not Match )
//Return first error message if no code are given
echo $error->get_error_message();
//Output: Page Not Found
//Return first error message for given error code
echo $error->get_error_message( \'not_match\' );
//Output: Field Not Match
//Return first error data
echo $error->get_error_data();
//Output: Page Data
//Return error data from error code.
echo $error->get_error_data( \'not_found\' );
//Output: Page Data
//add error data to error code
//syntex: add_data( $data, $code );
$error->add_data( \'Some data\', \'not_match\' );
echo $error->get_error_data( \'not_match\' );
//Output: Some data
//Check whether variable is a WordPress Error.
//return bool True, if WP_Error. False, if not WP_Error.
$data = is_wp_error( $error );
var_dump( $data );
//Output: bool(true)
来源和提示:更多背景信息
this post