functions - Why get_user_by() doesn't work in my code?

admin2025-04-19  0

I am editing the code of a plugin to add a small functionality that I need. I almost made it work, but I really struggle and don't understand why

$user_id_for_um_groups = get_user_by( 'email', $email );

doesn't work here.

I get 0 in the user_id2 column of my database (I need to get user ID).

I pasted the full code below, my added code is commented as "ADDED CODE".

I would be very grateful for your help, as I have spent a few days already trying to understand why it doesn't work. Thanks ;-)

<?php
public function bulk_add_users() {
    if ( isset( $_GET['action'] ) && 'bulk-add-users' === $_GET['action'] && isset( $_GET['group-id'] ) ) {
        $group_id       = absint( $_GET['group-id'] );
        $first_names    = $_REQUEST['first_name'];
        $last_names     = $_REQUEST['last_name'];
        $emails         = $_REQUEST['email'];
        $error_results  = [];
        $insert_results = [];
        if ( $emails ) {
            $role = apply_filters( 'uo-groups-user-role', get_option( 'default_role', 'subscriber' ) );
            foreach ( $emails as $k => $email ) {
                if ( ! empty( $email ) ) {
                    if ( is_email( $email ) ) {
                        $first       = $first_names[ $k ];
                        $last        = $last_names[ $k ];
                        $email       = sanitize_email( $email );
                        $is_existing = email_exists( $email );

                        if ( is_numeric( $is_existing ) ) {
                            $user_id     = $is_existing;
                            $user_groups = learndash_get_users_group_ids( $user_id, true );
                            if ( in_array( $group_id, $user_groups ) ) {
                                $error_results[] = sprintf( __( 'Line #%d: %s is existing user of group.', 'uncanny-learndash-groups' ), $k + 1, $email );
                                continue;
                            }
                            $user_data = array(
                                'user_email' => $email,
                                'user_id'    => $user_id,
                                'first_name' => $first,
                                'last_name'  => $last,
                                'role'       => $role,
                            );

                            if ( isset( $user_data['first_name'] ) && ! $is_existing ) {
                                update_user_meta( $user_id, 'first_name', $user_data['first_name'] );
                            }

                            if ( isset( $user_data['last_name'] ) && ! $is_existing ) {
                                update_user_meta( $user_id, 'last_name', $user_data['last_name'] );
                            }

                            $restApi = new RestApiEndPoints();
                            $restApi->add_existing_user( $user_data, false, $group_id, 0, 'not redeemed', false );

                        } else {
                            $user_data = array(
                                'user_login' => $email,
                                'user_email' => $email,
                                'first_name' => $first,
                                'last_name'  => $last,
                                'role'       => $role,
                                'group_id'   => $group_id,
                            );
                            $restApi   = new RestApiEndPoints();
                            $restApi->add_invite_user( $user_data, false, false, false );

                        }
                        $insert_results[] = sprintf( __( '%s added & invited successfully.', 'uncanny-learndash-groups' ), $email );

                    //ADDED CODE
                     global $wpdb;

                     $user_id_for_um_groups = get_user_by( 'email', $email );

                     $current_group_id_for_um_groups = $wpdb->get_var("SELECT ID FROM wpso_posts WHERE post_excerpt = $group_id");
                     $current_user_id_for_um_groups = get_current_user_id();
                     $wpdb->insert( 'wpso_um_groups_members', array( 
                     'group_id' => $current_group_id_for_um_groups,
                     'user_id1' => $user_id_for_um_groups,
                     'user_id2' => $current_user_id_for_um_groups,
                     'status' => 'approved',
                     'role' => 'member') );
                    //END OF ADDED CODE

                    } else {
                        $error_results[] = sprintf( __( 'Line #%d: Email (%s) not correct.', 'uncanny-learndash-groups' ), $k + 1, $email );
                    }
                } else {
                    if ( ! empty( $first_names[ $k ] ) || ! empty( $last_names[ $k ] ) ) {
                        $error_results[] = sprintf( __( 'Line #%d: Email field is empty.', 'uncanny-learndash-groups' ), $k + 1 );
                    }
                }
            }
        }
        $url = SharedFunctions::get_group_management_page_id( true );
        $url .= '?group-id=' . $group_id;
        $url .= '&bulk=1';
        if ( ! empty( $error_results ) ) {
            $url .= '&bulk-errors=' . urlencode( join( '<br /> ', $error_results ) );
        }
        if ( ! empty( $insert_results ) ) {
            $url .= '&success-invited=' . urlencode( join( '<br /> ', $insert_results ) );
        }

        wp_safe_redirect( $url );
        exit;
    }
}

I am editing the code of a plugin to add a small functionality that I need. I almost made it work, but I really struggle and don't understand why

