我使用管理指针来显示仪表板的“巡回”。当用户完成浏览并按下“关闭”按钮时,它会在“wp_usermeta”中的“dismissed_wp_pointers”中保存一个特定用户的值。这意味着用户不会一遍又一遍地看到相同的“旅游”。太棒了!但是我想要创建一个按钮,让我可以清除这个值。我不想删除'dismissed_wp_pointers‘或清除它的所有值,我只想删除被按下按钮时称为'g_tour’的值。
我该怎么做?
编辑:我试过这样做:
Click发布于 2018-05-31 13:10:10
出于某种原因,将被删除的指针存储为逗号分隔的列表。甚至连一个串行化数组都没有。因此,从其中移除项的方法是获取值,将其转换为数组,删除所需的元素,将其放在一起并保存:
// 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 );只需用指针的ID替换wp496_privacy即可。
另一种方法是将ID的字符串替换为空字符串:
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 );但是,如果它不是列表中的最后一项,那么您可能会得到如下的值:
wp390_widgets,,text_widget_custom_html这可能不会导致问题,但却在某种程度上扰乱了WordPress期望该值的外观。然后,您可以使用相同的方式将任何双逗号,,替换为单个逗号,,但是如果它是最后一个值,则必须在结尾处处理一个尾逗号。所以最终我发现Array方法要干净得多。
https://wordpress.stackexchange.com/questions/304945
复制相似问题