...

View Full Version : Wordpress Meta Boxes issue



AnimalD
04-19-2012, 08:48 PM
Hi, I have a group of Meta Boxes appearing on the home page of a site, and a group of other boxes appearing on the other pages. All update individually, but when the meta fields on the other pages are updated, the meta boxes on the home page are wiped clean. I have split the code into separate functions but have had no luck.

The trouble seems to be the fact that the boxes are updated through the $page_id, could this be including the home page and returning the values as empty as they are empty, and non-existent?

Here is the code:


add_action("admin_init", "admin_init");
add_action("admin_init", "admin_init");
add_action("save_post", "save_events");
add_action("save_post", "save_home");
$home_id = 5;

function admin_init(){
$post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'];
if ($post_id == '5'){
add_meta_box("contact-details", "Contact Details", "home_options", "page", "side", "default");
}else{
add_meta_box("page-details", "Page Details", "meta_options", "post", "side", "default");
add_meta_box("page-details", "Page Details", "meta_options", "page", "side", "default");
}
}

function home_options(){
global $post;
$home_id = 5;
$home = get_post_custom($home_id);
$address1 = $home['address1'][0];
$address2 = $home['address2'][0];
$address3 = $home['address3'][0];
$post_code = $home['post_code'][0];
$phone = $home['phone'][0];
$phone_int = $home['phone_int'][0];
$fax = $home['fax'][0];
$fax_int = $home['fax_int'][0];
$email = $home['email'][0];
$opening_times = $home['opening_times'][0];
$post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'];
if($post_id == $home_id){ ?>

<label for="address1">Address Line 1</label>
<input type="text" name="address1" value="<?php echo $address1; ?>" size="44"/>

<label for="address2">Address Line 2</label>
<input type="text" name="address2" value="<?php echo $address2; ?>" size="44"/>

<label for="address3">Address Line 3</label>
<input type="text" name="address3" value="<?php echo $address3; ?>" size="44"/>

<label for="post_code">Post Code</label>
<input type="text" name="post_code" value="<?php echo $post_code; ?>" size="44"/>

<label for="phone">Phone</label>
<input type="text" name="phone" value="<?php echo $phone; ?>" size="44"/>

<label for="phone_int">Phone (International)</label>
<input type="text" name="phone_int" value="<?php echo $phone_int; ?>" size="44"/>

<label for="fax">Fax</label>
<input type="text" name="fax" value="<?php echo $fax; ?>" size="44"/>

<label for="fax_int">Fax (International)</label>
<input type="text" name="fax_int" value="<?php echo $fax_int; ?>" size="44"/>

<label for="email">Email</label>
<input type="email" name="email" value="<?php echo $email; ?>" size="44"/>

<label for="opening_times">Opening Times</label>
<textarea rows="4" cols="45" name="opening_times"><?php echo $opening_times; ?></textarea>

<?php }

}

function meta_options(){
global $post;
$custom = get_post_custom($post->ID);
$post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'] ;
$page_desc = $custom["page_desc"][0];
$custom_side = $custom["custom_side"][0];
if($post_id !== '5'){ ?>

<label for="page_desc">Page Description: (100 characters max)</label>
<textarea rows="4" cols="45" name="page_desc" maxlength="100"><?php echo $page_desc; ?></textarea>

<label for="custom_side">Sidebar:</label>
<textarea rows="10" cols="45" name="custom_side"><?php echo $custom_side; ?></textarea>

<?php }

}

function save_events(){
global $post;
$home_id = 5;
$post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'] ;
if(post_id !== $home_id){
if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE){
return $post_id;
}
update_post_meta($post_id, "page_desc", $_POST["page_desc"]);
update_post_meta($post_id, "custom_side", $_POST["custom_side"]);
}
}

function save_home(){
global $post;
$home_id = 5;
$post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'] ;
if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE){
return $home_id;
}
update_post_meta($home_id, "address1", $_POST["address1"]);
update_post_meta($home_id, "address2", $_POST["address2"]);
update_post_meta($home_id, "address3", $_POST["address3"]);
update_post_meta($home_id, "post_code", $_POST["post_code"]);
update_post_meta($home_id, "phone", $_POST["phone"]);
update_post_meta($home_id, "phone_int", $_POST["phone_int"]);
update_post_meta($home_id, "fax", $_POST["fax"]);
update_post_meta($home_id, "fax_int", $_POST["fax_int"]);
update_post_meta($home_id, "email", $_POST["email"]);
update_post_meta($home_id, "opening_times", $_POST["opening_times"]);
}

// Display the custom field/meta in a post
// echo $meta_values = get_post_meta($post->ID, 'page_desc', true);
// echo $meta_values = get_post_meta($post->ID, 'custom_side', true);
// echo $meta_values = get_post_meta($home_id, 'address1', true);
// echo $meta_values = get_post_meta($home_id, 'address2', true);
// echo $meta_values = get_post_meta($home_id, 'address3', true);
// echo $meta_values = get_post_meta($home_id, 'post_code', true);
// echo $meta_values = get_post_meta($home_id, 'phone', true);
// echo $meta_values = get_post_meta($home_id, 'phone_int', true);
// echo $meta_values = get_post_meta($home_id, 'fax', true);
// echo $meta_values = get_post_meta($home_id, 'fax_int', true);
// echo $meta_values = get_post_meta($home_id, 'email', true);
// echo $meta_values = get_post_meta($home_id, 'opening_times', true);

angst
04-19-2012, 09:37 PM
this is product specific code. try the WP forum: http://wordpress.org/support/



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum