0

I have a real estate site. On property pages users can save homes as a favorite. The property gets added to the user meta data. I can successfully add and delete entries from the single property page with no issues.

I have "view favorites" page that displays all saved homes. I'd like users to be able to remove saved homes from this page. My issues is the homes are being displayed in a foreach and if you choose to remove one home...all homes end up being removed. I'm sure my issue is that the foreach is looping the delete_user_meta function but I can't figure out how to set it up correctly.

Each property has an array of entries (bed / bath / price / etc). I'm assuming I need to POST the array and use the posted array in the delete function but I can't figure it out.

My code:

<?php
$user_id = get_current_user_id(); //Get current user ID
global $current_user; wp_get_current_user();
$user_id = $current_user->ID;
$meta = get_user_meta($user_id, 'savedhome');
?>
<div class="row"> <!--defines and displays each result successfully-->
<?php foreach ($meta as $key => $value) { ?>
    <?php
        foreach($value as $k1 => $v1) {

        if ($k1 == 'Beds'){
        $k1beds = $v1;
        }// end if beds
        
        if ($k1 == 'Baths'){
        $k1bath = $v1;
        }// end if baths
        
        if ($k1 == 'Price'){
        $k1price = $v1;
        }// end if Price 
        }//end foreach value as k1 v1
    ?>
        <div class="favprice">
        <?php echo "$$k1price";?>
        </div>
        <div class="favbed">
        <?php echo "$k1beds";?>
        </div>
        <div class="favbath">
        <?php echo "$k1bath";?>
        </div>
 </div><!-- close row --> 
<?php
echo '<pre>' , var_dump($value) , '</pre>';  //displays correct array for each home 

if(isset($_POST["Remove"])){
    delete_user_meta($user_id, 'pluginlink', $value);
}//end if 

//Below will determine if home exists in user meta and display the correct save/delete button
$meta = get_user_meta($user_id, 'savedhome'); 
if(array_search($k1price, array_column($meta, 'Price')) !== false) {
    $savebutton = "display: none;";
    $deletebutton = "display: block;";
}
else {
    $savebutton = "display: block;";
    $deletebutton = "display: none;";
}
?>
<div class="buttondisplay" style=" <?php echo $savebutton; ?> ">
    <form method="post" enctype="multipart/form-data">
        <button type="submit" name="submit" style="background-color: #fff; padding-left:3px;" ><i class="fa fa-heart-o" aria-hidden="true" ></i></button>
    </form>
</div><!-- end save button -->
<div class="deletebutton" style=" <?php echo $deletebutton; ?> ">
    <form method="post" enctype="multipart/form-data" >
            <button type="submit" name="Remove" value="<?php echo $k1mlsid; ?>" style="background-color: #fff; padding-left:3px;"><i class="fa fa-heart" aria-hidden="true" ><?php echo $loginouttext; ?></i></button>
    </form>
</div><!-- end delete button -->
<?php } //end foreach meta as key value
?>
2
  • What's the array structure which is being saved as array? If the property ID is the key, you will need to unset it first while removing and then save the array again as user meta.
    – Abhik
    Jun 21 at 12:49
  • Update: I'm trying to send data serialized via POST but it keeps only sending a:11:{s:3: Not sure why it's not sending full array.
    – Jayce
    Jun 21 at 18:57

1 Answer 1

0

This is such a convoluted way of doing this. You should separate the delete_user_meta call from the foreach entirely and put type="hidden" inputs in the form with the rest of the data you need.

if(isset($_POST["Remove"])){
    delete_user_meta(get_current_user_id(), 'pluginlink', $_POST["value"]);
}//end if 

Form

<form method="post" enctype="multipart/form-data" >
  <button type="submit" name="Remove" value="<?php echo $k1mlsid; ?>" style="background-color: #fff; padding-left:3px;"><i class="fa fa-heart" aria-hidden="true" ><?php echo $loginouttext; ?></i></button>
  <input type="hidden" name="value" value="<?php echo $value; ?>">
</form>

Another way of tackling this would be to not use forms at all. Instead you use a link, like <a class="deletebutton" href="/?user-updates=remove&value=data">. Then you create a script to handle these queries.

if (isset($_GET['user-updates']) && $_GET['user-updates'] == 'remove') {
  delete_user_meta(get_current_user_id(), 'pluginlink', $_GET["remove"]);
}

It does the same thing with way less code, easier CSS, and easier AJAX in the future if you decide to add QOL improvements.

2
  • This makes a lot of sense but I'm still having an issue. In the isset piece of code I echoed a value to make sure it was returning the correct data..it is. The array for meta-value however is not fully coming in. When I echo $_POST["value"] it gets cut off only displaying a:11:{s:3:.
    – Jayce
    Jun 21 at 23:02
  • Update: I'm now able to see the correct array by adding htmlspecialchars to the form value but now all saved properties are deleted instead of just the one.
    – Jayce
    Jun 21 at 23:57

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.