I have a post php form and a set of inputs:
Every input looks the same, only the names change
You could use my tiny library ValueResolver in this case, for example:
$default = ValueResolver::resolve(get_option('your_name'), '<whatever your default value is>');
and don't forget to use namespace use LapaLabs\ValueResolver\Resolver\ValueResolver;
There are also ability to typecasting, for example if your variable's value should be integer
, so use this:
$id = ValueResolver::toInteger('6 apples', 1); // returns 6
$id = ValueResolver::toInteger('There are no apples', 1); // returns 1 (used default value)
Check the docs for more examples
you can change the get_option()
function to be something like
function get_option($name) {
$defaults = array(
'fist_name' => 'Mike',
'fist_name' => 'Wordpressor',
'my_name' => 'Dunno'
);
// get the value from the $defaults array
$val = $defaults[$name];
// but if the same value has already been posted - replace the default one
if (isset($_POST[$name])) {
$val = $_POST[$name];
}
return $val;
}
Set some variable at start with the post variables.
like
$name_val = "";
if(isset($_POST["your_name"])
{
$name_val = $_POST["your_name"];
}
<input type="text" name="your_name" value="<?= $name_val?>">
Since value
is the default, just add a condition around your get_option
-function, maybe something like this:
<input type="text" name="your_name" value="<?php $var = get_option('your_name'); if ($var == '') $var = 'your_default'; echo $var; ?>" />