0

Pardon me if this is ultrabasic, I'm fairly green to all this.

I'm trying to fetch a string value (chapter name) from a wordpress database, use a php function to rename the value and then concatenate the renamed value with some other variables to create a member_id.

Below is my current code (added to functions.php file) and I have 2 problems with it:

  1. From the 1st function, when I use the shortcode [member_chapter] in a page/post I always get PG no matter what value of chapter name was used to register. If I switch the order of the foreach statements, then I always get BB, i.e. I always get the return value of the first if statement no matter what chapter name is actually in the database.
  2. From the 2nd function I never get the chapter_id, instead the result is always like FAL--2020-1007, i.e. it doesn't add the chapter_id value generated from the 1st function

I'd really appreciate any help.


/**
 * Fetch chapter name and return as 2-character chapter_id 
 * Create shortcode for viewing chapter_id 
 */
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);
    foreach($result as $row){
        if ($row->meta_value = 'Peregrine') {
            return $row->meta_value = 'PG';
        }elseif ($row->meta_value = 'Barbary') {
            return $row->meta_value = 'BB';
        }else { 
            return $row->meta_value;
        }
    }
}
add_shortcode('member_chapter', 'fetch_chapter'); 

/**
 * Generate unique_id
 * Retrieve chapter_id
 * Create member_id
 * Add member_id to user_meta table
 */
add_action('user_register', 'generate_member_id');
function generate_member_id($user_id){
    $unique_id = 1000 + $user_id;
    $chapter_id = fetch_chapter();
    $member_id = "FAL-" . $chapter_id . "-" . date("Y") . "-" . $unique_id;
    update_user_meta($user_id, 'member_id', $member_id);
}

Revised snippet

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

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 . "-" . date("Y") . "-" . $unique_id;
    return $member_id;
}

add_shortcode('member_id', 'generate_member_id'); 

eneimi
  • 37
  • 5

1 Answers1

0

You have several mistakes! Please read more about program languages before writing programs. See PHP Tutorial

In first function you used assignment operator (which is =) instead of comparison operator (which is == or ===). You must change it everywhere use if in your code. Furthermore, You use return function inside foreach. it means your foreach loop runs just once and then return. You must use just if instead of foreach and use Limit 1 in your sql. Or do whatever to do in your loop and avoid to else return blahblah. Your second problem related to your first because it probably $row->meta_value is null for your first record returned.

Majid Hajibaba
  • 3,105
  • 6
  • 23
  • 55
  • I replaced the assignment operators with comparisons. And the 1st issue seems to be fixed now. I'm still a bit stumped on how to resolve the second issue. There's now just one return statement at the end, removed foreach and added LIMIT 1 to the query, but I've definitely gotten it wrong because the returned value is just ARRAY. – eneimi Aug 26 '20 at 21:29
  • You must use $result[0] instead of $row. And check in if statement whether result has any member or not. – Majid Hajibaba Aug 27 '20 at 05:37
  • Thanks @majidh, please see the revised snippet in my original post. I get the correct 2-character meta_value even with additional elseif statements. But the second problem persists - the result of the 1st function isn't added to the 2nd function – eneimi Aug 27 '20 at 08:10
  • You missed the `return` keyword! No need to thanks. Just upvote and accept if your problem resolved. – Majid Hajibaba Aug 27 '20 at 11:51
  • I just don't see it @majidh. I have the return statement in place `return $result[0]->meta_value;`. The returned value gets to the frontend (with shortcode) but not to the 2nd function. – eneimi Aug 27 '20 at 14:06
  • What if meta_value not equal to PG or BB?? Try to print your result value by `var_dump(result)` too what you recieved from database. – Majid Hajibaba Aug 27 '20 at 15:16
  • Those are the only 2 values in the DB. I simplified it just to first get things working. After some additional tinkering see the revised code snippet @majidh. With `add_shortcode('member_id', 'generate_member_id'); ` I now see the correct member_id in the frontend. But how to add that value to the user_meta table in the database? – eneimi Aug 27 '20 at 22:42
  • I didn't understand! If member_id is correct means your function returns right value? What you need to do exactly? – Majid Hajibaba Aug 28 '20 at 07:27
  • Yes, the right value is returned now @majidh, that's why I marked the question as answered. But I had to remove `update_user_meta($user_id, 'member_id', $member_id);` from the function. That means I don't have member_id value stored in my database, only displayed in frontend. How do I store the value to my database? – eneimi Aug 28 '20 at 12:22
  • You can simply use `insert into..` or `update .. set ...` like as your select. See https://stackoverflow.com/a/13936483/2527458 – Majid Hajibaba Aug 28 '20 at 19:09