1

I use admin pointers to show a "tour" of the dashboard. And when the user has taken the tour and pressed the Close button, it saves a value in 'dismissed_wp_pointers' under the specific user in 'wp_usermeta'. This means the user won't see the same "tour" over and over. Which is great! But I would like to create a button which allows me to clear this value. I don't want to delete 'dismissed_wp_pointers' or clear all it's values, I want to remove only the value called 'g_tour' when the button is pressed.

How would I acheive this?

EDIT: I tried this:

<a href="index.php?action=callfunction">Click</a>
<?php
if(isset($_GET['action']) && $_GET['action'] == 'callfunction') {
    // Get the dismissed pointers as saved in the database.
    $pointers = get_user_meta( $user_id, 'dismissed_wp_pointers', true );

    // Create an array by separating the list by comma.
    $pointers = explode( ',', $pointers );

    // Get the index in the array of the value we want to remove.
    $index = array_search( 'tour', $pointers );

    // Remove it.
    unset( $pointers[$index] );

    // Make the list a comma separated string again.
    $pointers = implode( ',', $pointers );

    // Save the updated value.
    update_user_meta( $user_id, 'dismissed_wp_pointers', $points );
}
?>

1 Answer 1

1

For some reason the dismissed pointers are just stored as a comma separated list. Not even a serialised array. So the way to remove an item from it would be to get the value, turn it into an array, remove the desired element, put back together, and save:

// Get the dismissed pointers as saved in the database.
$pointers = get_user_meta( $user_id, 'dismissed_wp_pointers', true );

// Create an array by separating the list by comma.
$pointers = explode( ',', $pointers );

// Get the index in the array of the value we want to remove.
$index = array_search( 'wp496_privacy', $pointers );

// Remove it.
unset( $pointers[$index] );

// Make the list a comma separated string again.
$pointers = implode( ',', $pointers );

// Save the updated value.
update_user_meta( $user_id, 'dismissed_wp_pointers', $points );

Just replace wp496_privacy with your pointer's ID.

An alternative is just to replace the string of the ID with an empty string:

pointers = get_user_meta( $user_id, 'dismissed_wp_pointers', true );

$pointers = str_replace( 'wp496_privacy', '', $pointers );

update_user_meta( $user_id, 'dismissed_wp_pointers', $points );

But if it's not the last item in the list then you could end up with values like:

wp390_widgets,,text_widget_custom_html

Which might not cause problems, but is sort of messing with the way WordPress will expect this value to look. You could always then just replace any double commas ,, with single commas , the same way, but you've then got to deal with a trailing comma on the end if it was the last value. So ultimately I just find the Array method much cleaner.

6
  • Fantastic reply! And I like how you explained it all. I tried using your function without any success. First as a regular function, but also triggered by a button click (check my first post for the code I used). I don't know why it doesn't work!
    – joq3
    May 31, 2018 at 13:28
  • Have you set the user ID? $user_id needs to be the actual ID of the user or a variable with the ID. May 31, 2018 at 13:36
  • Did not think of that. How do I get the current user?
    – joq3
    May 31, 2018 at 13:37
  • get_current_user_id() May 31, 2018 at 13:38
  • So sorry for the stupid questions! Now it works perfectly! Thank you for the great work!
    – joq3
    May 31, 2018 at 13:46

Your Answer

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

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