php 用户客户логинрегистрацияисменапаролянафронтенде

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 用户客户логинрегистрацияисменапаролянафронтенде相关的知识,希望对你有一定的参考价值。

----------------- functions.php

function remove_default_password_nag() {
global $user_ID;
delete_user_setting('default_password_nag', $user_ID);
update_user_option($user_ID, 'default_password_nag', false, true);
}
add_action('admin_init', 'remove_default_password_nag');

function custom_wp_register_form( $user_login, $user_password ) {
	$errors = new WP_Error();
	
	$sanitized_user_login = sanitize_user( $user_login );
 
	// Check the username
	if (empty($sanitized_user_login)) {
		$errors->add( 'empty_username', __( '<span class="message-error">Please enter a username.</span>' ) );
	} elseif ( ! validate_username( $user_login ) ) {
		$errors->add( 'invalid_username', __( '<span class="message-error">This username is invalid because it uses illegal characters. Please enter a valid username.</span>' ) );
		$sanitized_user_login = '';
	} elseif ( ! is_email( $sanitized_user_login ) ) {
		$errors->add( 'invalid_email', __( '<span class="message-error">Please use an email address for the username.</span>' ) );
	} elseif ( username_exists( $sanitized_user_login ) ) {
		$errors->add( 'username_exists', __( '<span class="message-error">Username already exists.</span>' ) );
	}
	
	
	if ($user_password !== '' )
	{
		$userdata['user_pass'] = $_POST['password'];
	}else{
		$errors->add( 'empty_password', __( '<span class="message-error">Please enter a password.</span>' ) );
	}
	
	if ( $errors->get_error_code() )
		return $errors;
	
	$user_id = wp_create_user( $sanitized_user_login, $user_password, $user_login);
	
	if ( ! $user_id ) {
		$errors->add( 'registerfail', sprintf( __( '<strong>ERROR</strong>: Couldn&#8217;t register you... please contact the <a href="mailto:%s">webmaster</a> !' ), get_option( 'admin_email' ) ) );
		return $errors;
	}
 
	update_user_option( $user_id, 'default_password_nag', true, true ); //Set up the Password change nag.
 
	return $user_id;
}

function wp_change_user_password_notification($user_id, $plaintext_pass = '')
{
	$user = get_userdata( $user_id );

	$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
	
	if ( empty($plaintext_pass) )
		return;

	$message  = sprintf(__('Username: %s'), $user->user_login) . "\r\n";
	$message .= sprintf(__('Password: %s'), $plaintext_pass) . "\r\n";
	$message .= get_page_link(loginPageID) . "\r\n";

	wp_mail($user->user_email, sprintf(__('%s Your username and password'), $blogname), $message);
}

function retrieve_password_custom($user_login)
{
	global $wpdb, $wp_hasher;
	$user_pass = '';
	$errors = new WP_Error();

	if ( empty( $user_login ) ) {
		$errors->add('empty_username', __('<strong>ERROR</strong>: Enter a username or e-mail address.'));
	} else if ( strpos( $user_login, '@' ) ) {
		$user_data = get_user_by( 'email', trim( $user_login ) );
		if ( empty( $user_data ) )
			$errors->add('invalid_email', __('<strong>ERROR</strong>: There is no user registered with that email address.'));
	} else {
		$login = trim($user_login);
		$user_data = get_user_by('login', $login);
	}
	
	if ( $errors->get_error_code() )
		return $errors;

	if ( !$user_data ) {
		$errors->add('invalidcombo', __('<strong>ERROR</strong>: Invalid username or e-mail.'));
		return $errors;
	}
	
	if(!$user_pass) $user_pass = wp_generate_password( 8, false);
	
	wp_update_user(array('ID' => $user_data->ID, 'user_pass' => $user_pass));
	
	wp_change_user_password_notification($user_data->ID, $user_pass);
	
	return $user_data->ID;
}


---------------- TEMPLATE 

