WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to do a task only once after an user has visited my site. I am currently doing like this:

function my_task(){
    //do my tasks
add_action('init', 'my_task');

I am doing this from a plugin. This actually loads my_task() function every time users load any page of wordpress. But I only want to load it once for each user for each session. Once they close the browser, and come back again, I want it to load again only for once till they exit.

How can I do this? Thanks in advance.

share|improve this question
up vote 2 down vote accepted

There are various ways to do this, but I've recently addressed a similar problem by using cookies.

add_action( 'init', 'wpse_93797_cookie' ); 
function wpse_93797_cookie() {
  if ( !isset ( $_COOKIE['wpse_93797_cookie_name'] ) ) {
    // Do your stuff.

    // Set your cookie so we don't do stuff the next time around.
    // Note that 0 makes the cookie expire at the end of the user's browser session.
    setcookie( 'wpse_93797_cookie_name', 'some-value-we-did-stuff-btw', 0, '/' );   

Manual for cookies on PHP.net

Also maybe helpful:

How can i set , get and destroy cookies in wordpress?

share|improve this answer
thanks, it solved my problems :) – тнє Sufi Mar 29 '13 at 12:59

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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