最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to Check User type from the form user registers in user_register hook?

programmeradmin2浏览0评论

I am registering two types of users. Student and Teacher. Student is registered through student registration form (easy registration form plugin) and teacher through teacher registration form(also easy registration form plugin). Now I want to use the user_register hook and create a function that will add prevailing discounts from a table in the database. These will be stored in another table with user id. The discount is based on whether it is a teacher or student. Source table for teacher discount is different than the source table for student discount.

add_action( 'user_register', 'insertintodiscount', 10, 1);

function insertintodiscount( $user_id )
{   
   $user = get_user_by('id',$user_id);
   global $wpdb;
   $count_query ="SELECT * FROM discount_for_users WHERE userid='$user_id'";
   $results = $wpdb->get_results ( "SELECT * FROM discount_for_users WHERE userid='$user_id'" );
   $numrows = $wpdb->get_var($count_query);
   
   
   if( $user->has_cap( 'administrator') or $user->has_cap( 'author')) {
       $discountMonths = 0;
       $discountValue = 0;
       $disctype ='admin';
} else if(  metadata_exists( 'user', $user_id, 'studentmothermobile' )) {
   $disctype = 'student';
           $stdDiscount = $wpdb->get_results ( "SELECT * FROM student_discount WHERE status='active' LIMIT 1" );
           foreach ( $stdDiscount as $print ) 
           {
               $val=1;
               $discountMonths =$print->months;
               $discountValue =$print->discount;
           }
}
   else if ( metadata_exists( 'user', $user_id, 'teachdobmeta' ) ) {
       $disctype = 'teacher';
           $tearesult = $wpdb->get_results ( "SELECT * FROM teacher_discount WHERE status='active' LIMIT 1" );
           foreach ( $tearesult as $teachprint ) 
           {
               $val=1;
               $discountMonths =$teachprint->months;
               $discountValue =$teachprint->discount;
           }
    }
   else 
   {
       $discountMonths = 2;
       $discountValue = 10;
       $disctype ='user';
       
   }
       
         $table = 'discount_for_users';
         $data = array(
             'userid' => $curusderid,
             'discount' => $discountValue,
             'vaildity' => $discountMonths,
             'type' => $disctype,
         );
         $format = array(
             '%d',
             '%d',
             '%d',
             '%s',
         );
         $success=$wpdb->insert( $table, $data, $format );       
}


In this everytime I am registering, the data is going into the else condition. So it is not able to determine whether the user is student or teacher from the meta key values inserted. How to determine student or teacher from the different registration form they are registering and use it in this function? There meta keys are different.

I am registering two types of users. Student and Teacher. Student is registered through student registration form (easy registration form plugin) and teacher through teacher registration form(also easy registration form plugin). Now I want to use the user_register hook and create a function that will add prevailing discounts from a table in the database. These will be stored in another table with user id. The discount is based on whether it is a teacher or student. Source table for teacher discount is different than the source table for student discount.

add_action( 'user_register', 'insertintodiscount', 10, 1);

function insertintodiscount( $user_id )
{   
   $user = get_user_by('id',$user_id);
   global $wpdb;
   $count_query ="SELECT * FROM discount_for_users WHERE userid='$user_id'";
   $results = $wpdb->get_results ( "SELECT * FROM discount_for_users WHERE userid='$user_id'" );
   $numrows = $wpdb->get_var($count_query);
   
   
   if( $user->has_cap( 'administrator') or $user->has_cap( 'author')) {
       $discountMonths = 0;
       $discountValue = 0;
       $disctype ='admin';
} else if(  metadata_exists( 'user', $user_id, 'studentmothermobile' )) {
   $disctype = 'student';
           $stdDiscount = $wpdb->get_results ( "SELECT * FROM student_discount WHERE status='active' LIMIT 1" );
           foreach ( $stdDiscount as $print ) 
           {
               $val=1;
               $discountMonths =$print->months;
               $discountValue =$print->discount;
           }
}
   else if ( metadata_exists( 'user', $user_id, 'teachdobmeta' ) ) {
       $disctype = 'teacher';
           $tearesult = $wpdb->get_results ( "SELECT * FROM teacher_discount WHERE status='active' LIMIT 1" );
           foreach ( $tearesult as $teachprint ) 
           {
               $val=1;
               $discountMonths =$teachprint->months;
               $discountValue =$teachprint->discount;
           }
    }
   else 
   {
       $discountMonths = 2;
       $discountValue = 10;
       $disctype ='user';
       
   }
       
         $table = 'discount_for_users';
         $data = array(
             'userid' => $curusderid,
             'discount' => $discountValue,
             'vaildity' => $discountMonths,
             'type' => $disctype,
         );
         $format = array(
             '%d',
             '%d',
             '%d',
             '%s',
         );
         $success=$wpdb->insert( $table, $data, $format );       
}


