Why get_user_by() doesn’t work in my code?

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 and I get 0 in the user_id2 column of my database (I need to get user ID).

I paste 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;
}
}

Read more here:: Why get_user_by() doesn’t work in my code?

Leave a Reply

Your email address will not be published. Required fields are marked *