【问题标题】:WordPress: How do I display WP_Error() errors on another page?WordPress:如何在另一个页面上显示 WP_Error() 错误?
【发布时间】:2011-11-02 07:59:35
【问题描述】:

如何在另一个页面上显示WP_Error() 错误?

functions.php,我有一个带有以下代码的表单处理器:

global $current_user, $errors; get_currentuserinfo();

$errors = new WP_Error();

if( isset( $_POST['user_email']) ) {
    if( !is_email( $_POST['user_email'] ) ) {
        $errors->add('invalid_email', __('<strong>ERROR</strong>: The email address isn&#8217;t correct.'));
    } elseif( email_exists( $_POST['user_email'] ) ) {
        $errors->add('email_exists', __('<strong>ERROR</strong>: This email is already registered, please choose another one.'));
    } elseif( $_POST['user_email'] <> $current_user->{user_email} ) {
        wp_update_user( array( 'ID' => $uid, 'user_email' => esc_attr( $_POST['user_email'] ) ) );
    }       
}
if($errors->get_error_code()) { return $errors; }

在显示表单的account.php 中,我有以下代码:

<?php
    $return = update_account();
    if(is_wp_error($return)) { echo $return->get_error_message(); }
?>

在测试过程中不会报告invalid_emailemail_exist 错误。然而,错误被阻止了。我只是不明白为什么它们没有被显示。我做错了什么?

【问题讨论】:

  • @Jerry 包括 wp-blog-header.php 既没有帮助也没有阻碍。
  • 好的,account.php 到底在哪里?您能否也发布更完整的代码版本?
  • account.php 和 functions.php 在主题目录中。提交表单时,会调用一个处理页面,其中执行 update_account() 函数并将用户返回到 account.php。

标签: php wordpress error-handling


【解决方案1】:

您需要在您的帐户页面上放置全局 $errors。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-08
    • 1970-01-01
    相关资源
    最近更新 更多