简体   繁体   中英

'Header already sent' Wordpress Plugin

I am trying to make a simple plugin that welcomes users when they visit the site for the first time but I keep getting a 'headers already sent' error.

I know this error has something to do with the cookie not being sent in the header. And I already found some solutions but none that work within the plugin. Can I do all this inside the plugin ? (I'm trying to write this inside a wordpress widget)

if (!isset($_COOKIE['visited'])) {
  setcookie ('visited', 'yes', time() + 3600);      
  echo "Welcome.";
}
else{                                               
  echo "Welcome back";
}

So if a user visits the site for the first time I want him to see 'Welcome' but if he is a returning visitor, I want to display 'welcome back',

Try use return instead of echo so the response is manage by the plugin

    if (!isset($_COOKIE['visited'])) {
      setcookie ('visited', 'yes', time() + 3600);      
      return  "Welcome.";
    }
    else{                                               
      return  "Welcome back";
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM