Make WordPress Core

Changes between Initial Version and Version 1 of Ticket #49809, comment 3


Ignore:
Timestamp:
04/09/2020 10:27:44 AM (4 years ago)
Author:
egyptimhotep
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #49809, comment 3

    initial v1  
    1 Guys, I am very sorry. My bad.
    2 Thank you for support.
     1Can someone give exaplanation? I need to add post_id value to array, to user meta, but sometimes I get to values into array, and the page was reloaded only one time. It added on every refresh.
     2
     3{{{#!php
     4<?php
     5// adds the watched ID to the users meta so they can't watch it again
     6function video_history_of_user($user_id, $post_id) {
     7
     8        // get options as array, update if is array and save it as option again
     9        $watched = get_user_meta('video_history', $user_id, true);
     10
     11        if (is_array($watched)) {
     12
     13                // if history array contain post id, remove it from history
     14                if (($key = array_search($post_id, $watched)) !== false) {
     15                    unset($watched[$key]);
     16                }
     17
     18                $watched[] = $post_id;
     19
     20        }else{
     21                $watched = array($post_id);
     22        }
     23
     24        update_user_meta($user_id, 'video_history', $watched);
     25       
     26}
     27}}}
     28
     29
     30Is it bugg or it is my bad.
     31
     32Thank you.
     33