0
function theme_achievements() {

    $user_id = get_current_user_id();   
    if ( empty( $user_id ) ) {
        return false;
    }

    if( $value >= '0' && $value <= '499' ) { 
        add_user_meta( $user_id, 'author_level', array( 'lvl-0', '' ) );
    } elseif( $value >= '500' && $value <= '999' ) { 
        update_user_meta( $user_id, 'author_level', array( 'lvl-1', '' ) );
    } else {
        ...
    }

}

add_action( 'wp_head', 'theme_achievements' ); 

With the function above I created an custom user meta_key (based on levels).

While looking inside my wp database (phpMyAdmin) to see if the meta_key is created and update properly, I noticed multiple author_level entries that keep increasing everything I load the page.

Inside the database user_id, meta_key, and meta_value are all the same but each entry has an unique umeta_id. Is this normal? I prefer to have just one entry per user to keep the database "clean".

1 Answer 1

0

You can fix this in 1 of 2 ways:

  1. change the call to add_user_meta() to update_user_meta() when your 1st conditional evals to true. As explained in update_user_meta(), "If the meta field for the user does not exist, it will be added."
  2. pass true as the $unique (4th) param to add_user_meta(). As explained in add_user_meta(), the $unique param controls "whether the same key should not be added" (i.e., when true, if the meta already exists, another one will not be added; it defaults to false).

In most cases, which is preferred comes down to "style".

Edit

The same applies to add_post_meta() vs update_post_meta() and add_term_meta() vs update_post_meta().

Where #1 or #2 matters is if you've hooked into update_{$meta_type}_meta and expect it to always fire. In case #1 (i.e., always use update_user_meta()) the update_user_meta action will not fire if the meta does not already exist, since update_user_meta() will simply call add_user_meta() if the meta doesn't ready exist.

Hence, if you care about capturing meta additions, you should generally hook into both update_{$meta_type}_meta and add_{$meta_type}_meta, even if you always use update_user_meta().

2
  • So instead of using wp_head, it's better to use add_action( 'updated_user_meta', 'my_update_user_meta', 10, 4 );?
    – kiarashi
    Commented Mar 12, 2017 at 8:48
  • @kiarashi sorry, I didn't see your comment until now. No, you should be using the function update_user_meta(), not the filter. The reason I added the discussion on the filters was that IF you hooked into the update_user_meta action and expected it to be fired the first time update_user_meta() was called you'd be disappointed...you have to hook into add_user_meta action as well. But you don't need to hook into either of those actions...that discussion only applies IF you hook into ... Commented Mar 14, 2017 at 16:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.