Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Troubleshoot: multiple custom meta fields WordPress

  • SOLVED

So here's the problem:

I have multiple custom meta boxes for pages, posts, custom post types, etc.

When I save a post, page, etc. the custom meta box works perfectly, saving the meta data.

However, every meta name from every custom meta box is added to the post's meta data, with no values.

What am I missing?

Here's the code:

<?php
//hook for custom meta box
add_action('admin_init', 'people_meta');

//call back to add meta box
function people_meta()
{
add_meta_box('info', 'Information', 'my_property_meta', 'person', 'normal', 'low');
}
//callback for meta box
function my_property_meta($post)
{
$metadata = get_post_custom($post->ID);

?>
<input type="hidden" name="myplugin_noncename" id="myplugin_noncename" value="<?php echo wp_create_nonce('my-nonce'); ?>" /> <!--creates hidden field used for check -->
Firstname:<input type='text' name='firstname' value='<?php if($metadata['firstname'][0]!='') echo $metadata['firstname'][0]; ?>' /><br/>
Lastname:<input type='text' name='lastname' value='<?php if($metadata['lastname'][0]!='') echo $metadata['lastname'][0]; ?>' /><br/>

Title 1:<input type='text' name='title1' value='<?php if($metadata['title1'][0]!='') echo $metadata['title1'][0]; ?>' /><br/>
Title 2:<input type='text' name='title2' value='<?php if($metadata['title2'][0]!='') echo $metadata['title2'][0]; ?>' /><br/>

Phone:<input type='text' name='phone' value='<?php if($metadata['phone'][0]!='') echo $metadata['phone'][0]; ?>' /><br/>
Fax:<input type='text' name='fax' value='<?php if($metadata['fax'][0]!='') echo $metadata['fax'][0]; ?>' /><br/>
Address 1:<input type='text' name='address1' value='<?php if($metadata['address1'][0]!='') echo $metadata['address1'][0]; ?>' /><br/>
Address 2:<input type='text' name='address2' value='<?php if($metadata['address2'][0]!='') echo $metadata['address2'][0]; ?>' /><br/>

Email:<input type='text' name='email' value='<?php if($metadata['email'][0]!='') echo $metadata['email'][0]; ?>' /><br/>

Website:<input type='text' name='website' value='<?php if($metadata['website'][0]!='') echo $metadata['website'][0]; ?>' /><br/>

Focus 1:<input type='text' name='focus1' value='<?php if($metadata['focus1'][0]!='') echo $metadata['focus1'][0]; ?>' /><br/>
Focus 2:<input type='text' name='focus2' value='<?php if($metadata['focus2'][0]!='') echo $metadata['focus2'][0]; ?>' /><br/>
Focus 3:<input type='text' name='focus3' value='<?php if($metadata['focus3'][0]!='') echo $metadata['focus3'][0]; ?>' /><br/>

Education 1:<input type='text' name='education1' value='<?php if($metadata['education1'][0]!='') echo $metadata['education1'][0]; ?>' /><br/>
Education 2:<input type='text' name='education2' value='<?php if($metadata['education2'][0]!='') echo $metadata['education2'][0]; ?>' /><br/>
Education 3:<input type='text' name='education3' value='<?php if($metadata['education3'][0]!='') echo $metadata['education3'][0]; ?>' /><br/>
Education 4:<input type='text' name='education4' value='<?php if($metadata['education4'][0]!='') echo $metadata['education4'][0]; ?>' /><br/>

<?php
}

//saving hook
add_action('save_post', 'my_save_meta');

