0

Following this post, I tried to add the code in the template of a genesis child theme to edit user's profile (change the password), using genesis actions (genesis_entry_content, genesis_loop,...), replacing the genesis loop,..

Unfortunatly it doesn't work properly; the fields are there but I have a lot of errors, principally undefined variables. Now, I don't know how to insert it with genesis, or if there is a better solution ?

0

Thanks Kerzzy, This worked more or less, though I needed to add this right after the opening php tag:

/**
*
* Template Name: Change Password Template
*/
// fix buffering issue
ob_start();

I encountered two issues and the above code fixed those problems.

  1. I couldn't set my page to use this new template without declaring it first

  2. I had a header buffering error -- "Warning: Cannot modify header information - headers already sent by... "

Now all it needs is a success message. I'll report back when I get that working.

0

Here is my complete code (template page), to allow users to change their password with 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(); ?>

So the default genesis loop is replaced by this form. Works in Wordpress 4.3 and Genesis 2.2.0 with Genesis Child Theme

EDIT : @user3776790 Here is the second version of the code, a bit different :

<?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(); ?>

In functions.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;
    }
}

I use sessions, and the same way that Wordpress uses to log in. Works better

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.