1

How do I add a value to the key dismissed_wp_pointers without removing the existing values (if there are any).

Doing this will put the value there, but it will remove everything existing first.

function reset_after_customize() {
    $user_id = get_current_user_id();
    update_user_meta( $user_id, 'dismissed_wp_pointers', 'welcome_tour' );
}

I don't understand how to add the value to the existing values/value.

1 Answer 1

1

update_user_meta( int $user_id, string $meta_key, mixed $meta_value, mixed $prev_value = '' ) updates existing user meta based on user_id and meta key. If there are many fields with the same key, you can pass `prev_value' to tell which field you want to update.

add_user_meta( int $user_id, string $meta_key, mixed $meta_value, bool $unique = false ) adds meta data to a user. It's last param is unique and it tells whether the same key should not be added (default value is false, so many fields will be created).

On the other hand, if you want to store list o values as one meta, then you'll have to use arrays or comma separated list.

If we're talking about dismissed pointers, then we want to use comma separated list, since that is how WP stores the pointers:

$dismissed = array_filter( explode( ',', (string) get_user_meta( get_current_user_id(), 'dismissed_wp_pointers', true ) ) );

if ( ! in_array( $new_pointer, $dismissed ) ) {
    $dismissed[] = $new_pointer;
    $dismissed = implode( ',', $dismissed );

    update_user_meta( get_current_user_id(), 'dismissed_wp_pointers', $dismissed );
}
4
  • But this is only for limiting the creation of a new key? I want to update the one that is already there. Adding to the other values which already are there.
    – joq3
    Commented Jun 4, 2018 at 14:34
  • So you want to store list of values as value for user meta? Commented Jun 4, 2018 at 14:35
  • 1
    I want to add a specified value (welcome_tour) to the existing dismissed_wp_pointers. And if there already are values in it, i want to keep them and add mine to them.
    – joq3
    Commented Jun 4, 2018 at 14:37
  • 1
    @joq3 OK, I've updated my answer Commented Jun 4, 2018 at 14:46

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.