<?php
	if($_POST['register']){
		$user_login = $_POST['user_login'];
		$user_password = $_POST['user_password'];
		$errors = custom_wp_register_form($user_login, $user_password);
		if ( !is_wp_error($errors) ) {
			$register_redirect_to = !empty( $_POST['redirect_to'] ) ? $_POST['redirect_to'].'?registration=success' : 'wp-login.php?checkemail=registered';
			wp_safe_redirect( $register_redirect_to );
			exit();
		}
	}
	if($_POST['reset']){
		$reset_errors = retrieve_password_custom($_POST['user_login']);
		if ( !is_wp_error($reset_errors) ) {
			$reset_redirect_to = !empty( $_POST['redirect_to'] ) ? $_POST['redirect_to'] : 'wp-login.php?action=lostpassword';
			wp_safe_redirect( $reset_redirect_to );
			exit();
		}
	}
	if($_POST['log']){
		$login = wp_signon();
		unset($_GET['registration']);
		$login_redirect_to = !empty( $_POST['redirect_to'] ) ? $_POST['redirect_to'].'?login=success' : 'wp-login.php';
		if (!is_wp_error($login) ) {
			wp_safe_redirect($login_redirect_to);
			exit();
		}
		else{
			$login_errors = $login->get_error_message();
		}
	}
?>
<?php
/*
Template Name: Log in Sign up Template
*/
get_header(); ?>

	<div class="two-column"> 
		<!-- content -->
		<div id="content">
			<!-- generic form -->
			<section class="generic-form">
				<?php if (have_posts()) : ?><?php while (have_posts()) : the_post(); ?>
					<?php the_content(); ?>
				<?php endwhile; ?><?php endif; ?>
				
				<?php
					$redirect = get_page_link(loginPageID);
					if(!empty($_GET['to-page'])){ $redirect = get_page_link($_GET['to-page']); }
				?>
				
				<form name="loginform" id="loginform" action="<?php echo get_page_link(loginPageID); ?>" method="post" class="login-form">
					<fieldset>
						<legend title="login">login</legend>
						<h2>Log In</h2>
						<?php
							print_r($login_errors);
							if(!empty($_GET['login']))
							{
								echo '<span class="message-success">You have successfully logged in</span>';
							}
						?>
						<div class="row">
							<div class="txt"><input type="text" name="log" id="log" placeholder="Username/Email" title="Username/Email" /></div>
							<div class="txt"><input type="password" name="pwd" id="pwd" placeholder="Password" title="Password" /></div>
							<input type="submit" name="submit" id="submit" value="Log in">
							<input type="hidden" name="redirect_to" value="<?php echo $redirect; ?>" />
							<input name="rememberme" type="hidden" id="rememberme" value="forever" class="check" />
						</div>
					</fieldset>
				</form>
				
				<?php if (get_option('users_can_register')): ?>
					<form name="registerform" id="registerform" class="contact-form" action="<?php echo get_page_link(loginPageID); ?>" method="post">
						<fieldset>
							<legend title="signup">signup</legend>
							<h2>Sign Up</h2>
							<?php
								if(is_wp_error($errors)){
									foreach($errors->get_error_codes() as $code){
										foreach($errors->get_error_messages($code) as $error){
											echo $error;
										}
									}
								}
								if(!empty($_GET['registration']))
								{
									echo '<span class="message-success">Registration complete. Now you can log in.</span>';
								}
							?>
							<div class="row">
								<div class="txt"><input type="text" name="user_login" id="register-user-login" value="" placeholder="Username/Email" title="Username/Email" /></div>
								<div class="txt">
									<input type="password" name="user_password" placeholder="Password" title="Password">
								</div>
								<input type="hidden" name="redirect_to" value="<?php echo get_page_link(loginPageID); ?>" />
								<input type="hidden" name="register" value="1" />
								<input type="submit" name="wp-submit" value="Sign up" />
							</div>
						</fieldset>
					</form>
				<?php endif; ?>
				
				<form method="post" action="<?php echo get_page_link(loginPageID); ?>" class="wp-user-form">
					<fieldset>
						<legend title="Lost-password">Lost password</legend>
						<h2>Lost password</h2>
						<?php
							if(is_wp_error($reset_errors)){
								foreach($reset_errors->get_error_codes() as $code){
									foreach($reset_errors->get_error_messages($code) as $error){
										echo $error;
									}
								}
							}
						?>
						<?php $reset = $_GET['reset']; if($reset == true) { echo '<span class="message-success"> The password will be sent to your email address.</span>'; } ?>
						<div class="row">
							<div class="txt"><input type="email" name="user_login" placeholder="Username/Email" title="Username/Email"></div>
							<?php do_action('login_form', 'resetpass'); ?>
							<input type="hidden" name="reset" value="1" />
							<input type="submit" name="user-submit" value="Retrieve Password" title="Retrieve Password">
							<input type="hidden" name="redirect_to" value="<?php echo get_page_link(loginPageID); ?>?reset=true" />
							<input type="hidden" name="user-cookie" value="1" />
						</div>
					</fieldset>
				</form>
				
			</section>
		</div>
	</div>

