2

I have the following PHP function to fetch some user_ids which I would then like to add as recipients to a message as per below.

function true_my_bp_get_users_by_xprofile( $field_id_to_check, $num_to_find ) {
global $wpdb;


$table_name = $wpdb->prefix . "bp_xprofile_data";

$user_ids = $wpdb->get_results( 
     $wpdb->prepare( 
        "SELECT user_id FROM $table_name 
                 WHERE field_id = %d AND value LIKE '%%\"%d\"%%'", 
        $field_id_to_check,
        $num_to_find
    )
);
print_r($user_ids);
}

I am using true_my_bp_get_users_by_xprofile( 5, 18 ); which prints Array ( [0] => stdClass Object ( [user_id] => 1 ) [1] => stdClass Object ( [user_id] => 2 ) )

Then I have a HTML form with this code:

$body_input=isset($_POST['body_input'])?$_POST['body_input']:'';
$subject_input=isset($_POST['subject_input'])?$_POST['subject_input']:'';

send_msg( $user_ids,$body_input, $subject_input);

With send_msg being

function send_msg($user_id, $title, $message){
$args = array( 'recipients' => $user_id, 'sender_id' => bp_loggedin_user_id(), 'subject' => $title, 'content' => $message );
messages_new_message( $args );
}

What I want to do:

Take the array from $user_ids and put it here: 'recipients' => $user_id

I have tried replacing $user_id with $user_ids in the function but it doesn't work.

redditor
  • 4,196
  • 1
  • 19
  • 40
  • As a good practice; `global` variables are a bad practice. Wrap the logic in a class, make $wpdb a private class property. I know WP allows this. Also, does a plugin no already exist for this functionality? `Dont reinvent the wheel`. – David J Eddy Sep 26 '17 at 14:17

1 Answers1

1

Since you are putting data in the $user_ids variable inside a function, its scope is limited to that function only. The data can be stored and accessed outside the function in a couple of differetn ways.

1). Pass a variable to true_my_bp_get_users_by_xprofile by reference.

$user_ids = null;

function true_my_bp_get_users_by_xprofile( $field_id_to_check, $num_to_find, &$user_ids ) {
    global $wpdb;
    $table_name = $wpdb->prefix . "bp_xprofile_data";

    $user_ids = $wpdb->get_results( 
         $wpdb->prepare( 
            "SELECT user_id FROM $table_name 
                     WHERE field_id = %d AND value LIKE '%%\"%d\"%%'", 
            $field_id_to_check,
            $num_to_find
        )
    );
    print_r($user_ids);
}

Call the function

true_my_bp_get_users_by_xprofile( 5, 18, $user_ids );

Now your $user_ids has the data and accessable outside the function.

2). Return the $user_ids from true_my_bp_get_users_by_xprofile function

function true_my_bp_get_users_by_xprofile( $field_id_to_check, $num_to_find ) {
    global $wpdb;
    $table_name = $wpdb->prefix . "bp_xprofile_data";

    $user_ids = $wpdb->get_results( 
         $wpdb->prepare( 
            "SELECT user_id FROM $table_name 
                     WHERE field_id = %d AND value LIKE '%%\"%d\"%%'", 
            $field_id_to_check,
            $num_to_find
        )
    );
    print_r($user_ids);

    return $user_ids;
}

Calling the function like $user_ids = true_my_bp_get_users_by_xprofile( 5, 18 );

Now, you can call the send_msg function as you've done in your code above i.e.

send_msg( $user_ids, $body_input, $subject_input);
Junaid
  • 1,270
  • 3
  • 14
  • 33
  • That's really interesting, thank you. I guess they don't have to be two separate functions. Would there be a better way to to do it if I put `send_msg` inside `true_my_bp_get_users_by_xprofile`. Although I don't know how to do this. https://pastebin.com/irCZ8iXe This is the full code as is, it's a bit messy at the moment. – redditor Sep 26 '17 at 14:25
  • You can call the `send_msg` function from the first one but then you'll have to pass on `$body_input` and `$subject_input` to the first variable or access it with `global` and so on. I think returning `$user_ids` is the best way to go. – Junaid Sep 26 '17 at 14:55
  • Thanks, I went for the 2nd one. – redditor Sep 26 '17 at 15:35