** SOLVED **
I had failed to require Wordpress in the form processor. Adding this line at the top fixed it (always the simplest thing, right!?):
require_once( explode( "wp-content" , __FILE__ )[0] . "wp-load.php" );
I've created a custom front-end profile for a client's site, and I'm adding a way for other users to favorite that profile. (Please don't suggest anything like BuddyPress or another plugin -- I've tried about a dozen, and none of them have all of the capabilities I need. Thanks.) :)
Anyway.... here is the overview. I hope it's clear. Let's say that USER 1 is viewing the profile of USER 10.
- In the database, user 1 has a field called favorite_10 which can be set to "yes" or "no" (or null)
- When someone clicks the "Favorite" button in another user's profile, it will run a simple script to change the value from "yes" to "no" or vice-versa
That's it. I think it's a pretty good solution, but the form processing script is breaking at the update_user_meta
line.
Here we go.
foreach($sitterlist as $sitteritem) {
$code = 'favorite_'.(esc_html($sitteritem->ID));
$key = esc_html($sitteritem->ID);
$favorite[$key] = get_the_author_meta( $code, $currentuser);
}
$is_favorite=null;
if ($favorite[$usertosearch]=='yes') {
$is_favorite = true;
} else {
$is_favorite = false;
}
?>
<form method="post" action="updatefavorite.php">
<input type="hidden" value="<?php echo $usertosearch; ?>" name="usertosearch" id="usertosearch">
<input type="hidden" value="<?php echo $currentuser; ?>" name="currentuser" id="currentuser">
<input type="hidden" value="<?php echo $is_favorite; ?>" name="is_favorite" id="is_favorite">
<button type="submit" id="favoritebutton" name="favoritebutton"><span>Favorite</span></button>
</form>
And here is updatefavorite.php
<?php
if(isset($_POST['favoritebutton'])) {
$currentuserid = $_POST['currentuser'];
$currentprofileid = $_POST['usertosearch'];
$currentfavorite = $_POST['is_favorite'];
$code = 'favorite_'.$currentprofileid;
if ($currentfavorite) {
$currentfavorite = 'no';
} else {
$currentfavorite = 'yes';
}
update_user_meta($currentuserid,$code,$currentfavorite);
}
header('Location: ' . $_SERVER['HTTP_REFERER']);
?>
After a lot of testing and debugging, I've figured out that it's breaking at update_user_meta
... but I don't know why.
Thank you!