<?php get_footer(); ?>


----- change password

function user_change_password_process(){
 global $errors;
 if($_POST['reset_password']) {
  $errors = new WP_Error();
  
  $user_ID = get_current_user_id();
 
  if(!is_user_logged_in())
   return;
  
  if($_POST['password'] == '' || $_POST['repeat_password'] == '') {
   $errors->add('password_empty', __('<strong>ERROR</strong>: Please enter a password, and confirm it', 'base'));
  }
  if($_POST['password'] != $_POST['repeat_password']) {
   // passwords do not match
   $errors->add('password_mismatch', __('<strong>ERROR</strong>: Passwords do not match', 'base'));
  }
  if ( strlen( $_POST['password'] ) < 8 ) {
   $errors->add( 'password_too_short', "<strong>ERROR</strong>: Passwords must be at least eight characters long" );
  }

  if ( $errors->get_error_code() )
   return $errors;

  
  $user_data = array(
   'ID' => $user_ID,
   'user_pass' => $_POST['password']
  );
  wp_update_user($user_data);
  wp_change_user_password_notification($user_ID, $_POST['password']);
  wp_redirect(add_query_arg('password-reset', '1', $_POST['redirect']));
  exit;
 }
}

function custom_wp_login_form( $args = array() ) {
	$defaults = array( 'echo' => true,
						'redirect' => ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], // Default redirect is back to the current page
	 					'form_id' => 'loginform',
						'label_username' => __( 'Username' ),
						'label_password' => __( 'Password' ),
						'label_remember' => __( 'Remember Me' ),
						'label_log_in' => __( 'go' ),
						'id_username' => 'user_login',
						'id_password' => 'user_pass',
						'id_remember' => 'rememberme',
						'id_submit' => 'wp-submit',
						'remember' => true,
						'value_username' => '',
						'value_remember' => false, // Set this to true to default the "Remember me" checkbox to checked
					);
	$args = wp_parse_args( $args, apply_filters( 'login_form_defaults', $defaults ) );

	$form = '
		<form class="login-form" name="' . $args['form_id'] . '" id="' . $args['form_id'] . '" action="' . esc_url( site_url( 'wp-login.php', 'login_post' ) ) . '" method="post">
			' . apply_filters( 'login_form_top', '', $args ) . '
			<fieldset><div class="row"><label>Login</label>
				<input type="text" name="log" id="' . esc_attr( $args['id_username'] ) . '" class="input" value="' . esc_attr( $args['value_username'] ) . '" size="20" />
				<input type="password" name="pwd" id="' . esc_attr( $args['id_password'] ) . '" class="input" value="" size="20" /></div>
			' . apply_filters( 'login_form_middle', '', $args ) . '
			' . ( $args['remember'] ? '' : '' ) . '
				<input type="submit" name="wp-submit" id="' . esc_attr( $args['id_submit'] ) . '" class="button-primary" value="' . esc_attr( $args['label_log_in'] ) . '" />
				<input type="hidden" name="redirect_to" value="' . esc_url( $args['redirect'] ) . '" />
			' . apply_filters( 'login_form_bottom', '', $args ) . '
		</fieldset></form>';

	if ( $args['echo'] )
		echo $form;
	else
		return $form;
}
?>

