如何在WP3.3中更新前端的USER_EMAIL?

时间:2012-03-11 作者:Carson

我正在使用以下代码,用户配置文件中的所有内容都在更新,除了用户的电子邮件。

在模板中:

global $current_user, $wp_roles;
get_currentuserinfo();

/* Load the registration file. */
require_once( ABSPATH . WPINC . \'/registration.php\' );

/* 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
                        wp_redirect( get_permalink() . \'?error\' );
                exit;
        }

        /* Update user information. */
        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[\'user_email\'] ) )
                update_user_meta($current_user->id, \'user_email\', esc_attr( $_POST[\'user_email\'] ) );
        if ( !empty( $_POST[\'cell_phone\'] ) )
                update_user_meta( $current_user->id, \'cell_phone\', esc_attr( $_POST[\'cell_phone\'] ) );
        if ( !empty( $_POST[\'mailing_address\'] ) )
                update_user_meta( $current_user->id, \'mailing_address\', esc_attr( $_POST[\'mailing_address\'] ) );
        if ( !empty( $_POST[\'description\'] ) )
                update_user_meta( $current_user->id, \'description\', esc_attr( $_POST[\'description\'] ) );


             /* Redirect so the page will show updated info. */
    if ( !$error ) {
        wp_redirect( get_permalink() . \'?success\' );
    }
        else {
            wp_redirect( get_permalink() . \'?error\' );
        }
        exit;
    }


get_header(); ?>
在页面中:

<?php if (stripos($_SERVER[\'REQUEST_URI\'],\'?success\') !== false) { // THIS IS THE BEGINNING ?>
    <div class="alert alert-success" align="center">Your profile was updated successfully.</div>
<?php } ?>
<?php if (stripos($_SERVER[\'REQUEST_URI\'],\'?error\') !== false) { // THIS IS THE BEGINNING ?>
    <div class="alert alert-danger" align="center">Hmm, something went wrong and your profile was not updated.</div>
<?php } ?>

<form method="post" id="adduser" action="<?php the_permalink(); ?>">
    <table class="profile">
        <tr>
            <td class="left">
                First Name
            </td>
            <td class="right">
                <input type="text" name="first_name" id="first_name" value="<?php global $current_user; get_currentuserinfo(); echo $current_user->first_name; ?>" />
            </td>
        </tr>

         <tr>
            <td class="left">
                Last Name   
            </td>
            <td class="right">
                <input type="text" name="last_name" id="last_name" value="<?php global $current_user; get_currentuserinfo(); echo $current_user->last_name; ?>" />
            </td>
        </tr>

        <tr>
            <td class="left">
                Email Address
            </td>
            <td class="right">
                <input type="text" name="user_email" id="user_email" value="<?php global $current_user; get_currentuserinfo(); echo $current_user->user_email; ?>" />
            </td>
        </tr>

        <tr>
            <td class="left">
                Phone Number
            </td>
            <td class="right">
                <input type="text" name="cell_phone" id="cell_phone" value="<?php global $current_user; get_currentuserinfo(); echo $current_user->cell_phone; ?>" />
            </td>
        </tr>

         <tr>
            <td class="left">
                Mailing Address
            </td>
            <td class="right">
                <textarea name="mailing_address" id="mailing_address" rows="4" cols="30" class="regular-text"><?php global $current_user; get_currentuserinfo(); echo $current_user->mailing_address; ?></textarea>
            </td>
        </tr>

        <tr>
            <td class="left">
                Business Description
            </td>
            <td class="right">
                <textarea name="description" id="description" rows="4" cols="30" class="regular-text"><?php global $current_user; get_currentuserinfo(); echo $current_user->description; ?></textarea>
            </td>
        </tr>

    </table>

    <p class="form-submit">

        <?php echo $referer; ?>
        <input name="updateuser" type="submit" id="updateuser" class="submit button" value="update-user" />
        <?php wp_nonce_field( \'update-user\' ) ?>
        <input name="action" type="hidden" id="action" value="Update" />
    </p>
</form>

2 个回复
最合适的回答,由SO网友:mor7ifer 整理而成

您需要使用wp_update_user() 对于电子邮件,因为它不是用户元,而是核心用户数据。代码应如下所示:

$args = array(
    \'ID\'         => $current_user->id,
    \'user_email\' => esc_attr( $_POST[\'user_email\'] )
);
wp_update_user( $args );
注意:这是未经测试的,但应该是现成的。

SO网友:x y

如果您计划在前端使用此代码,我会检查电子邮件是否可以免费使用。否则,您将创建一个安全漏洞。

if (isset( $_POST[\'email\'])) {
    // check if user is really updating the value
    if ($user_email != $_POST[\'email\']) {       
        // check if email is free to use
        if (email_exists( $_POST[\'email\'] )){
            // Email exists, do not update value.
            // Maybe output a warning.
        } else {
            $args = array(
                \'ID\'         => $current_user->id,
                \'user_email\' => esc_attr( $_POST[\'email\'] )
            );            
        wp_update_user( $args );
       }   
   }
}     

结束

相关推荐

New post email alert

我想显示一个复选框,其中包含通过电子邮件订阅网站的选项我看到一个wordpress博客,上面的复选框旁边有以下标签通过电子邮件订阅此网站问题是我找不到显示它的插件:我尝试了Subscribe2、Post Notification和Gurken进行评论订阅。你知道我需要安装什么插件吗?