WordPress.org

Forums

Clear cookie of posts password protected (8 posts)

  1. Linesofcode
    Member
    Posted 1 year ago #

    I'm sorry if this isn't the right section of the forum to publish this topic.

    I'd like to create a page just to reset cookie.
    I tried to create a page: unset.php, I put the page inside the wp-content\themes\mytheme\, with this code:

    <?php setcookie("wp-postpass_", "", time()-3600);
    unset($_COOKIE["wp-postpass_"]); ?>

    Then, I would like to call this way:

    <form method="post" action="unset.php">
      <input type="submit" value="logout"/>
    <form>

    Or even
    <a href="/unset.php">Logout</a>

    But it's not working. Any ideias? Thanks!

  2. esmi
    Forum Moderator
    Posted 1 year ago #

  3. Linesofcode
    Member
    Posted 1 year ago #

    I want to clear the posts protected pass. How does that help? How can I call that function in button?

  4. esmi
    Forum Moderator
    Posted 1 year ago #

  5. Linesofcode
    Member
    Posted 1 year ago #

    I appreciate your try to help, but it's in vain.
    Your first link, it doesn't say where to put that code, for example.
    The second link a guy in comments said this:

    This code fucked up the rest of my site!! I removed it and now the passwords are not accepted!!

    And, again, I would prefer to have a button, or sort of, so I could redirect to another page and log off.

    I know the code to delete the cookie, I don't know where to put it. I would like to create a custom page (unset.php), like I said in the first post..is it possible or not? Because, whenever I create the page, if I go to http://www.site.com/unset.php it automatically redirects to 404 error page.

    I must say that this custom page I put into the same directory as 404 error page..is that the problem?

  6. Linesofcode
    Member
    Posted 1 year ago #

    I have currently this:

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

    And, in the form, I call this way:
    [clear__ foo="clear it"]Some text input[/clear__]
    But I'm getting:
    Warning: Cannot modify header information - headers already sent by

  7. bcworkz
    Member
    Posted 1 year ago #

    setcookie() uses the PHP header() function, so calling from a short code callback is way too late.

    Put your setcookie() code on the separate unset.php file. Require once the wp-load.php file first thing. You probably want to call auth_redirect() next. The separate file will get rid of the headers sent issue.

    If it still does not work, the problem would be the COOKIEHASH constant was re-defined between calls.

  8. Linesofcode
    Member
    Posted 1 year ago #

    I managed to get this working yesterday, but I forgot to put here.
    My only problem was the location of the page "unset.php".

    1 - So, I created the file unset.php and I copy into the root directory of the website (not wp-content\themes..etc)
    2 - Coded a function to get all cookies wordpress was using
    3 - Match the cookie to "wp-postpass_"
    4 - If the cookie was found, I delete it and redirect to index.php

    To call everything, I just needed to call the page unset.php by ahref.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.