$user_id_for_um_groups = get_user_by( 'email', $email );

doesn't work here.

I get 0 in the user_id2 column of my database (I need to get user ID).

I pasted the full code below, my added code is commented as "ADDED CODE".

I would be very grateful for your help, as I have spent a few days already trying to understand why it doesn't work. Thanks ;-)

<?php
public function bulk_add_users() {
    if ( isset( $_GET['action'] ) && 'bulk-add-users' === $_GET['action'] && isset( $_GET['group-id'] ) ) {
        $group_id       = absint( $_GET['group-id'] );
        $first_names    = $_REQUEST['first_name'];
        $last_names     = $_REQUEST['last_name'];
        $emails         = $_REQUEST['email'];
        $error_results  = [];
        $insert_results = [];
        if ( $emails ) {
            $role = apply_filters( 'uo-groups-user-role', get_option( 'default_role', 'subscriber' ) );
            foreach ( $emails as $k => $email ) {
                if ( ! empty( $email ) ) {
                    if ( is_email( $email ) ) {
                        $first       = $first_names[ $k ];
                        $last        = $last_names[ $k ];
                        $email       = sanitize_email( $email );
                        $is_existing = email_exists( $email );

                        if ( is_numeric( $is_existing ) ) {
                            $user_id     = $is_existing;
                            $user_groups = learndash_get_users_group_ids( $user_id, true );
                            if ( in_array( $group_id, $user_groups ) ) {
                                $error_results[] = sprintf( __( 'Line #%d: %s is existing user of group.', 'uncanny-learndash-groups' ), $k + 1, $email );
                                continue;
                            }
                            $user_data = array(
                                'user_email' => $email,
                                'user_id'    => $user_id,
                                'first_name' => $first,
                                'last_name'  => $last,
                                'role'       => $role,
                            );

                            if ( isset( $user_data['first_name'] ) && ! $is_existing ) {
                                update_user_meta( $user_id, 'first_name', $user_data['first_name'] );
                            }

                            if ( isset( $user_data['last_name'] ) && ! $is_existing ) {
                                update_user_meta( $user_id, 'last_name', $user_data['last_name'] );
                            }

                            $restApi = new RestApiEndPoints();
                            $restApi->add_existing_user( $user_data, false, $group_id, 0, 'not redeemed', false );

                        } else {
                            $user_data = array(
                                'user_login' => $email,
                                'user_email' => $email,
                                'first_name' => $first,
                                'last_name'  => $last,
                                'role'       => $role,
                                'group_id'   => $group_id,
                            );
                            $restApi   = new RestApiEndPoints();
                            $restApi->add_invite_user( $user_data, false, false, false );

                        }
                        $insert_results[] = sprintf( __( '%s added & invited successfully.', 'uncanny-learndash-groups' ), $email );

                    //ADDED CODE
                     global $wpdb;

                     $user_id_for_um_groups = get_user_by( 'email', $email );

                     $current_group_id_for_um_groups = $wpdb->get_var("SELECT ID FROM wpso_posts WHERE post_excerpt = $group_id");
                     $current_user_id_for_um_groups = get_current_user_id();
                     $wpdb->insert( 'wpso_um_groups_members', array( 
                     'group_id' => $current_group_id_for_um_groups,
                     'user_id1' => $user_id_for_um_groups,
                     'user_id2' => $current_user_id_for_um_groups,
                     'status' => 'approved',
                     'role' => 'member') );
                    //END OF ADDED CODE

                    } else {
                        $error_results[] = sprintf( __( 'Line #%d: Email (%s) not correct.', 'uncanny-learndash-groups' ), $k + 1, $email );
                    }
                } else {
                    if ( ! empty( $first_names[ $k ] ) || ! empty( $last_names[ $k ] ) ) {
                        $error_results[] = sprintf( __( 'Line #%d: Email field is empty.', 'uncanny-learndash-groups' ), $k + 1 );
                    }
                }
            }
        }
        $url = SharedFunctions::get_group_management_page_id( true );
        $url .= '?group-id=' . $group_id;
        $url .= '&bulk=1';
        if ( ! empty( $error_results ) ) {
            $url .= '&bulk-errors=' . urlencode( join( '<br /> ', $error_results ) );
        }
        if ( ! empty( $insert_results ) ) {
            $url .= '&success-invited=' . urlencode( join( '<br /> ', $insert_results ) );
        }

        wp_safe_redirect( $url );
        exit;
    }
}
Share Improve this question edited Nov 11, 2019 at 12:44 butlerblog 5,1213 gold badges28 silver badges44 bronze badges asked Nov 10, 2019 at 15:28 anastasia_pzanastasia_pz 32 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 1

