• About Me
  • My Portfolio
  • Blog
    • PHP
    • WORDPRESS
    • Laravel
    • Code Snippets
    • NodeJs
    • ReactJs
  • Contact Me
  • Hire Me On Upwork

Narinder Singh

Web Developer

Code Snippets WordPress

WordPress custom template for user login

By Narinder Bisht on Sunday, September 22, 2019

If you are looking for a custom template for your theme. Which including the WordPress user login and signup page process.
Generally, WordPress have default layout for user login and signup.

I am sharing a custom code. I hope it will help you.

/* Main redirection of the default login page */
function mytheme_redirect_login_page() {
	$login_page  = home_url('/login/');
	//$page_viewed = explode('?', basename($_SERVER['REQUEST_URI']) );
	
	//if( $page_viewed[0] == "wp-login.php" && $_SERVER['REQUEST_METHOD'] == 'GET' ) {
	$page_viewed = basename($_SERVER['REQUEST_URI']);

	if($page_viewed == "wp-login.php" && $_SERVER['REQUEST_METHOD'] == 'GET') {
		$redirect_to = isset( $_SERVER['QUERY_STRING'] ) ? $login_page.'/?'.$_SERVER['QUERY_STRING'] : $login_page;
		wp_redirect($redirect_to);
		exit;
	}
}
add_action('init','mytheme_redirect_login_page');

You need to add a page in your WordPress website with name login. You can change that If you change the login page slug. Same will update in above function code.

Following function code will redirect the user on the login page if the login failed.

More information about wp_login_failed

/* Where to go if a login failed */
function mytheme_custom_login_failed() {
	$login_page  = home_url('/login/');
	wp_redirect($login_page . '?login=failed');
	exit;
}
add_action('wp_login_failed', 'mytheme_custom_login_failed');

Following function verify the login form submit. Where to go if any of the fields were empty.

/* Where to go if any of the fields were empty */
function mytheme_verify_user_pass($user, $username, $password) {
	$login_page  = home_url('/login/');
	
	
	if($username == "" || $password == "") {
		if(isset($_SERVER['QUERY_STRING'])) {
			wp_redirect($login_page.'?'.$_SERVER['QUERY_STRING']);
		} else {
			wp_redirect($login_page . "?login=empty");
		}
		exit;
	}
}
add_filter('authenticate', 'mytheme_verify_user_pass', 1, 3);

What to do on logout. User will redirect to the login page.

/* What to do on logout */
function mytheme_logout_redirect() {
	$login_page  = home_url('/login/');
	wp_redirect($login_page . "?login=false");
	exit;
}
add_action('wp_logout','mytheme_logout_redirect');

If you want to add sign out link in your theme login. This following will help you. Kindly replace your menu location.

add_filter( 'wp_nav_menu_items', 'mytheme_custom_menu_item', 10, 2 );
function mytheme_custom_menu_item ( $items, $args ) {
    if ($args->theme_location == 'user_menu') {
        $items .= '<li><a class="dropdown-item" href="'. wp_logout_url() .'">Sign Out</a></li>';
    }
    return $items;
}

The following function will redirect the user after login successfully.

function mytheme_login_redirect( $redirect_to, $request, $user  ) {
	if ( ! is_wp_error( $user ) ) {
		// do redirects on successful login
		
		if( $redirect_to != ''){
			return $redirect_to;
		}
		elseif ( $user->has_cap( 'administrator' ) || $user->has_cap( 'shop_manager' ) ) {
			return admin_url();
		} else {
			return home_url();
		} 
	} else {
		// display errors, basically
		return $redirect_to;
	}
	
}

add_filter( 'login_redirect', 'mytheme_login_redirect', 10, 3 );

For view full source code. Kindly visit this GitHub repository

custom login page WP login tempate
0
Previous Post
Next Post
Related Posts
Code Snippets

How to fix DNS_PROBE_FINISHED_NXDOMAIN issue on chrome? If website is working fine with VPN extension.

Code Snippets WordPress

How to Customizing Email Templates, Subjects and add content dynamically?

WooCommerce WordPress

Can’t import reviews from CSV files- Customer Reviews for WooCommerce

About Me
Narinder Singh Bisht
Narinder Singh Bisht

Web Developer & Blogger

Hire me on Upwork
Narinder Upwork Profile
PDF Plugin
SeoBuddy
Recent Posts
  • Automate the Calendly and MailerLite integration to sync the event subscriber data with MailerLite into a specific group
  • How to fix DNS_PROBE_FINISHED_NXDOMAIN issue on chrome? If website is working fine with VPN extension.
  • How to Customizing Email Templates, Subjects and add content dynamically?
  • Can’t import reviews from CSV files- Customer Reviews for WooCommerce
  • The country code drop-down list doesn’t appear in the Elementor popup (country & phone field contact form 7)
Categories
  • API
  • Code Snippets
  • CSS
  • Drupal
  • HTML
  • Laravel
  • Magento
  • NodeJs
  • PHP
  • ReactJs
  • WooCommerce
  • WordPress
Theme by Scissor Themes Proudly powered by WordPress