//callback for saving hook, should be able to save all custom meta boxes might break into seperate functions for each
function my_save_meta($post_id)
{
//check if info came from our form and not another call to save_post hook
if ( !wp_verify_nonce( $_POST['myplugin_noncename'], 'my-nonce' ))
{
return $post_id;
}

//check for autosave, if autosave is running, do not do anything
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
{
return $post_id;
}
update_post_meta($post_id, 'firstname', $_POST['firstname']);
update_post_meta($post_id, 'lastname', $_POST['lastname']);
update_post_meta($post_id, 'title1', $_POST['title1']);
update_post_meta($post_id, 'title2', $_POST['title2']);
update_post_meta($post_id, 'phone', $_POST['phone']);
update_post_meta($post_id, 'fax', $_POST['fax']);
update_post_meta($post_id, 'address1', $_POST['address1']);
update_post_meta($post_id, 'address2', $_POST['address2']);
update_post_meta($post_id, 'email', $_POST['email']);
update_post_meta($post_id, 'website', $_POST['website']);
update_post_meta($post_id, 'focus1', $_POST['focus1']);
update_post_meta($post_id, 'focus2', $_POST['focus2']);
update_post_meta($post_id, 'focus3', $_POST['focus3']);
update_post_meta($post_id, 'education1', $_POST['education1']);
update_post_meta($post_id, 'education2', $_POST['education2']);
update_post_meta($post_id, 'education3', $_POST['education3']);
update_post_meta($post_id, 'education4', $_POST['education4']);
}
?>
<?php
//hook for custom meta box
add_action('admin_init', 'page_features_meta');

//call back to add meta box
function page_features_meta()
{
add_meta_box('info', 'Page Features', 'page_features', 'page', 'normal', 'low');
}
//callback for meta box
function page_features($post)
{
$metadata = get_post_custom($post->ID);

?>
<input type="hidden" name="myplugin_noncename" id="myplugin_noncename" value="<?php echo wp_create_nonce('my-nonce'); ?>" /> <!--creates hidden field used for check -->

<label for="calendar">Google Calendar ID:</label>
<br />
<input type='text' name='calendar' value='<?php if($metadata['calendar'][0]!='') echo $metadata['calendar'][0]; ?>' /><br/>

<label for="form">Gravity Form ID:</label>
<br />
<input type='text' name='form' value='<?php if($metadata['form'][0]!='') echo $metadata['form'][0]; ?>' /><br/>

<label for="embed">Embed Code:</label>
<br />
<textarea rows="5" cols="60" name='embed'><?php if($metadata['embed'][0]!='') echo $metadata['embed'][0]; ?></textarea><br/>

<label for="childnav">Child Navigation:</label>
<br />
<input type="radio" name="childnav" value="true" <?php if ($metadata['childnav'][0] == 'true') echo "checked=1";?>> True <br/>
<input type="radio" name="childnav" value="false" <?php if ($metadata['childnav'][0] != 'true') echo "checked=1";?>> False <br/>
<?php
}

//saving hook
add_action('save_post', 'page_save_meta');

//callback for saving hook, should be able to save all custom meta boxes might break into seperate functions for each
function page_save_meta($post_id)
{
//check if info came from our form and not another call to save_post hook
if ( !wp_verify_nonce( $_POST['myplugin_noncename'], 'my-nonce' ))
{
return $post_id;
}

//check for autosave, if autosave is running, do not do anything
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
{
return $post_id;
}
update_post_meta($post_id, 'calendar', $_POST['calendar']);
update_post_meta($post_id, 'form', $_POST['form']);
update_post_meta($post_id, 'embed', $_POST['embed']);
update_post_meta($post_id, 'childnav', $_POST['childnav']);
}
?>
<?php
//hook for custom meta box
add_action('admin_init', 'video_meta');

//call back to add meta box
function video_meta()
{
add_meta_box('info', 'Video Meta', 'video', 'video', 'normal', 'low');
}
//callback for meta box
function video($post)
{
$metadata = get_post_custom($post->ID);

?>
<input type="hidden" name="myplugin_noncename" id="myplugin_noncename" value="<?php echo wp_create_nonce('my-nonce'); ?>" /> <!--creates hidden field used for check -->

<label for="embed">Embed Code:</label>
<br />
<textarea rows="5" cols="60" name='embed'><?php if($metadata['embed'][0]!='') echo $metadata['embed'][0]; ?></textarea><br/>
<? }

//saving hook
add_action('save_post', 'video_save_meta');

//callback for saving hook, should be able to save all custom meta boxes might break into seperate functions for each
function video_save_meta($post_id)
{
//check if info came from our form and not another call to save_post hook
if ( !wp_verify_nonce( $_POST['myplugin_noncename'], 'my-nonce' ))
{
return $post_id;
}

//check for autosave, if autosave is running, do not do anything
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
{
return $post_id;
}
update_post_meta($post_id, 'embed', $_POST['embed']);
}
?>

Answers (2)

2011-11-23

Gabriel Reguly answers:

Hi Charlie,