I tweaked your code to make sure the $email was defined before attempting to process. I stop where the problem may be. If you follow all the if and else code, the "continue"resident when an email address is NOT a part of a particular group (subscriber, admin, customer etc) will skip the processing and move onto the next email. Thus your call may not be called and thus you will not get a result back.

I also added some important filters to your form entry at the top of the code snippet.

The only other path, is if the email is not defined.

 public function bulk_add_users() 
    {
     if ( isset( $_GET['action'] ) && 'bulk-add-users' === $_GET['action'] && isset( $_GET['group-id'] ) ) {
         $group_id       = sanitize_text_field(absint( $_GET['group-id']) );
         $first_names    = sanitize_text_field($_REQUEST['first_name']);
         $last_names     = sanitize_text_field($_REQUEST['last_name']);
         $emails         =  filter_var($_REQUEST['email'], FILTER_VALIDATE_EMAIL);  
         $error_results  = [];
         $insert_results = [];

        if ( $emails ) 
         {
           $role = apply_filters( 'uo-groups-user-role', get_option( 'default_role', 'subscriber' ) );
             foreach ( $emails as $k => $email ) 
            {
             if ( ! empty( $email ) ) 
               {
               if ( is_email( $email ) )
                  {
                   $first       = $first_names[ $k ];
                   $last        = $last_names[ $k ];
                   $email       = sanitize_email( $email );
                   $is_existing = email_exists( $email );
                   if ( is_numeric( $is_existing ) ) 
                     {
                     $user_id     = $is_existing;
                      $user_groups = learndash_get_users_group_ids( $user_id, true );

                      // THE POTENTIAL SKIP IS HERE. IF THE EMAIL IS NOT IN THE GROUP
                      if ( in_array( $group_id, $user_groups ) ) 
                      {
                      $error_results[] = sprintf( __( 'Line #%d: %s is existing user of group.', 'uncanny-learndash-groups' ), $k + 1, $email );

                      //*************IF YOU CONTINUE HERE, IT IS POSSIBLE TO NEVER REACH YOUR CODE
                       continue;     
                      }  // if ( in_array( $group_id, $user_groups ) 

                    //-------------------------------------
                    // Go here only if the email is in the group
                     $user_data = array(
                             'user_email' => $email,
                             'user_id'    => $user_id,
                             'first_name' => $first,
                             'last_name'  => $last,
                            'role'       => $role,
                        );
                    if ( isset( $user_data['first_name'] ) && ! $is_existing )
                      update_user_meta( $user_id, 'first_name', $user_data['first_name'] );

                    if ( isset( $user_data['last_name'] ) && ! $is_existing ) 
                            update_user_meta( $user_id, 'last_name', $user_data['last_name'] );

                        $restApi = new RestApiEndPoints();
                        $restApi->add_existing_user( $user_data, false, $group_id, 0, 'not redeemed', false );




                }   //  if ( is_numeric( $is_existing ) )
              else 
                {
                 $user_data = array(
                 'user_login' => $email,
                 'user_email' => $email,
                 'first_name' => $first,
                 'last_name'  => $last,
                 'role'       => $role,
                 'group_id'   => $group_id,
                 );
                 $restApi   = new RestApiEndPoints();
                 $restApi->add_invite_user( $user_data, false, false, false );

               } //  if ( is_numeric( $is_existing ) ) 

                $insert_results[] = sprintf( __( '%s added & invited successfully.', 'uncanny-learndash-groups' ), $email );


                //------------YOUR ADDED CODE
                // Not ever reached on a continue or if the email is not defined in the form
                 global $wpdb;

              $user_id_for_um_groups = get_user_by( 'email', $email );
              $current_group_id_for_um_groups = $wpdb->get_var("SELECT ID FROM wpso_posts WHERE post_excerpt = $group_id");
              $current_user_id_for_um_groups = get_current_user_id();
              $wpdb->insert( 'wpso_um_groups_members', array( 
              'group_id' => $current_group_id_for_um_groups,
              'user_id1' => $user_id_for_um_groups,
              'user_id2' => $current_user_id_for_um_groups,
              'status' => 'approved',
              'role' => 'member') );
             //----------- YOUR END OF ADDED CODE
              } 
            else
              $error_results[] = sprintf( __( 'Line #%d: Email (%s) not correct.', 'uncanny-learndash-groups' ), $k + 1, $email );

             }     // if ( is_email( $email ) ) 
          }        //  if ( ! empty( $email ) ) 
        }          //  foreach ( $emails as $k => $email ) 
     }            // if ( $emails ) 


      .............................
   } // public function bulk_add_users() 
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745013962a279976.html

最新回复(0)