Right, I’m banging my head against a wall here. I’m sure it’s something incredibly simple but I keep getting undefined index errors on all of these variables.
function meta_genus_species() {
global $post;
if (isset($post)) {
$custom = get_post_custom($post->ID);
}
if (isset($custom)) {
$genus = $custom["genus"][0];
$species = $custom["species"][0];
$etymology = $custom["etymology"][0];
$family = $custom["family"][0];
$common_names = $custom["common_names"][0];
}
?>
<label>Genus:</label>
<input name="genus" value="<?php if(isset($genus)) { echo $genus; } ?>" />
<label>Species:</label>
<input name="species" value="<?php if(isset($species)) { echo $species; } ?>" />
<p><label>Etymology:</label><br />
<textarea cols="50" rows="5" name="etymology"><?php if(isset($etymology)) { echo $etymology; } ?></textarea></p>
<label>Family:</label>
<input name="family" value="<?php if(isset($family)) { echo $family; } ?>" />
<label>Common Names:</label>
<input name="common_names" value="<?php if(isset($common_names)) { echo $common_names; } ?>" />
<?php
}
I get this for every variable:
Notice: Undefined index: genus in […]sf-species-profiles.php on line 207
Any ideas?
It’s a common PHP error, usually when you try to access an array member with a non-existent key;
You should check for the key first with
isset( $array['foobar'] );
UPDATE: In this case, I would chuck in a loop that sets-up the variables for you, checking for the index in the process.
You are already calling isset() each time you are printing the data to the screen.
Why not just skip this part:
and do this when you print an input:
The extra variable assignments are not needed and are causing notices to be generated here.
BTW …
You need to escape your output before it is printed in a form:
An alternative, which has come out of a Twitter discussion on this post, is to change how you’re getting your data.
get_post_custom()
returns an array of arrays and is what’s causing you headaches. I would recommend usingget_post_custom_values()
instead:A better alternative to custom values would be to use custom meta. You can define these as unique, then when you get the custom meta back out of the DB you’ll have one value rather than an indexed array with only one member. Just something to consider.