the plugin is the best way to add more featured to WordPress and on theme change the feature will not be gone. In this tutorial, we are going to develop a WordPress login registration plugin with the shortcode. Now start step by step,
you can use the shortcode for the plugin
[CMWP_LOGIN redirect=
[CMWP_RESTRICTED login_page_id=
[CMWP_REGISTRATION user_type=
[CMWP_RESTRICTED login_page_id=
First Step:
Create a folder inside your plugin folder and try to make that unique as another plugin of WordPress do not conflict with the plugin
Example: codemystery_login
Second Step:
Create a PHP file as the same name as the folder created, here you can use the index.php also but it is good to create according to the folder name
Example: codemystery_login.php
Third Step:
Now open codemystery_login.php file in a text or code editor and describe the plugin under commenting, remember to give the name of the plugin unique as it also should not be same as any other plugin of WordPress, the plugin URI is the location of the plugin in server, the description is to describe the plugin, the version is to define the version number of the plugin, author is to define the author name of the plugin and author URI is the website or link of the author of the WordPress plugin.
Example:
Fourth Step:
Its the part for coding and arrange to start code
- 1. here I have created another folder to separate the code inside my codemystery_login (plugin folder) and given name include
- 2. create 4 PHP file in include folder (you can create more or less) and give the name to them as
- a. login_form.php to make the login form with shortcode,
- b. registration_form.php to make the registration form with shortcode,
- c. logout_url.php to get the logout URL using shortcode
- d. and restricted_page.php to make the pages cannot be reached without login
- 3. Now open the codemystery_login.php file and define as global some variable
- a. First, define the WordPress user as global using global $user_login;
- b. login redirect URL, login page URL, the directory path and will use them if not
sent from the WordPress shortcode the defining codes are
- 4. Now include the files you have created in the include folder
The code is:
Fifth Step:
Now open the file login_form.php and start to write the code
- a.
here CMWP_LOGIN is the shortcode and cm_login_form is the function to be called for the shortcode
- b. now make the function cm_login_form
function cm_login_form($page_ids){ $page_ids=shortcode_atts(array( "redirect" => CMWP_LOGIN_REDIRECT, "login_page_id" => CMWP_LOGIN_URL ), $page_ids); $output_login_form = cm_login_form_perform($page_ids); //send back text to replace shortcode in post return $output_login_form; } ?>
from the function we are calling another function and passing the shortcode variables values and here redirect is the shortcode variable and login_page_id also the shortcode and the values of the variable is passing through the function and if there are not any values then it will get the defined values.
- c. now make the cm_login_form_perform function and you have done the login form
or
true, 'redirect' => $redirect_uri, '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' => true ); // Calling the login form. wp_login_form( $args ); endif; } ?>
Sixth Step:
Open the registration_form.php file and write the code and the calling the shortcode and its related function and getting the shortcode variables and its values are the same as making login form located above.
CMWP_LOGIN_REDIRECT ), $page_ids); $output_login_form = cm_reg_form_perform($page_ids); //send back text to replace shortcode in post return $output_login_form; } ?> set_role( $user_type ); $user_id = wp_update_user( array( 'ID' => $user_id, 'user_url' => $user_url, 'first_name' => $first_name, 'last_name' => $last_name, 'description' => $description ) ); if ( is_wp_error( $user_id ) ) { $message = 'User created please login'; } else { $message = 'User created succesfully please login'; } }else{ $message = 'Username or email alrady exist'; } }else{ $message = 'Both Password not matched'; } } ?>
Seventh Step:
Now open the logout_url.php file and paste the code and edit according to your need
CMWP_LOGIN_URL ), $page_id); $output_login_form = cm_logout_link_perform($page_id); //send back text to replace shortcode in post return $output_login_form; } function cm_logout_link_perform($get_page_id){ //getting the login page id $login_page_uri_id = $get_page_id["login_page_id"]; //getting the login page url $login_page_uri = get_page_link($login_page_uri_id ); if ( is_user_logged_in() ) : ?>
Seventh Step:
Now open the restricted_page.php file and paste the code and edit according to your need
CMWP_LOGIN_URL ), $page_id); $output_login_form = cm_restricted_page_perform($page_id); //send back text to replace shortcode in post return $output_login_form; } function cm_restricted_page_perform($get_page_id){ //getting the login page id $login_page_uri_id = $get_page_id["login_page_id"]; //getting the login page url $login_page_uri = get_page_link($login_page_uri_id ); $status = true; if ( !is_user_logged_in() ) : ?>
And now you have created a login registration plugin for WordPress and it can be used at any theme just install and use the shortcode
And you can use the shortcode
[CMWP_LOGIN redirect=
[CMWP_RESTRICTED login_page_id=
[CMWP_REGISTRATION user_type=
[CMWP_RESTRICTED login_page_id=