我们如何获得成功信息

时间:2013-03-13 作者:Adi

如何通过简单地使用变量获得成功消息,就像我们可以获得错误消息一样$error . 所以还有其他变量$success. 目前我正在使用此代码注册新用户。所以注册后,我想显示成功消息。

if ( \'POST\' == $_SERVER[\'REQUEST_METHOD\'] && !empty( $_POST[\'action\'] ) && $_POST[\'action\'] == \'adduser\' ) {
    $user_pass = wp_generate_password();
    $userdata = array(
        \'user_login\' => esc_attr( $_POST[\'user_name\'] ),
        \'user_pass\' => esc_attr( $_POST[\'pass1\'] ),
        \'user_email\' => esc_attr( $_POST[\'email\'] ),
        \'user_url\' => esc_attr( $_POST[\'website\'] ),
        \'description\' => esc_attr( $_POST[\'description\'] ),
        \'role\' => get_option( \'default_role\' ),
    );

    if ( !$userdata[\'user_login\'] )
        $error = __(\'A username is required for registration.\', \'frontendprofile\');
    elseif ( username_exists($userdata[\'user_login\']) )
        $error = __(\'Sorry, that username already exists!\', \'frontendprofile\');

    elseif ( !is_email($userdata[\'user_email\'], true) )
        $error = __(\'You must enter a valid email address.\', \'frontendprofile\');
    elseif ( email_exists($userdata[\'user_email\']) )
        $error = __(\'Sorry, that email address is already used!\', \'frontendprofile\');
    else if ( $_POST[\'pass1\'] !== $_POST[\'pass2\'] ) {
        $error = __(\'Sorry, Passwords must match\', \'frontendprofile\');
    }
    else if ( strlen( $_POST[\'pass1\'] ) < 6 ) {
        $error = __(\'Sorry, Passwords must be at least six characters long\', \'frontendprofile\');
    }
    else{
        $new_user = wp_insert_user( $userdata );
        wp_new_user_notification($new_user, $user_pass);
    }
}

1 个回复
最合适的回答,由SO网友:Vinod Dalvi 整理而成

您可以声明$success变量并使用它显示成功消息,如下所示。

如果用户成功注册,则声明$success变量并为其分配成功消息,如下所示:

$success = \'\';
if ( \'POST\' == $_SERVER[\'REQUEST_METHOD\'] && !empty( $_POST[\'action\'] ) && $_POST[\'action\'] == \'adduser\' ) {
$user_pass = wp_generate_password();
$userdata = array(
    \'user_login\' => esc_attr( $_POST[\'user_name\'] ),
    \'user_pass\' => esc_attr( $_POST[\'pass1\'] ),
    \'user_email\' => esc_attr( $_POST[\'email\'] ),
    \'user_url\' => esc_attr( $_POST[\'website\'] ),
    \'description\' => esc_attr( $_POST[\'description\'] ),
    \'role\' => get_option( \'default_role\' ),
);

if ( !$userdata[\'user_login\'] )
    $error = __(\'A username is required for registration.\', \'frontendprofile\');
elseif ( username_exists($userdata[\'user_login\']) )
    $error = __(\'Sorry, that username already exists!\', \'frontendprofile\');

elseif ( !is_email($userdata[\'user_email\'], true) )
    $error = __(\'You must enter a valid email address.\', \'frontendprofile\');
elseif ( email_exists($userdata[\'user_email\']) )
    $error = __(\'Sorry, that email address is already used!\', \'frontendprofile\');
else if ( $_POST[\'pass1\'] !== $_POST[\'pass2\'] ) {
    $error = __(\'Sorry, Passwords must match\', \'frontendprofile\');
}
else if ( strlen( $_POST[\'pass1\'] ) < 6 ) {
    $error = __(\'Sorry, Passwords must be at least six characters long\', \'frontendprofile\');
}
else{
    $new_user = wp_insert_user( $userdata );
 if (!is_wp_error($return) ){
    $success = __($userdata[\'user_login\'].\' User registered $userdata[\'user_login\'].\', \'frontendprofile\');
    wp_new_user_notification($new_user, $user_pass);
}
}
}
您可以使用$success变量显示消息,如下所示:

if($success)
     echo $success;

结束

相关推荐