<div class="tabs-area">
	<div class="heading">
		<a href="<?php bloginfo('url'); ?>/wp-login.php?action=lostpassword" class="forgot">Forgot password?</a>
		<ul class="tabset">
			<li class=""><a href="#tab-1" class="tab active">Login in</a></li>
			<li><a href="#tab-2" class="tab">Register</a></li>
		</ul>
	</div>
	<div class="form">
		<div class="holder">
			<div class="frame">
				<div class="tab-content" id="tab-1">
					<form name="loginform" id="loginform" action="<?php bloginfo('url'); ?>/wp-login.php" method="post" class="login-form">
						<fieldset>
							<ul>
								<li>
									<label for="log">Username</label>
									<div class="text-input">
										<input type="text" name="log" id="log"/>
									</div>
								</li>
								<li>
									<label for="pwd">Password</label>
									<div class="text-input">
										<input type="password" name="pwd" id="pwd" />
									</div>
								</li>
								<li>
									<input type="submit" name="submit" id="submit" tabindex="4" class="btn-submit" value="Sign in"/>
									<input type="hidden" name="redirect_to" value="<?php echo get_option( 'user_account_url' ); ?>" />
									<div class="wrap">
										<input name="rememberme" type="checkbox" id="rememberme" value="forever" tabindex="3" class="check" />
										<label for="rememberme">Remember me</label>
									</div>
								</li>
							</ul>
						</fieldset>
					</form>
				</div>
				<div class="tab-content" id="tab-2">

					<form name="registerform" id="registerform" action="<?php bloginfo('url'); ?>/wp-login.php?action=register" method="post" class="login-form">
						<fieldset>
							<ul>
								<li>
									<label for="user_login">Username</label>
									<div class="text-input">
										<input type="text" name="user_login" id="user_login" class="input" />
									</div>
								</li>
								<li>
									<label for="user_email">E-mail</label>
									<div class="text-input">
										<input type="text" name="user_email" id="user_email" class="input" />
									</div>
								</li>
								<li>
									<input type="submit" name="wp-submit" id="wp-submit" class="btn-submit" value="Register"/>
									<input type="hidden" name="redirect_to" value="<?php echo get_option( 'user_account_url' ); ?>" />
									<div class="wrap wrap-r">
										<p id="reg_passmail">A password will be e-mailed to you.</p>
										<br class="clear" />
									</div>
								</li>
							</ul>
						</fieldset>
					</form>
				</div>
			</div>
		</div>
	</div>
</div>


--------------------------- или ----------------------

<?php
	if($_POST['register']){ 
		$user_login = $_POST['user_login'];
		$user_password = $_POST['user_password'];
		$errors = custom_wp_register_form($user_login, $user_password);
		if ( !is_wp_error($errors) ) {
			$redirect_to = !empty( $_POST['redirect_to'] ) ? $_POST['redirect_to'] : 'wp-login.php?checkemail=registered';
			wp_safe_redirect( $redirect_to );
			exit();
		}
	}
?>
<?php
/*
Template Name: Log in Sign up Template
*/
get_header(); ?>

	<div class="two-column"> 
		<!-- content -->
		<div id="content">
			<!-- generic form -->
			<section class="generic-form">
				<?php if (have_posts()) : ?><?php while (have_posts()) : the_post(); ?>
					<?php the_content(); ?>
				<?php endwhile; ?><?php endif; ?>
				
				<?php echo custom_wp_login_form(); ?>
				
				<?php if (get_option('users_can_register')): ?>
					<form name="registerform" id="registerform" class="contact-form" action="<?php echo get_permalink(); ?>" method="post">
						<fieldset>
							<legend title="signup">signup</legend>
							<h2>Sign Up</h2>
							<div class="row">
								<?php
									if(is_wp_error($errors)){
										foreach($errors->get_error_codes() as $code){
											foreach($errors->get_error_messages($code) as $error){
												echo $error;
											}
										}
									}
								?>
								<div class="txt"><input type="text" name="user_login" id="register-user-login" value="" placeholder="Username/Email" title="Username/Email" /></div>
								<div class="txt">
									<input type="password" name="user_password" placeholder="Password" title="Password">
								</div>
								<input type="hidden" name="redirect_to" value="<?php echo get_permalink(); ?>" />
								<input type="hidden" name="register" value="1" />
								<input type="submit" name="wp-submit" value="Sign up" />
							</div>
						</fieldset>
					</form>
				<?php endif; ?>
				
			
				<form method="post" action="<?php echo site_url('wp-login.php?action=lostpassword', 'login_post') ?>" class="wp-user-form">
					<fieldset>
						<legend title="Lost-password">Lost password</legend>
						<h2>Lost password</h2>
						<?php $reset = $_GET['reset']; if($reset == true) { echo '<span class="message">A message will be sent to your email address.</span>'; } ?>
						<div class="row">
							<div class="txt"><input type="email" name="user_login" placeholder="Username/Email" title="Username/Email"></div>
							<?php do_action('login_form', 'resetpass'); ?>
							<input type="submit" name="user-submit" value="Retrieve Password" title="Retrieve Password">
							<input type="hidden" name="redirect_to" value="<?php echo $_SERVER['REQUEST_URI']; ?>?reset=true" />
							<input type="hidden" name="user-cookie" value="1" />
						</div>
					</fieldset>
				</form>
				
			</section>
		</div>
	</div>

<?php get_footer(); ?>


