1

I have made a favorite plugin. With a shortcode I'm able to display the button to 'a wish/favorite list' anywhere I want the button. This button is for adding the post_IDs of pages/posts/blogs/articles etc in a cookie array with this code:

<?php 
 if (isset($_POST['submit_wishlist'])){
  if (!isset($_COOKIE['favorites'])){
   //echo 'not set <br>';
   $cookie_value = get_the_ID();
   $init_value = array($cookie_value);
   $init_value = serialize($init_value);
   //echo $init_value;
   setcookie('favorites', $init_value, time() + (86400 * 30), "/");
   wp_redirect($_SERVER['HTTP_REFERER']);
 } else {
   //echo 'set <br>';
   $cookie_value = get_the_ID();
   $prev_value = $_COOKIE['favorites'];
   $prev_value = stripslashes($prev_value);
   $prev_value = unserialize($prev_value);

   array_push($prev_value, $cookie_value);
   $new_value = serialize($prev_value);
   //echo $new_value;
   setcookie('favorites', $new_value, time() + (86400 * 30), "/");
   wp_redirect($_SERVER['HTTP_REFERER']);
 }
} 
?>

This is working fine, and the post_ids getting stored in the cookie array. With the code print_r(unserialize($_COOKIE['favorites'])); I'm able to print the Cookie and get a overview of all the stored post_ids.

Problem/Question
Currently I've add a new shortcode for displaying the favorite list. Each value of that list, is getting a trashbutton for deleting/unset that cookie. Now I need to get the following code to work:

<?php 
$all_favorites= unserialize($_COOKIE['favorites']);

echo '<table>';
foreach($all_favorites as $key => $value) {
 echo '<tr>';
  echo 'Post-ID = ' . $value . ' ';
  ?>
  <form method="POST"><button type="submit" class="btn btn-default" name="delete"><span class="glyphicon glyphicon-trash"></span></button>
  <input type="hidden" name="delete_id" value="<?php echo $value; ?>" />
  </form><br>
  <?php
 echo '</tr>';
}
echo '</table>';
if (isset($_POST['delete'])){
  //function for setting new cookie, function is displayed on each page before the get_header()
  set_cookie_delete();
}
?>

The output of this part of code:

enter image description here

Edit
The function:

<?php
function set_cookie_delete(){
  $all_favorites = unserialize($_COOKIE['favorites']);
  $delete_id = $_POST['delete_id'];
  echo 'deleted value = ' . ' ' . $delete_id . '<br>';

  $array_delete = array_diff($all_favorites, array($delete_id));
  $array_delete = serialize($array_delete);
  print_r($array_delete);

  wp_redirect($_SERVER['HTTP_REFERER']);
  setcookie('favorites', $array_delete, time() + (86400 * 30), "/");

  //echo '<br><br>';
  //print_r($_COOKIE);

  }
  ?>

What I don't understand is why my setcookie(); is not working. It's on the beginning of the page and I first refresh the page so the cookie is able to set, right?

Every help will be appreciated, thanks in advance!

  • Sorry to ask but why are you using cookies instead of saving it to DB. The list is session based? – Laxmana Apr 26 '17 at 13:07
  • It's just for education. I know the disadvantage of using Cookies. But I need to use this for this assignment from my teacher. – W. White Apr 26 '17 at 13:09
  • I see. Sorry I was curious :) – Laxmana Apr 26 '17 at 13:09
  • 1
    Also note that in order to set cookies, you need to do it before the HTTP headers are sent out, which has already happened by the time your shortcode is called. As for changing what's in the cookie itself, that's standard PHP code, not WordPress code – Tom J Nowell Apr 26 '17 at 13:27
  • Because you redirect before setting the cookie? – Laxmana Apr 28 '17 at 8:43
3

You need to put the id of each item inside form to indicate the item that will be deleted.

<?php 
$all_favorites= unserialize($_COOKIE['favorites']);

echo '<table>';
foreach($all_favorites as $key => $value) {
 echo '<tr>';
  echo 'Post-ID = ' . $value . ' ';
  ?>
  <form method="POST">
    <input type="hidden" name="id" value="<?php echo $value; ?>">
    <button type="submit" class="btn btn-default" name="delete">
        <span class="glyphicon glyphicon-trash"></span>
    </button>
  </form><br>
  <?php
 echo '</tr>';
}
echo '</table>';
if (isset($_POST['delete'])){
   $id = $_POST['id']; // do security checks (sanitize etc)
   // unset post with $id from cookie

}
?>
1

SOLUTION

First of all, many thanks to Laxmana for pushing me in the right direction!

I have the function set_cookie_delete() to this code:
The function

<?php
  if (isset($_POST['delete'])){
  $all_favorites = unserialize($_COOKIE['favorites']);
  $delete_id = $_POST['delete_id'];
  //echo 'deleted value = ' . ' ' . $delete_id . '<br>';

  $array_delete = array_diff($all_favorites, array($delete_id));
  $array_delete = serialize($array_delete);
  //print_r($array_delete);

  setcookie('favorites', $array_delete, time() + (86400 * 30), "/");
  wp_redirect($_SERVER['HTTP_REFERER']);
 }
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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