How to create a login form in wordpress without plugin

How to create a login form in wordpress without plugin
296 Views

Are you need simple login form without plugin for your WordPress website? Yes, Then i have created [cxc_login_form] shortcode with quick simple code. So you can put login form anywhere like in Pages, Posts and Widget etc… Therefore you just need to put [cxc_login_form] shortcode where you need login form. So you need to follow below steps.

Code for shortcode.

You need to put below code in theme’s functions.php file. If you have created child theme then add this in function file of child theme.

Login form
<?php
add_shortcode( 'cxc_login_form', 'cxc_login_form_callback' );

function cxc_login_form_callback() {
	ob_start();

	if ( !is_user_logged_in() ) {

		global $errors_login;

		if (!empty( $errors_login ) ) {
			?>
			<div class="alert alert-danger">
				<?php echo $errors_login; ?>
			</div>
		<?php } ?>
		<form method="post" class="wc-login-form">
			<div class="login_form">
				<div class="log_user">
					<label for="user_name">Username</label>
					<input name="log" type="text" id="user_name" value="<?php echo isset($_POST['log']) ? $_POST['log'] : ''; ?>">
				</div>
				<div class="log_pass">
					<label for="user_password">Password</label>
					<input name="pwd" id="user_password" type="password">
				</div>
				<?php
				ob_start();
				do_action( 'login_form' );
				echo ob_get_clean();
				?>
				<?php wp_nonce_field( 'userLogin', 'formType' ); ?>
			</div>
			<button type="submit">LOG IN</button>
		</form>
		<?php
	} else {
		echo '<p class="error-logged">You are already logged in.</p>';
	}

	$login_form = ob_get_clean();
	return $login_form;
}
?>

Code for Login.

In this code i am validating form and log in WordPress on wp action. Add it in functions.php file after above code. If this code will not work then change action wp to init.

<?php
add_action( 'wp', 'cxc_user_login_callback' );

function cxc_user_login_callback() {

	if ( isset( $_POST['formType'] ) && wp_verify_nonce( $_POST['formType'], 'userLogin' ) ) {
		global $errors_login;
		$uName = $_POST['log'];
		$uPassword = $_POST['pwd'];		

		if ($uName == '' && $uPassword != '') {
			$errors_login = '<strong>Error! </strong> Username is required.';
		} elseif ($uName != '' && $uPassword == '') {
			$errors_login = '<strong>Error! </strong> Password is required.';
		} elseif ($uName == '' && $uPassword == '') {
			$errors_login = '<strong>Error! </strong> Username & Password are required.';
		} elseif ($uName != '' && $uPassword != '') {
			$creds = array();
			$creds['user_login'] = $uName;
			$creds['user_password'] = $uPassword;
			$creds['remember'] = false;
			$user = wp_signon( $creds, false );
			if ( is_wp_error($user) ) {
				$errors_login = $user->get_error_message();
			} else {				
				wp_set_current_user( $user->ID );
				wp_set_auth_cookie( $user->ID );
				do_action( 'wp_login', $user->user_login, $user );
				wp_redirect( site_url() );
				exit;
			}
		}
	}
}
?>

WordPress Default Login Form.

Provides a simple login form for use anywhere within WordPress. You can get login form using wp_login_form() function.

<?php
  if (!is_user_logged_in()) {
      $args = array(
          'echo' => true,
          'remember' => true,
          'redirect' => ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'],
          'form_id' => 'loginform',
          'id_username' => 'user_login',
          'id_password' => 'user_pass',
          'id_remember' => 'rememberme',
          'id_submit' => 'wp-submit',
          'label_username' => __('Username or Email Address'),
          'label_password' => __('Password'),
          'label_remember' => __('Remember Me'),
          'label_log_in' => __('Log In'),
          'value_username' => '',
          'value_remember' => false
      );
      wp_login_form($args);
  } else {
      echo '<h4>Welcome to the Community!</h4>';
  }
?>

Is it possible to construct a form in WordPress without using a plugin?

You must embed the code into your WordPress site functions.php to create a contact form without using a plug-in. This entails copying and pasting an auto-generated string of code into the text editor for your WordPress web page.

How can I change the URL of my WordPress login page without using plugins?

The wp-login.php file contains all of the code that creates the login page and manages the login procedure by default. In our new file, we may reuse the code from wp-login.php.

What is the URL for logging into WordPress?

To access the WordPress login page, add /login/, /admin/, or /wp-login.php to the end of your site’s URL.

Was this article helpful?
YesNo

Leave a comment

Your email address will not be published.