WordPress – generate random id each time a post is loaded

CONTEXT

I have an index page that displays a list of posts. In addition to having each post identified by a unique ID, I would like each viewing session of a post (i.e. a specific user views a specific post at a specific time) to be unique.

Read More

PROBLEM

Is there an easy way to generate a unique ID that identifies the current single-post viewing session, on post load?

Related posts

1 comment

  1. I usually use this to generate a 40 char random code. But I guess it depends what you want to use it for.

    $new_key = substr(md5(microtime()),0,40);

    If it needs to be unique, then I’d check that it’s not currently in use by using a temporary DB.

    Alternatively, you can probably use a GUID or UUID.

Comments are closed.