That is normal behavior.

You could check for empty values and do not save them

If you need, I can post a fix for you.

Regards,
Gabriel


Gabriel Reguly comments:

Hi Charlie,

Here you have a quick fix:


if ( ! empty( $_$_POST['calendar'] ) ) {
update_post_meta($post_id, 'calendar', $_POST['calendar']);
}
if ( ! empty( $_$_POST['form'] ) ) {
update_post_meta($post_id, 'form', $_POST['form']);
}
if ( ! empty( $_$_POST['embed'] ) ) {
update_post_meta($post_id, 'embed', $_POST['embed']);
}
if ( ! empty( $_$_POST['childnav'] ) ) {
update_post_meta($post_id, 'childnav', $_POST['childnav']);
}


Regards,
Gabriel


Gabriel Reguly comments:

Hi Charlie,

You should apply the same logic here:


update_post_meta($post_id, 'firstname', $_POST['firstname']);

update_post_meta($post_id, 'lastname', $_POST['lastname']);

update_post_meta($post_id, 'title1', $_POST['title1']);

update_post_meta($post_id, 'title2', $_POST['title2']);

update_post_meta($post_id, 'phone', $_POST['phone']);

update_post_meta($post_id, 'fax', $_POST['fax']);

update_post_meta($post_id, 'address1', $_POST['address1']);

update_post_meta($post_id, 'address2', $_POST['address2']);

update_post_meta($post_id, 'email', $_POST['email']);

update_post_meta($post_id, 'website', $_POST['website']);

update_post_meta($post_id, 'focus1', $_POST['focus1']);

update_post_meta($post_id, 'focus2', $_POST['focus2']);

update_post_meta($post_id, 'focus3', $_POST['focus3']);

update_post_meta($post_id, 'education1', $_POST['education1']);

update_post_meta($post_id, 'education2', $_POST['education2']);

update_post_meta($post_id, 'education3', $_POST['education3']);

update_post_meta($post_id, 'education4', $_POST['education4']);


Regards,
Gabriel


Gabriel Reguly comments:

Hi Luis,

Sorry, and I don't want to start an argument, but the code seems ok to me.

I did not tested the code, but I see this:


//check if info came from our form and not another call to save_post hook

if ( !wp_verify_nonce( $_POST['myplugin_noncename'], 'my-nonce' ))



Please correct me if I am wrong.

Saludos,
Gabriel


Gabriel Reguly comments:

Hi Luis,

Aha, I see what you mean now :-)

Oddly the asker says the code is working, he only does not want the empty fields to be saved.

Or I got it wrongly?

Saludos,
Gabriel

2011-11-23

Luis Abarca answers:

Amigo, i think your problems is with your nonce fields, try this way.

//callback for meta box

function video($post)

{

$metadata = get_post_custom($post->ID);

<strong> wp_nonce_field('my-nonce', 'myplugin_noncename');</strong>
?>
<label for="embed">Embed Code:</label>
<br />
<textarea rows="5" cols="60" name='embed'><?php if($metadata['embed'][0]!='') echo $metadata['embed'][0]; ?></textarea><br/><?
}


//callback for saving hook, should be able to save all custom meta boxes might break into seperate functions for each

function video_save_meta($post_id)
{
<strong> //check if info came from our form and not another call to save_post hook
if ( !wp_verify_nonce( $_POST['myplugin_noncename'], 'my-nonce' ))
{
return $post_id;
}</strong>

//check for autosave, if autosave is running, do not do anything
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
{
return $post_id;
}

update_post_meta($post_id, 'embed', $_POST['embed']);
}


Luis Abarca comments:

You are right Gabriel, Charlie said, that the code is OK and just don't want empty fields.

But i think he should use "wp_nonce_field" when send data through a form, besides is more easy to build the hidden field ;)


Luis Abarca comments:

By the way, i'm using this code to check for empty values and update post meta's



$fields = array(
'firstname', 'lastname', 'title1', 'title2', 'phone', 'fax', 'address1', 'address2',
'email', 'website' // ...
);

foreach ($fields as $item) {
if ( isset($_POST[$item]) && !empty($_POST[$item]) ) {
update_post_meta($post_id, $item, $_POST[$item]);
}
}


This way you can avoid the E_NOTICE messages and also add or delete fields quickly