26 February 2021

How to create login form(Custom login form) in wordpress without plugin?

Create login form(Custom login form)  in wordpress without plugin.



<?php
 /* Template Name: login */
 ob_start();
get_header();
?>


<div id="primary" class="content-area">
                <main id="main" class="site-main" role="main">
                                <?php
                                // Start the loop.
                                while ( have_posts() ) : the_post();
                                                // Include the page content template.
                                                get_template_part( 'template-parts/content', 'page' );
                                                // If comments are open or we have at least one comment, load up the comment template.
                                                if ( comments_open() || get_comments_number() ) {
                                                                comments_template();
                                                }
                                                // End of the loop.
                                endwhile;
                                ?>
                </main><!-- .site-main -->
           
</div><!-- .content-area -->
 
<?php

/* if (!$user_ID) {  
   //All code goes in here.  
}  
else {  
   wp_redirect( home_url() ); exit;  
}
 */


?>

<?php
  
if($_POST) 
{  

    global $wpdb;  
   global $username; 
   echo $username = $_POST['username'];  
    echo $password = $_POST['password'];  
 
    $login_data = array();  
    $login_data['user_login'] = $username;  
    $login_data['user_password'] = $password;  

    $user = wp_signon($login_data, false);   
     
    if ( is_wp_error($user) )   
    {  
        echo "Invalid login details";  
       
     } else 
    {   

 $userID = $user->ID;
       wp_clear_auth_cookie();
       wp_set_current_user($userID);
       wp_set_auth_cookie($userID);
   echo 'working';
       $redirect_to = user_admin_url();
       wp_safe_redirect( user_admin_url() );
       exit(); 
}  
}
 ?>  
  
<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(); 

?>



No comments:

Post a Comment

How to add re-captcha v3 on all Elementor forms using coding?

 Add re-captcha v3 on all Elementor forms using coding add_action('wp_footer',function(){     ?> <script src="https://www...