27 February 2021

How to create meta or custom field in database in Wordpress without a plugin?

Create meta or custom field in database in WordPress without a plugin


For Create

                        update_user_meta( $user, 'person_name', false);

update_user_meta( $user, 'phone_no', false);

update_user_meta( $user, 'city',false );

For Update

                        update_user_meta( $user, 'person_name', $_POST['person_name'] );

update_user_meta( $user, 'phone_no', $_POST['phone_no'] );

update_user_meta( $user, 'city', $_POST['city'] );

How to update user profile email in wordpress.

update user profile email in wordpress (wp_update_user())


 $args = array(

    'ID'         => $user_id,

    'user_email' => esc_attr( $useremail )

);

wp_update_user( $args );

How to create custom error in Wordpress without a plugin?

 Create custom error in WordPress without a plugin

    

 global $reg_errors;

    $reg_errors = new WP_Error;

    $person_name=$_POST['person_name'];

    $useremail=$_POST['useremail'];

    $password=$_POST['password'];

    

    

    if(empty( $useremail ) || empty($password))

    {

        $reg_errors->add('field', 'Required form field is missing');

    }    

 

    if ( !is_email( $useremail ) )

    {

        $reg_errors->add( 'email_invalid', 'Email id is not valid!' );

    }

    

    if ( email_exists( $useremail ) )

    {

        $reg_errors->add( 'email', 'Email Already exist!' );

    }

    if ( 5 > strlen( $password ) ) {

        $reg_errors->add( 'password', 'Password length must be greater than 5!' );

    }

    

    if (is_wp_error( $reg_errors ))

    { 

        foreach ( $reg_errors->get_error_messages() as $error )

        {

             $signUpError='<p style="color:#FF0000; text-aling:left;"><strong>ERROR</strong>: '.$error . '<br /></p>';

        } 

    }

    

    

    if ( 1 > count( $reg_errors->get_error_messages() ) )

    {



}



  <?php if(isset($signUpError)){echo '<div>'.$signUpError.'</div>';}?>

If user is not login in wordpress And redirect to another page.

If the user is not login in to WordPress And redirects to another page. 


<?php


if(!is_user_logged_in()) {

    wp_redirect( site_url().'/login' );

}

 

?>


<a href="<?php echo wp_logout_url(site_url().'/login'); ?>">Logout</a>

How to remove top toolbar from subscribers in Wordpress?

Remove a top toolbar from subscribers in WordPress.


add_action('after_setup_theme', 'remove_admin_bar');

function remove_admin_bar() {

if (!current_user_can('administrator') && !is_admin()) {

  show_admin_bar(false);

}

}

How create(Add) new custom fields in profile in wordpress without plugin?

 create(Add) new custom fields in profile in wordpress without plugin

function fb_add_custom_user_profile_fields( $user ) {

?>

<h3><?php _e('Extra Profile Information', 'your_textdomain'); ?></h3>

<table class="form-table">

<tr>

<th>

<label for="phone_no"><?php _e('phone_no', 'your_textdomain'); ?>

</label></th>

<td>

<input type="text" name="phone_no" id="phone_no" value="<?php echo esc_attr( get_the_author_meta( 'phone_no', $user->ID ) ); ?>" class="regular-text" /><br />

<span class="description"><?php _e('Please enter your phone no.', 'your_textdomain'); ?></span>

</td>

</tr>

<tr>

<th>

<label for="city"><?php _e('city', 'your_textdomain'); ?>

</label></th>

<td>

<input type="text" name="city" id="city" value="<?php echo esc_attr( get_the_author_meta( 'city', $user->ID ) ); ?>" class="regular-text" /><br />

<span class="description"><?php _e('Please enter your city address.', 'your_textdomain'); ?></span>

</td>

</tr>

<tr>

<th>

<label for="dateofbirth"><?php _e('dateofbirth', 'your_textdomain'); ?>

</label></th>

<td>

<input type="date" name="dateofbirth" id="dateofbirth" value="<?php echo esc_attr( get_the_author_meta( 'dateofbirth', $user->ID ) ); ?>" class="regular-text" /><br />

<span class="description"><?php _e('Please enter your Date of birth.', 'your_textdomain'); ?></span>

</td>

</tr>

</table>

<?php }


