Save users last visited blogs within whole network

I have a big network with subdomains (many of which use domains).
I thought about solving this problem with cookies but after some research I don’t think that crossdomain cookies is a best Idea.

So I imagine this happing by saving in users meta some array of blogs and then displaying them on main page.

Read More

I came across update_user_meta() which is very handy in my case.

global $blog_id;
$current_blog_details = get_blog_details( array( 'blog_id' => $blog_id ) );

if (is_user_logged_in()) {
    $user = wp_get_current_user();
    update_user_meta($user->ID, 'last_visited_blogs',  $current_blog_details->blogname);
}

I save this function inside admin-bar.php so every blog shares this code.
Now I want to remake this code to use save 5 last visited blogs as an array and then get those values and display on some place.

First question would be how to save 5 sites.
I guess update is not my case no?

Related posts

Leave a Reply

1 comment

  1. Thank you for your attantion, I made it working with update_user_meta
    Here is my little function I wrote

    global $blog_id;
    $current_blog_details = get_blog_details( array( 'blog_id' => $blog_id ) );
    
    if (is_user_logged_in()) {
        $user = wp_get_current_user();
    
        $last_visited_blogs = get_user_meta($user->ID, 'last_visited_blogs',true);
        if($last_visited_blogs) {
            if (end($last_visited_blogs) != $current_blog_details->blogname) {
                if (count($last_visited_blogs) < 5) {
                    array_push($last_visited_blogs, $current_blog_details->blogname);
                } else {
                    array_shift($last_visited_blogs);
                    array_push($last_visited_blogs, $current_blog_details->blogname);
                }
                update_user_meta($user->ID, 'last_visited_blogs', $last_visited_blogs);
            }
        } else {
            $last_visited_blogs = array($current_blog_details->blogname);
            update_user_meta($user->ID, 'last_visited_blogs', $last_visited_blogs);
        }
        $last_visited_blogs_reversed = array_reverse($last_visited_blogs);
        foreach($last_visited_blogs_reversed as $key => $value) {
            $wp_admin_bar->add_menu( array(
                'id'    => $value,
                'title' => $value,
                'href'  => admin_url( 'didiyle.php' ),
            ) );
        }
    }
    

    I use it in my admin-bar, if you need you can use it anywhere, needs a little modifications though