5 Replies - 555 Views - Last Post: 17 September 2013 - 03:32 AM Rate Topic: -----

#1 Keylogger  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 343
  • Joined: 14-February 11

Wordpress create function to clear cookies

Posted 17 September 2013 - 02:46 AM

Hi guys,

I want to create a function to clear the posts password protected, since it stays to long.
I went to functions.php, and then I wrote this:

add_shortcode('clear__', 'clear__cookies');
function clear__cookies() {
  setcookie('wp-postpass_' . COOKIEHASH, '', time()-3600);
}


And then in the form, I just added this:
[clear__ foo="clear it"]Some text input[/clear__]


But it's giving me an error about headers already sent.
How can I solve this? Any better approach? I need a button/link to call a function to clear the cookies.

Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Wordpress create function to clear cookies

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3485
  • View blog
  • Posts: 10,045
  • Joined: 08-June 10

Re: Wordpress create function to clear cookies

Posted 17 September 2013 - 02:53 AM

Quote

But it's giving me an error about headers already sent.

of course. headers (and cookies are part of the HTTP headers) must be sent before you start sending output (is COOKIEHASH variable?). execute this function before WP starts printing the page.
Was This Post Helpful? 0
  • +
  • -

#3 Keylogger  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 343
  • Joined: 14-February 11

Re: Wordpress create function to clear cookies

Posted 17 September 2013 - 02:57 AM

Seems that is a global variable, and it's needed..I already saw the value..it's an hash.

Well, I put my function in the top of the file functions.php, but still doesn't work.

Warning: Cannot modify header information - headers already sent by (output started at /home/allfiles/public_html/website/wp-content/themes/default/header.php:5) in /home/allfiles/public_html/website/wp-content/themes/default/functions.php

This post has been edited by Keylogger: 17 September 2013 - 02:57 AM

Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3485
  • View blog
  • Posts: 10,045
  • Joined: 08-June 10

Re: Wordpress create function to clear cookies

Posted 17 September 2013 - 03:00 AM

Quote

Seems that is a global variable, and it's needed..I already saw the value..it's an hash.

so it looks like there is no variable content in that function.

Quote

Well, I put my function in the top of the file functions.php, but still doesn't work.

same reason as above. you need to find in WP where you can run pre-output functions.
Was This Post Helpful? 0
  • +
  • -

#5 Keylogger  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 343
  • Joined: 14-February 11

Re: Wordpress create function to clear cookies

Posted 17 September 2013 - 03:26 AM

View PostDormilich, on 17 September 2013 - 03:00 AM, said:

Seems that is a global variable, and it's needed..I already saw the value..it's an hash.
so it looks like there is no variable content in that function.

There is. I did:
echo $_COOKIE('wp-postpass_' . COOKIEHASH);
, and I got a value.
How do I verify if a cookie contains "wp-postpass"? I think I only need that to put this working.
Because I would do a loop like this:
if (isset($_SERVER['HTTP_COOKIE'])) {
    $cookies = explode(';', $_SERVER['HTTP_COOKIE']);
    foreach($cookies as $cookie) {
		if(..contains..$_COOKIE['wp-postpass_']){
	             // then I delete
		}
    }
}



Ok, I think I just solved. If it's not the best solution, let me know.
if (isset($_SERVER['HTTP_COOKIE'])) {
    $cookies = explode(';', $_SERVER['HTTP_COOKIE']);
    foreach($cookies as $cookie) {
		$var = $cookie;
		if(strpos($var, 'wp-postpass_')){
			 $parts = explode('=', $cookie);
			 $name = trim($parts[0]);
			 setcookie($name, '', time()-3600);
			 echo 'done';
		}
    }
}

Was This Post Helpful? 0
  • +
  • -

#6 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3485
  • View blog
  • Posts: 10,045
  • Joined: 08-June 10

Re: Wordpress create function to clear cookies

Posted 17 September 2013 - 03:32 AM

that loop is absolutely not necessary. $_COOKIE is a superglobal and as such always set (whether it contains data is another matter). some array filtering will suffice.
function hasPostpass($item) {
    return (false !== strpos($item, 'wp-postpass_'));
}
if (count(array_filter($_COOKIE, 'hasPostpass'))) {
    // ...
}


This post has been edited by Dormilich: 17 September 2013 - 03:33 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1