使用Genesis添加“编辑个人资料”页面

时间:2015-08-27 作者:Sovnfield

在此之后post, 我尝试在genesis子主题的模板中添加代码,以编辑用户的配置文件(更改密码),使用genesis操作(genesis\\u entry\\u content,genesis\\u loop,…),更换genesis回路,。。

不幸的是,它不能正常工作;字段在那里,但我有很多错误,主要是未定义的变量。现在,我不知道如何将其与genesis一起插入,或者是否有更好的解决方案?

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

以下是我的完整代码(模板页),允许用户使用Genesis Framework更改密码:

<?php
// Global $error
$error = array();

add_action(\'genesis_before\',\'check_new_password\');
function check_new_password(){
/* Get user info. */
global $current_user, $wp_roles, $user_pass_md5, $error;

/* If profile was saved, update profile. */
if ( \'POST\' == $_SERVER[\'REQUEST_METHOD\'] && !empty( $_POST[\'action\'] ) && $_POST[\'action\'] == \'update-user\' ) {
    $old_pass = $_POST[\'old-pass\'];
    $new_pass1 = $_POST[\'pass1\'];
    $new_pass2 = $_POST[\'pass2\'];

    if(!empty($old_pass) && wp_check_password($old_pass,$current_user->user_pass)){
    /* Update user password. */
        if (!empty($new_pass1) && !empty($new_pass2)){
            if ( !$new_pass1 == $new_pass2 )
                $error[] = __(\'The passwords you entered do not match.  Your password was not updated.\', \'profile\');
        }else
            $error[] = __(\'Please add a new password.\', \'profile\');
    }else
        $error[] = __(\'The old password you entered is not valid.\', \'profile\');

    if( empty( $error ) ) {
        wp_clear_auth_cookie();

        // Set the new password for the user
        wp_set_password(esc_attr( $new_pass1 ), $current_user->ID);

        // Here we calculate the expiration length of the current auth cookie and compare it to the default expiration.
        // If it\'s greater than this, then we know the user checked \'Remember Me\' when they logged in.
        $logged_in_cookie = wp_parse_auth_cookie(\'\', \'logged_in\');

        // This filter is documented in wp-includes/pluggable.php
        $default_cookie_life = apply_filters(\'auth_cookie_expiration\', (2 * DAY_IN_SECONDS), $current_user->ID, false);
        $remember = (($logged_in_cookie[\'expiration\'] - time()) > $default_cookie_life);
        wp_set_auth_cookie($current_user->ID, $remember);

        wp_redirect(get_permalink());
        exit;
    }
}
}

function my_custom_loop() {
global $current_user, $wp_roles, $error;

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(!empty($error)) echo \'<p class="error">\' . implode("<br />", $error) . \'</p>\'; ?>
                <form method="post" id="adduser" action="<?php the_permalink(); ?>">
                    <p class="form-password">
                        <label for="old-pass"><?php _e(\'Old password *\', \'genesis\'); ?> </label>
                        <input class="text-input" name="old-pass" type="password" id="old-pass" />
                    </p><!-- .form-password -->
                    <p class="form-password">
                        <label for="pass1"><?php _e(\'New password *\', \'genesis\'); ?> </label>
                        <input class="text-input" name="pass1" type="password" id="pass1" />
                    </p><!-- .form-password -->
                    <p class="form-password">
                        <label for="pass2"><?php _e(\'Repeat the new password *\', \'genesis\'); ?></label>
                        <input class="text-input" name="pass2" type="password" id="pass2" />
                    </p><!-- .form-password -->

                    <?php 
                        // Action hook for plugin and extra fields
                        do_action(\'edit_user_profile\',$current_user);
                    ?>
                    <p class="form-submit">
                        <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><!-- .entry .post -->
    <?php endwhile; ?>
<?php else: ?>
    <p class="no-data">
        <?php _e(\'Sorry, no page matched your criteria.\', \'profile\'); ?>
    </p><!-- .no-data -->
<?php endif;
}
remove_action( \'genesis_loop\', \'genesis_do_loop\' );
add_action( \'genesis_loop\', \'my_custom_loop\' );

genesis(); ?>
因此,默认genesis循环被此表单替换。Wordpress 4.3和Genesis 2.2.0中包含Genesis子主题的作品

EDIT :@user3776790这是代码的第二个版本,有点不同:

<?php
/*
 * Template Name: Login page
 */
 // Start session if doesn\'t already exists
if (session_status() == PHP_SESSION_NONE) {
    session_start();
}

function login_loop() {
    global $user_login;

    if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <div id="post-<?php the_ID(); ?>">
        <div class="entry-content entry">
            <?php if(!empty($_SESSION[\'login-fail\']) && $_SESSION[\'login-fail\'] == \'failed\'){  ?>
                <div class="error">
                    <p>Username or password invalid. Try again.</p>
                </div>
            <?php 
                $_SESSION[\'login-fail\'] = \'\';
            }

        if (is_user_logged_in()) {
            echo \'<div class="logout">Hello, <div class="logout_user">\',     $user_login,\'. You are already logged in.</div><a id="wp-submit" href="\', wp_logout_url(home_url()), \'" title="Log out">Log out</a></div>\';
        } else {
            $args = array(
                \'echo\'           => true,
                \'redirect\'       => home_url(), 
                \'form_id\'        => \'loginform\',
                \'label_username\' => __(\'Username\'),
                \'label_password\' => __(\'Password\'),
                \'label_remember\' => __(\'Remember me\'),
                \'label_log_in\'   => __(\'Log In\'),
                \'id_username\'    => \'user_login\',
                \'id_password\'    => \'user_pass\',
                \'id_remember\'    => \'rememberme\',
                \'id_submit\'      => \'wp-submit\',
                \'remember\'       => true,
                \'value_username\' => NULL,
                \'value_remember\' => false
            );
            wp_login_form($args); 
        } ?> 
        </div><!-- .entry-content -->
        </div><!-- .entry .post -->
        <?php endwhile; ?>
    <?php else: ?>
        <p class="no-data">
            <?php _e(\'Sorry, no page matched your criteria.\',\'genesis\'); ?>
        </p><!-- .no-data -->
    <?php endif;
}
remove_action( \'genesis_loop\', \'genesis_do_loop\' );
add_action( \'genesis_loop\', \'login_loop\' );

genesis(); ?>
在函数中。php:

/**
 * Stays on the same page if login error
 */
add_action( \'wp_login_failed\', \'jinfo_front_end_login_fail\' );
function jinfo_front_end_login_fail( $username ){
    session_start();
    // Source adress of the form
    $referer = $_SERVER[\'HTTP_REFERER\'];

    // If it\'s a valid adress, a login fail session variable is created
    // and the page ir redirected to the login page
    if ( !empty($referer) && !strstr($referer,\'wp-login\') && !strstr($referer,\'wp-admin\') ) {
        $_SESSION[\'login-fail\'] = \'failed\';
        wp_redirect($referer);
        exit;
    }
}
我使用会话,就像Wordpress登录一样。效果更好

SO网友:user3776790

感谢Kerzzy,这或多或少起到了作用,尽管我需要在打开php标记后添加这一点:

/**
*
* Template Name: Change Password Template
*/
// fix buffering issue
ob_start();
我遇到了两个问题,上面的代码修复了这些问题。

如果不先声明,我无法将页面设置为使用此新模板

我遇到了一个标头缓冲错误--“警告:无法修改标头信息-标头已由…发送”

现在,它只需要一条成功的信息。当我开始工作时,我会向你汇报的。

相关推荐

如何从USERS-edit.php中删除颜色选择器代码

我知道我不应该改变Wordpress的核心。但如果我想在“用户编辑”中使用仪表板用户配置文件页面。php和删除大块代码(如颜色选择器)的方法。从第259行到第336行-我想全部删除。<?php if ( ! ( IS_PROFILE_PAGE && ! $user_can_edit ) ) : ?> <tr class="user-rich-editing-wrap"> <th scope="