Today, you will learn how to create a custom login and register without a plugin in WordPress. There are so many different ways of customizing the WordPress login page and register page process. There are two functions that are very important for logging in and registering. The first method is to check the username and the password. If your username and password are wrong, then this method will show an error in your Browser; and the second method is to create new user information and save it into your WordPess database.
WordPress Function
- wp_signon – It helps to authenticate a user with the option to remember the credentials.
- wp_create_user – This function allows you to insert a new user into the WordPress database.
Example: Wordpress Two Method parameters.
wp_signon
- $login_data = array();
- $login_data['user_login'] = $username;
- $login_data['user_password'] = $password;
- $login_data['remember'] = $remember;
- $user_verify = wp_signon( $login_data, false );
wp_create_user
- <?php
-
-
-
- $username = $wpdb->escape($_REQUEST['username']);
-
- if (strpos($username, ' ') !== false)
- {
- $errors['username'] = "Sorry, no spaces allowed in usernames";
- }
-
- if (emptyempty($username))
- {
- $errors['username'] = "Please enter a username";
- }
- elseif (username_exists($username))
- {
- $errors['username'] = "Username already exists, please try another";
- }
-
-
-
- $email = $wpdb->escape($_REQUEST['email']);
-
- if (!is_email($email))
- {
- $errors['email'] = "Please enter a valid email";
- }
- elseif (email_exists($email))
- {
- $errors['email'] = "This email address is already in use";
- }
-
-
-
- if (0 === preg_match("/.{6,}/", $_POST['password']))
- {
- $errors['password'] = "Password must be at least six characters";
- }
-
-
-
- if (0 !== strcmp($_POST['password'], $_POST['password_confirmation']))
- {
- $errors['password_confirmation'] = "Passwords do not match";
- }
-
-
-
- if ($_POST['terms'] != "Yes")
- {
- $errors['terms'] = "You must agree to Terms of Service";
- }
-
- if (0 === count($errors))
- {
- $password = $_POST['password'];
- $new_user_id = wp_create_user($username, $password, $email);
-
-
-
- $success = 1;
- header('Location:' . get_bloginfo('url') . '/login/?success=1&u=' . $username);
- }
Let's start with the following steps.
Step 1: To create a template in WordrPess, Create PHP file like new –login.php.
Go to your active theme.
Open the active theme folder and save the PHP file which is created.
Open PHP file into PHP editor like Netbeans , Notepad++ etc.
Step 2: Open the file and write the template name, shown in the example, given below:
The changes required are:
Save it.
Step 3: Go to WordPress admin panel and create WordPress pages.
Click Pages-> Add New.
Click publish.
- <?php
-
-
-
- get_header();
-
-
- if($_POST)
- {
-
- global $wpdb;
-
-
- $username = $wpdb->escape($_REQUEST['username']);
- $password = $wpdb->escape($_REQUEST['password']);
- $remember = $wpdb->escape($_REQUEST['rememberme']);
-
- if($remember) $remember = "true";
- else $remember = "false";
-
- $login_data = array();
- $login_data['user_login'] = $username;
- $login_data['user_password'] = $password;
- $login_data['remember'] = $remember;
-
- $user_verify = wp_signon( $login_data, false );
-
- if ( is_wp_error($user_verify) )
- {
- echo "Invalid login details";
-
- } else
- {
- echo "<script type='text/javascript'>window.location.href='". home_url() ."'</script>";
- exit();
- }
-
- } else
- {
-
-
-
-
-
- }
- ?>
-
- <form id="login1" name="form" action="<?php echo home_url(); ?>/login/" method="post">
-
- <input id="username" type="text" placeholder="Username" name="username"><br>
- <input id="password" type="password" placeholder="Password" name="password">
- <input id="submit" type="submit" name="submit" value="Submit">
- </form>
- <?php get_footer(); ?>
The processing is shown below:
Fill in information
Click sign up button.
Go to the database and show the record.
Register file code