Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

1 Answer 1

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

 
discard

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.