I have a template where a user will set some custom meta values via a form on the front end of my site. I call this form via a function in my functions.php file.
Currently when the text inputs are filled out and I hit submit, the values save to the database like expected, however I don’t see the values submitted on the page until I hit refresh on my browser. But when I do that I then see the values the user set but the values get stripped from the database, (hitting refresh again on my browser will default back to no values be set).
Not sure I have this setup correctly?
Here is the code from my template:
<h2>Your Values</h2>
Value 1: <?php if(!$new_high) { echo 'Not Set'; } else { echo '$' . $new_high; } ?>
Value 2: <?php if(!$new_medium) { echo 'Not Set'; } else { echo '$' . $new_medium; } ?>
Value 3: <?php if(!$new_low) { echo 'Not Set'; } else { echo '$' . $new_low; } ?>
Then I call the function in the template that has the form: <?php my_form_function(); ?>
In my functions file I have this which houses the form and updates the user_meta:
function my_form_function() {
global $current_user;
?>
<form name="setPrices" action="" method="POST">
<fieldset>
<label for="lowPrice">Value 3:</label>
<input type="text" id="lowPrice" name="lowPrice" value="<?php echo $_POST['lowPrice']; ?>" />
</fieldset>
<fieldset>
<label for="mediumPrice">Value 2:</label>
<input type="text" id="mediumPrice" name="mediumPrice" value="<?php echo $_POST['mediumPrice']; ?>" />
</fieldset>
<fieldset>
<label for="highPrice">Value 1:</label>
<input type="text" id="highPrice" name="highPrice" value="<?php echo $_POST['highPrice']; ?>" />
</fieldset>
<button type="submit">Save</button>
</form>
<?php
$low_price = $_POST['lowPrice'];
$medium_price = $_POST['mediumPrice'];
$high_price = $_POST['highPrice'];
update_user_meta( $current_user->ID, '_low_price', $low_price);
update_user_meta( $current_user->ID, '_medium_price', $medium_price);
update_user_meta( $current_user->ID, '_high_price', $high_price);
}
So just to make sure I haven’t confused anyone, The above does work (kinda) and when the form is submitted the values save to the database as expected. But I don’t see the values in my template until I refresh the page, and once I do that the values reset to nothing. I know my problem is how/where I have update_user_meta
You need to pull your meta_data from WP and fill that info into the form… like so: