I’m working on a small plugin - part of it needs to add several meta boxes to all custom post types. I’ve tried looping through my array of custom port types in various ways but I keep getting errors that I’ve already called functions such as add_meta_box. The script below is my latest effort, it’s not saving values from the last three meta boxes (only the first).
I really want to reduce the size of this. I’m using a class, as per some stuff I learned on a plugin course on net tuts. It’s worked ok for other things, but PHP is not my strength and so I’m pretty damn stuck! Can someone help me loop through or pass the array of custom post types to the function add_meta_box?
Here is the part of relevant script, sorry it’s a big large..
//meta boxes...
public function meta_boxes(){
//load the array into a var
$all_post_types = $this->post_types;
$just_trades = array();
foreach($all_post_types as $trade){
$just_trades[] = $trade['post_type_name'];
}
//call the action
add_action('add_meta_boxes','make_meta_box');
function make_meta_box(){
//make array of all meta boxes we want to show..
add_meta_box('landline','Landline Phone Number','landline_function',$just_trades);//id, UI, the function, what page it shows on
add_meta_box('mobile','Mobile Phone Number','mobile_function',$just_trades);//id, UI, the function, what page it shows on
add_meta_box('email','Email Address','email_function',$just_trades);//id, UI, the function, what page it shows on
add_meta_box('website','Website URL','website_function',$just_trades);//id, UI, the function, what page it shows on
}
function landline_function($post){ // function that makes the HTML for the admin UI
$landline = get_post_meta($post->ID,'landline',true);// id is available, the key we want, return a single or array value. Single for this.
?>
<label for="landline">Landline Number: </label>
<input type="text" class="widefat" name="landline" id="landline" value="<?php echo $landline;?>" />
<?php
}
add_action('save_post','save_landline');
function save_landline($id){ // ID is available by default
if(isset($_POST['landline'])){
update_post_meta($id,'landline',strip_tags($_POST['landline']));
}
}
function mobile_function($post){ // function that makes the HTML for the admin UI
$mobile = get_post_meta($post->ID,'mobile',true);// id is available, the key we want, return a single or array value. Single for this.
?>
<label for="mobile">Mobile Number: </label>
<input type="text" class="widefat" name="mobile" id="mobile" value="<?php echo $mobile;?>" />
<?php
}
add_action('save_post','save_mobile');
function save_mobile($id){ // ID is available by default
if(isset($_POST['mobile'])){
update_post_meta($id,'mobile',strip_tags($_POST['mobile']));
}
}
function email_function($post){ // function that makes the HTML for the admin UI
$email = get_post_meta($post->ID,'email',true);// id is available, the key we want, return a single or array value. Single for this.
?>
<label for="email">Email: </label>
<input type="text" class="widefat" name="email" id="email" value="<?php echo $email;?>" />
<?php
}
add_action('save_post','save_email');
function save_email($id){ // ID is available by default
if(isset($_POST['email'])){
update_post_meta($id,'email',strip_tags($_POST['email']));
}
}
function website_function($post){ // function that makes the HTML for the admin UI
$website = get_post_meta($post->ID,'website',true);// id is available, the key we want, return a single or array value. Single for this.
?>
<label for="website">Website: </label>
<input type="text" class="widefat" name="website" id="website" value="<?php echo $website;?>" />
<?php
}
add_action('save_post','save_website');
function save_website($id){ // ID is available by default
if(isset($_POST['website'])){
update_post_meta($id,'website',strip_tags($_POST['website']));
}
}
}
..any help would be great. Dan.