function custom_wp_login_form( $args = array() ) {
	$defaults = array( 'echo' => true,
						'redirect' => ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], // Default redirect is back to the current page
	 					'form_id' => 'loginform',
						'label_username' => __( 'Username' ),
						'label_password' => __( 'Password' ),
						'label_remember' => __( 'Remember Me' ),
						'label_log_in' => __( 'go' ),
						'id_username' => 'user_login',
						'id_password' => 'user_pass',
						'id_remember' => 'rememberme',
						'id_submit' => 'wp-submit',
						'remember' => true,
						'value_username' => '',
						'value_remember' => false, // Set this to true to default the "Remember me" checkbox to checked
					);
	$args = wp_parse_args( $args, apply_filters( 'login_form_defaults', $defaults ) );

	$form = '
		<form class="login-form" name="' . $args['form_id'] . '" id="' . $args['form_id'] . '" action="' . esc_url( site_url( 'wp-login.php', 'login_post' ) ) . '" method="post">
			' . apply_filters( 'login_form_top', '', $args ) . '
			<fieldset><legend title="login">login</legend><h2>Log In</h2><div class="row">
				<div class="txt"><input type="text" name="log" id="' . esc_attr( $args['id_username'] ) . '" class="input" value="' . esc_attr( $args['value_username'] ) . '" placeholder="Username/Email"  /></div>
				<div class="txt"><input type="password" name="pwd" id="' . esc_attr( $args['id_password'] ) . '" class="input" value="" placeholder="Password" /></div>
			' . apply_filters( 'login_form_middle', '', $args ) . '
			' . ( $args['remember'] ? '' : '' ) . '
				<input type="submit" name="wp-submit" id="' . esc_attr( $args['id_submit'] ) . '" class="button-primary" value="Log in" />
				<input type="hidden" name="redirect_to" value="' . esc_url( $args['redirect'] ) . '" /></div>
			' . apply_filters( 'login_form_bottom', '', $args ) . '
		</fieldset></form>';

	if ( $args['echo'] )
		echo $form;
	else
		return $form;
}

function remove_default_password_nag() {
global $user_ID;
delete_user_setting('default_password_nag', $user_ID);
update_user_option($user_ID, 'default_password_nag', false, true);
}
add_action('admin_init', 'remove_default_password_nag');

function custom_wp_register_form( $user_login, $user_password ) {
	$errors = new WP_Error();
	
	$sanitized_user_login = sanitize_user( $user_login );
 
	// Check the username
	if (empty($sanitized_user_login)) {
		$errors->add( 'empty_username', __( '<span class="message-error">Please enter a username.</span>' ) );
	} elseif ( ! validate_username( $user_login ) ) {
		$errors->add( 'invalid_username', __( '<span class="message-error">This username is invalid because it uses illegal characters. Please enter a valid username.</span>' ) );
		$sanitized_user_login = '';
	} elseif ( ! is_email( $sanitized_user_login ) ) {
		$errors->add( 'invalid_email', __( '<span class="message-error">Please use an email address for the username.</span>' ) );
	} elseif ( username_exists( $sanitized_user_login ) ) {
		$errors->add( 'username_exists', __( '<span class="message-error">Username already exists.</span>' ) );
	}
	
	
	if ($user_password !== '' )
	{
		$userdata['user_pass'] = $_POST['password'];
	}else{
		$errors->add( 'empty_password', __( '<span class="message-error">Please enter a password.</span>' ) );
	}
	
	if ( $errors->get_error_code() )
		return $errors;
	
	$user_id = wp_create_user( $sanitized_user_login, $user_password, $user_login);
	
	if ( ! $user_id ) {
		$errors->add( 'registerfail', sprintf( __( '<strong>ERROR</strong>: Couldn&#8217;t register you... please contact the <a href="mailto:%s">webmaster</a> !' ), get_option( 'admin_email' ) ) );
		return $errors;
	}
 
	update_user_option( $user_id, 'default_password_nag', true, true ); //Set up the Password change nag.
 
	return $user_id;
}

以上是关于php 用户客户логинрегистрацияисменапаролянафронтенде的主要内容,如果未能解决你的问题,请参考以下文章

php Регистрацияпользователявопределеннуюгруппу

php Постраничнаянавигациявдеталке

php Шаблоныпоусловиючерез$ template#страница#категория#запись

php 元素与WP - Отменаформатированиякраткогоописаниятолькодляопределенныхстраниц

python Скриптпреобразованияматрицызаполняемойпострочновматрицузаполняемуюпоспирали---найденчерезгугл

php WordPress - ДобавляемКатегориииМеткик(стандарным)Страницам