In this everytime I am registering, the data is going into the else condition. So it is not able to determine whether the user is student or teacher from the meta key values inserted. How to determine student or teacher from the different registration form they are registering and use it in this function? There meta keys are different.

Share Improve this question edited Feb 26, 2021 at 10:13 fuxia 107k38 gold badges255 silver badges459 bronze badges asked Feb 26, 2021 at 8:48 Pushkal SinghPushkal Singh 111 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

This is just an alternative answer and not the expected answer to the question. I was searching for ways to find which user is teacher or student from the registration form they register. From what I understand, it can be done through checking the form metakey relationship in the user_meta table. This is a way to find which data is stored for teacher and which for student. Using this we find out the teacher and student. In my question I was using a function in user_register hook to get the userid of the user immediately after the user registers. But this has a problem.

Not all meta data is accessible using the user_meta table in this function. This I got from this question of stackoverflow How to get user_meta value for new user regsitered?

The alternative solution that I suggest is to add the discount for user on first login. This is done to get access to the user_meta data completely and also find which user is teacher and which user is student. Here is the code for this:

add_action( 'user_register', 'function_new_user');

function function_new_user($user_id) { 
   add_user_meta( $user_id, '_new_user', '1' );
}

add_action('wp_login', 'function_check_login_redirect', 10, 2);


function function_check_login_redirect($user_login, $user) {
   $logincontrol = get_user_meta($user->ID, '_new_user', 'TRUE');
if ( $logincontrol ) 
{
    $user_id = $user->ID;
    update_user_meta( $user->ID, '_new_user', '0' );
    global $wpdb;
    $count_query ="SELECT * FROM discount_for_users WHERE userid='$user_id'";
    $results = $wpdb->get_results ( "SELECT * FROM discount_for_users WHERE userid='$user_id'" );
    $numrows = $wpdb->get_var($count_query);
    
    if( $user->has_cap( 'administrator') or $user->has_cap( 'author')) 
    {
        $discountMonths = 0;
        $discountValue = 0;
        $disctype ='admin';
    } 
    elseif( metadata_exists( 'user', $user_id, 'studentmothermobile' )) 
    {
        $disctype = 'student';
            $stdDiscount = $wpdb->get_results ( "SELECT * FROM student_discount WHERE status='active' LIMIT 1" );
            foreach ( $stdDiscount as $print ) 
            {
                $val=1;
                $discountMonths =$print->months;
                $discountValue =$print->discount;
            }
    }
    elseif ( metadata_exists( 'user', $user_id, 'teachdobmeta' ) ) 
    {
        $disctype = 'teacher';
            $tearesult = $wpdb->get_results ( "SELECT * FROM teacher_discount WHERE status='active' LIMIT 1" );
            foreach ( $tearesult as $teachprint ) 
            {
                $val=1;
                $discountMonths =$teachprint->months;
                $discountValue =$teachprint->discount;
            }
    }
    else 
    {
        $discountMonths = 0;
        $discountValue = 0;
        $disctype ='user';
        
    }
        
          $table = 'discount_for_users';
          $data = array(
              'userid' => $user_id,
              'discount' => $discountValue,
              'vaildity' => $discountMonths,
              'type' => $disctype,
          );
          $format = array(
              '%d',
              '%d',
              '%d',
              '%s',
          );
          $success=$wpdb->insert( $table, $data, $format ); 
      
       
   }
}

In the solution we add value 1 to the user meta data on registering and update it to 0 on first login. There is condition which checks whether the meta data value is 1 and run the function. So it runs the function first time only and we are able to add discount associated with teacher and student based on the type using this method. Please note this is alternate solution and if there is a direct solution which can solve this problem, please provide it. It will be helpful. Our first priority is to add discount on registration and not on first login.

发布评论

评论列表(0)

  1. 暂无评论