以下是我的完整代码(模板页),允许用户使用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登录一样。效果更好