嗨,
首先,检查结果是否为WP_Error
对象是否:
$id = wp_insert_post(...);
if (is_wp_error($id)) {
$errors = $id->get_error_messages();
foreach ($errors as $error) {
echo $error; //this is just an example and generally not a good idea, you should implement means of processing the errors further down the track and using WP\'s error/message hooks to display them
}
}
这是通常的方式。
但是WP\\u Error对象可以实例化,而不会发生任何错误,只是作为一般错误存储以防万一。如果要执行此操作,可以使用检查是否有任何错误get_error_code()
:
function my_func() {
$errors = new WP_Error();
... //we do some stuff
if (....) $errors->add(\'1\', \'My custom error\'); //under some condition we store an error
.... //we do some more stuff
if (...) $errors->add(\'5\', \'My other custom error\'); //under some condition we store another error
.... //and we do more stuff
if ($errors->get_error_code()) return $errors; //the following code is vital, so before continuing we need to check if there\'s been errors...if so, return the error object
.... // do vital stuff
return $my_func_result; // return the real result
}
如果这样做,则可以检查返回错误的进程,就像
wp_insert_post()
上述示例。
课程是documented on the Codex.<还有a little article here.