function fb_save_custom_user_profile_fields( $user_id ) {

if ( !current_user_can( 'edit_user', $user_id ) )

return FALSE;

update_user_meta( $user_id, 'phone_no', $_POST['phone_no'] );

update_user_meta( $user_id, 'city', $_POST['city'] );

update_user_meta( $user_id, 'dateofbirth', $_POST['dateofbirth'] );

}


add_action( 'show_user_profile', 'fb_add_custom_user_profile_fields' );

add_action( 'edit_user_profile', 'fb_add_custom_user_profile_fields' );


add_action( 'personal_options_update', 'fb_save_custom_user_profile_fields' );

add_action( 'edit_user_profile_update', 'fb_save_custom_user_profile_fields' );

26 February 2021

How to create registration form in Wordpress without plugin?

Create registration form in Wordpress without plugin.

 

<?php

 /* Template Name: registration */

 

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

global $wpdb, $user_ID;  

if (!$user_ID) {  

   //All code goes in here.  

}  

else {  

   wp_redirect( home_url() ); exit;  

}




?>


<?php

$info = '';

if (isset($_POST['user_registeration']))

{

    //registration_validation($_POST['username'], $_POST['useremail']);

    global $reg_errors;

    $reg_errors = new WP_Error;

    $username=$_POST['username'];

    $useremail=$_POST['useremail'];

    $password=$_POST['password'];

    

    

    if(empty( $username ) || empty( $useremail ) || empty($password))

    {

        $reg_errors->add('field', 'Required form field is missing');

    }    

    if ( 6 > strlen( $username ) )

    {

        $reg_errors->add('username_length', 'Username too short. At least 6 characters is required' );

    }

    if ( username_exists( $username ) )

    {

        $reg_errors->add('user_name', 'The username you entered already exists!');

    }

    if ( ! validate_username( $username ) )

    {

        $reg_errors->add( 'username_invalid', 'The username you entered is not valid!' );

    }

    if ( !is_email( $useremail ) )

    {

        $reg_errors->add( 'email_invalid', 'Email id is not valid!' );

    }

    

    if ( email_exists( $useremail ) )

    {

        $reg_errors->add( 'email', 'Email Already exist!' );

    }

    if ( 5 > strlen( $password ) ) {

        $reg_errors->add( 'password', 'Password length must be greater than 5!' );

    }

    

    if (is_wp_error( $reg_errors ))

    { 

        foreach ( $reg_errors->get_error_messages() as $error )

        {

             $signUpError='<p style="color:#FF0000; text-aling:left;"><strong>ERROR</strong>: '.$error . '<br /></p>';

        } 

    }

    

    

    if ( 1 > count( $reg_errors->get_error_messages() ) )

    {

        // sanitize user form input

        global $username, $useremail;

        $username   =   sanitize_user( $_POST['username'] );

        $useremail  =   sanitize_email( $_POST['useremail'] );

        $password   =   esc_attr( $_POST['password'] );

        

        $userdata = array(

            'user_login'    =>   $username,

            'user_email'    =>   $useremail,

            'user_pass'     =>   $password

            );

        $user = wp_insert_user( $userdata );

if($user){

$info = 'You have been registered successfully';

}

    }


}

?>


 



  <?php echo $info; ?>


 <h3>Create your account</h3>

    <form action="" method="post" name="user_registeration">

        <label>Username <span class="error">*</span></label>  

        <input type="text" name="username" placeholder="Enter Your Username" class="text" required /><br />

        <label>Email address <span class="error">*</span></label>

        <input type="text" name="useremail" class="text" placeholder="Enter Your Email" required /> <br />

        <label>Password <span class="error">*</span></label>

        <input type="password" name="password" class="text" placeholder="Enter Your password" required /> <br />

        <input type="submit" name="user_registeration" value="SignUp" />

    </form>

    <?php if(isset($signUpError)){echo '<div>'.$signUpError.'</div>';}?>




<?php get_footer(); ?>



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(); 

?>



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...