【问题标题】:Unable to show error message on form update无法在表单更新时显示错误消息
【发布时间】:2017-08-06 13:29:35
【问题描述】:

我有这个 wordpress 前端配置文件更新。用户可以更新他们的密码和电子邮件前端,但是当他们提交更新时它可以工作,但即使两个字段之间的密码不匹配,它也无法显示错误消息或输入已经存在的电子邮件。

 global $current_user, $wp_roles;

 $error = array();    
  /* If profile was saved, update profile. */
   if ( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] 
) && $_POST['action'] == 'update-user' ) {

/* Update user password. */
if ( !empty($_POST['pass1'] ) && !empty( $_POST['pass2'] ) ) {
    if ( $_POST['pass1'] == $_POST['pass2'] )
        wp_update_user( array( 'ID' => $current_user->ID, 'user_pass' => 
    esc_attr( $_POST['pass1'] ) ) );
    else
        $error[] = __('The passwords you entered do not match.  Your 
 password was not updated.', 'profile');
}

/* Update user information. */
if ( !empty( $_POST['url'] ) )
    wp_update_user( array( 'ID' => $current_user->ID, 'user_url' => esc_url( $_POST['url'] ) ) );
if ( !empty( $_POST['email'] ) ){
    if (!is_email(esc_attr( $_POST['email'] )))
        $error[] = __('The Email you entered is not valid.  please try again.', 'profile');
    elseif(email_exists(esc_attr( $_POST['email'] )) != $current_user->id )
        $error[] = __('This email is already used by another user.  try a different one.', 'profile');
    else{
        wp_update_user( array ('ID' => $current_user->ID, 'user_email' => esc_attr( $_POST['email'] )));
    }
}

if ( !empty( $_POST['first-name'] ) )
    update_user_meta( $current_user->ID, 'first_name', esc_attr( $_POST['first-name'] ) );
if ( !empty( $_POST['last-name'] ) )
    update_user_meta($current_user->ID, 'last_name', esc_attr( $_POST['last-name'] ) );
if ( !empty( $_POST['description'] ) )
    update_user_meta( $current_user->ID, 'description', esc_attr( $_POST['description'] ) );

/* Redirect so the page will show updated info.*/
if ( count($error) > 0 )  {

    do_action('edit_user_profile_update', $current_user->ID);
    wp_redirect( get_permalink() );

    exit;
}

}

为什么不显示我不确定的错误消息..

【问题讨论】:

  • 你没有回应错误

标签: php wordpress


【解决方案1】:

如果表达式验证为假,则回显您的 $error 变量。见下文。

if ( count($error) > 0 )  {

    do_action('edit_user_profile_update', $current_user->ID);
    wp_redirect( get_permalink() );

    exit;
}else{
    echo implode( "<br>", $error );
}

【讨论】:

  • 是的,我这样做了,但它仍然不会出现。为了确保文本不会被 css 元素隐藏,我也确实经历了这一点,但并没有真正输出。不知道问题出在哪里。
猜你喜欢
  • 2018-12-16
  • 2017-04-26
  • 2019-08-22
  • 2021-02-05
  • 1970-01-01
  • 2013-09-27
  • 2020-09-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多