如何编辑自定义用户元信息前端

时间:2017-01-15 作者:rob12345

我试图允许用户更新网站前端的自定义user\\u元字段。它在后端工作正常,但在前端页面上,当按update时,页面变为空白,但仍在更新用户元。有人能帮我解决空白页的问题吗。

我最初发现了这个问题How to edit a user profile on the front end?. 我将在下面发布代码,我想他们一定是页面顶部的php出错导致了这种情况。

上一篇帖子的答案:

你可以通过复制你的主题页面来做到这一点。php到一个名为user profile之类的新文件。php,并在其顶部添加以下代码:

/* 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.*/
  /*I am not Author of this Code- i dont know why but it worked for me after changing below line to if ( count($error) == 0 ){ */
    if ( count($error) == 0 ) {
        //action hook for plugins and extra fields saving
        do_action(\'edit_user_profile_update\', $current_user->ID);
        wp_redirect( get_permalink() );
        exit;
    }
}
?>
然后将该页的循环替换为以下循环:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <div id="post-<?php the_ID(); ?>">
        <div class="entry-content entry">
            <?php the_content(); ?>
            <?php if ( !is_user_logged_in() ) : ?>
                    <p class="warning">
                        <?php _e(\'You must be logged in to edit your profile.\', \'profile\'); ?>
                    </p><!-- .warning -->
            <?php else : ?>
                <?php if ( count($error) > 0 ) echo \'<p class="error">\' . implode("<br />", $error) . \'</p>\'; ?>
                <form method="post" id="adduser" action="<?php the_permalink(); ?>">
                    <p class="form-username">
                        <label for="first-name"><?php _e(\'First Name\', \'profile\'); ?></label>
                        <input class="text-input" name="first-name" type="text" id="first-name" value="<?php the_author_meta( \'first_name\', $current_user->ID ); ?>" />
                    </p><!-- .form-username -->
                    <p class="form-username">
                        <label for="last-name"><?php _e(\'Last Name\', \'profile\'); ?></label>
                        <input class="text-input" name="last-name" type="text" id="last-name" value="<?php the_author_meta( \'last_name\', $current_user->ID ); ?>" />
                    </p><!-- .form-username -->
                    <p class="form-email">
                        <label for="email"><?php _e(\'E-mail *\', \'profile\'); ?></label>
                        <input class="text-input" name="email" type="text" id="email" value="<?php the_author_meta( \'user_email\', $current_user->ID ); ?>" />
                    </p><!-- .form-email -->
                    <p class="form-url">
                        <label for="url"><?php _e(\'Website\', \'profile\'); ?></label>
                        <input class="text-input" name="url" type="text" id="url" value="<?php the_author_meta( \'user_url\', $current_user->ID ); ?>" />
                    </p><!-- .form-url -->
                    <p class="form-password">
                        <label for="pass1"><?php _e(\'Password *\', \'profile\'); ?> </label>
                        <input class="text-input" name="pass1" type="password" id="pass1" />
                    </p><!-- .form-password -->
                    <p class="form-password">
                        <label for="pass2"><?php _e(\'Repeat Password *\', \'profile\'); ?></label>
                        <input class="text-input" name="pass2" type="password" id="pass2" />
                    </p><!-- .form-password -->
                    <p class="form-textarea">
                        <label for="description"><?php _e(\'Biographical Information\', \'profile\') ?></label>
                        <textarea name="description" id="description" rows="3" cols="50"><?php the_author_meta( \'description\', $current_user->ID ); ?></textarea>
                    </p><!-- .form-textarea -->

                    <?php 
                        //action hook for plugin and extra fields
                        do_action(\'edit_user_profile\',$current_user); 
                    ?>
                    <p class="form-submit">
                        <?php echo $referer; ?>
                        <input name="updateuser" type="submit" id="updateuser" class="submit button" value="<?php _e(\'Update\', \'profile\'); ?>" />
                        <?php wp_nonce_field( \'update-user\' ) ?>
                        <input name="action" type="hidden" id="action" value="update-user" />
                    </p><!-- .form-submit -->
                </form><!-- #adduser -->
            <?php endif; ?>
        </div><!-- .entry-content -->
    </div><!-- .hentry .post -->
    <?php endwhile; ?>
<?php else: ?>
    <p class="no-data">
        <?php _e(\'Sorry, no page matched your criteria.\', \'profile\'); ?>
    </p><!-- .no-data -->
<?php endif; ?>
剩下要做的就是创建一个新页面并选择“用户配置文件”作为页面模板。

上一页结束

如果有人有解决方案,那会很有帮助的。谢谢。

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

我已经解决了。

在重定向中,我更改了if (count($error) == 0 ){if (count($error) < 1 ){

此外,对于添加的任何自定义用户元字段,请复制以下行,并将“description”一词更改为函数文件中创建的字段的名称。

if ( !empty( $_POST[\'description\'] ) )
        update_user_meta( $current_user->ID, \'description\', esc_attr( $_POST[\'description\'] ) );

相关推荐

GravityForms是否将每个用户或IP的提交时间限制为30分钟?

我遵循GravityWiz的DIY代码,但作者没有提到如何让代码在30分钟、1小时、2小时等时间内工作。。。?他只解释了如何在24小时内工作-http://gravitywiz.com/better-limit-submission-per-time-period-by-user-or-ip/当查看此处的代码时-https://gist.githubusercontent.com/spivurno/4024361/raw/gw-gravity-forms-submission-limit.php这是否意味