Skip to content
Advertisement

Add result of php function to WP database

I have the following function to create a member_id:

add_action('user_register', 'generate_member_id');
function generate_member_id($user_id){
    $unique_id = 1000 + get_current_user_id();
    $chapter_id = fetch_chapter();
    $member_id = "FAL-" . $chapter_id . "-" . $unique_id;
    
    return $member_id;
}

It appears to work fine and I can call the function from a shortcode and get the correct value.

Now I want to add the returned value to a database and have tried using

  • update_user_meta()
  • $wpdb->update
  • $wpdb->insert
  • $wpdb->query all with necessary arguments but nothing seems to work.

I expected this to do it, but it just generates a generic error that I am not able to debug (I have a problem with error logging):

global $wpdb;
$users = $wpdb->get_results( "SELECT ID FROM $wpdb->users" );
$generated_id = generate_member_id()
if( $users ) {
    foreach ( $users as $user ) {
        update_user_meta( $user->ID, 'member_id', generate_member_id() );
    }
}

However if I change generate_member_id() to a string like 'memberId', it works and the database is updated. So I don’t know what I’m doing wrong. Why can’t I add’ the result of the generate_member_id() function and to a WP database?

EDIT

add_action('user_register', 'fetch_chapter');
function fetch_chapter(){
    $current_user = wp_get_current_user();
    $current_user_id = $current_user->ID;
    global $wpdb;
    $result = $wpdb->get_results('SELECT meta_value FROM usermeta WHERE meta_key = 'chapter_name' AND user_id = '. $current_user->ID .' LIMIT 1');
    if ($result[0]->meta_value == 'Peregrine') {
           $result[0]->meta_value = 'PG';
    }elseif ($result[0]->meta_value == 'Barbary') {
           $result[0]->meta_value = 'BB';
    } 
     return $result[0]->meta_value;
}

Advertisement

Answer

There are a few things I notice that could cause problems:

  1. You have this line that (1) has no ; at the end and (2) it doesn’t look like you need it anyway – you don’t use $generated_id anywhere:
    $generated_id = generate_member_id()
  1. You are not passing the user_id into generate_member_id for your users in the loop calling update_user_meta

  2. … but even if you did pass in the user_id, generate_member_id isn’t using it anyway – it is using the current user id. That is the wrong value for the users in your loop.

You need to fix your generate_member_id function to work with the user_id parameter. I assume you want this function to work without one too, so it still gets the get_current_user_id is no user id is passed in:

add_action('user_register', 'generate_member_id');
function generate_member_id($user_id){

    // If no user_id is passed in, use the current user id
    if (!$user_id) $user_id = get_current_user_id();

    $unique_id = 1000 + $user_id;
    $chapter_id = fetch_chapter_id();
    $member_id = "FAL-" . $chapter_id . "-" . $unique_id;
    
    return $member_id;
}

And then remove the erroneous $generated_id... line and pass the user_id to the generate_member_id in your update_user_meta:

global $wpdb;
$users = $wpdb->get_results( "SELECT ID FROM $wpdb->users" );
if( $users ) {
    foreach ( $users as $user ) {
        update_user_meta( $user->ID, 'member_id', generate_member_id($user->ID) );
    }
}
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement