Reputation: 13
I have recently decided to try and develop a wordpress website with a custom post type that i use to store "motorbikes". Instead of using custom fields, i would prefer to use a custom meta box for anyone to store the motorbike information as such as speed to be displayed on the website frontend.
Here is the code, notice on my save function for the meta data i haven't used a nonce and so, this is because i have been trying to get it to simply save the meta box inputs.
Here is my code:
function kc_mgb_initate(){
#this function is the first function that will be called to initiate our plugiin
#this will be incharge of creating the Menu and Submenu's, as well as initate the custom post's, meta box's and taxonomies
#Hang on, this plugin does not need options so no need for menus, skepping to creating custom posts
//add_menu_page('Motorbike_Garage', 'Motor Bike Garage'); #the function that registers the menu pages
#first create the labels for our custom post
$customlabels = array(
'name' => 'Motorbike Garage',
'singular_name' => 'Motorbike Garage',
'add_new' => 'Add New Motorbike',
'add_new_item' => 'Add New Motorbike',
'edit' => 'Edit',
'edit_item' => 'Edit Motorbikes',
'new_item' => 'New Motorbikes',
'view' => 'View',
'view_item' => 'View Motorbikes',
'search_items' => 'Search Motorbikes',
'not_found' => 'No Motorbikes found',
'not_found_in_trash' => 'No Motorbikes found in Trash',
'parent' => 'Parent Motorbike Review'
);
#array for our custom supports
$customsupports = array( 'title', 'editor', 'comments', 'thumbnail', 'custom-fields' );
#now a new array for our custom post args
$customargs = array('labels' => $customlabels, 'public' => true, 'menu_position' => 5, 'supports' => $customsupports, 'taxonomies' => array('motorbike_category'), 'has_archive' => true, 'register_meta_box_cb' => 'add_motorbikes_metaboxes'
); #this function registers the new custom post's
#the 'register_meta_box_cb' => 'add_motorbikes_metaboxes' adds the meta box information needed for this post by calling the add_motorbikes_metaboxes function
#now for the custom taxonomy we shall call 'motorbike_categories'
#the variables
$taxonomylabels = array('name' => _x( 'motorbikes', 'taxonomy general name' ),
'singular_name' => _x( 'motorbike', 'taxonomy singular name' ),
'search_items' => __( 'Search Motorbikes' ),
'all_items' => __( 'All Motorbikes' ),
'parent_item' => __( 'Parent Motorbike' ),
'parent_item_colon' => __( 'Parent Motorbike:' ),
'edit_item' => __( 'Edit Motorbike' ),
'update_item' => __( 'Update Motorbike' ),
'add_new_item' => __( 'Add New Motorbike' ),
'new_item_name' => __( 'New Motorbike Name' ),
'menu_name' => __( 'Motorbike Categories' ),
);
$taxonomyslugs = array('slug' => 'motorbikes', 'hierarchical' => true, 'with_front' => false);
#our function to add the taxonomy
register_taxonomy('motorbike_category', 'post', array('hierarchical' => true, 'labels' => $taxonomylabels, 'rewrite' => $taxonomyslugs));
register_post_type('motorbikes', $customargs);
}
function add_motorbikes_metaboxes(){
#this adds the metabox information for our meta data
add_meta_box('motorbikegaragedata', 'Motorbike Garage Information', 'create_motorbike_meta_box', 'motorbikes', 'side', 'default');
}
function create_motorbike_meta_box($post, $box){
#this function will create the form for us
#we need year, top mph, weight, seat height, engine in cc, power and 1/4 mile
$mbg_top_mph = get_post_meta($post->ID, 'mbg_top_mph', true);
$mbg_year = get_post_meta($post->ID, 'mbg_year', true);
$mbg_weight = get_post_meta($post->ID, 'mbg_weight', true);
$mbg_seat_height = get_post_meta($post->ID, 'mbg_seat_height', true);
$mbg_engine = get_post_meta($post->ID, 'mbg_engine', true);
$mbg_power = get_post_meta($post->ID, 'mbg_power', true);
$mbg_sec = get_post_meta($post->ID, 'mbg_sec', true);
?>
<p> Motorbike MPH </p>
<input type="text" name="mbg_top_mph" value="<?php echo $mbg_top_mph; ?>" /> <br />
<p> Motorbike Year </p>
<input type="text" name="mbg_year" value="<?php echo $mbg_year; ?>" /> <br />
<p> Motorbike Weight(KG) </p>
<input type="text" name="mbg_weight" value="<?php echo $mbg_weight; ?>" /> <br />
<p> Motorbike Seat Height(CM) </p>
<input type="text" name="mbg_seat_height" value="<?php echo $mbg_seat_height; ?>" /> <br />
<p> Motorbike Engine(cc)</p>
<input type="text" name="mbg_engine" value="<?php echo $mbg_engine; ?>" /><br />
<p> Motorbike Power(BHP) </p>
<input type="text" name="mbg_power" value="<?php echo $mbg_power; ?>" /><br />
<p> Motorbike 1/4 Mile(SECs) </p>
<input type="text" name="mbg_sec" value="<?php echo $mbg_sec; ?>" />
<?php
}
function save_motorbikes_metaboxes($post_id, $post){
#this saves the motorbike meta data
update_post_meta($post->ID, 'mbg_top_mph', $_POST['mbg_top_mph']);
update_post_meta($post->ID, 'mbg_year', $_POST['mbg_year']);
update_post_meta($post->ID, 'mbg_weight', $_POST['mbg_weight']);
update_post_meta($post->ID, 'mbg_seat_height', $_POST['mbg_seat_height']);
update_post_meta($post->ID, 'mbg_engine', $_POST['mbg_engine']);
update_post_meta($post->ID, 'mbg_power', $_POST['mbg_power']);
update_post_meta($post->ID, 'mbg_sec', $_POST['mbg_sec']);
}
#this is the action that will now call the function to add our custom posts
add_action('init', 'kc_mgb_initate'); # 'init' is when wordpresss has initiated properly
add_action('save_post', 'save_motorbikes_metaboxes'); #this is too call the save function
I have checked my variable names and so, i am just stuck to working out why it is not saving my meta box information.
Any tips or information you have would be really appreciated. Thank You.
Upvotes: 0
Views: 1387
Reputation: 15003
You never connect add_motorbikes_metaboxes
or save_motorbikes_metaboxes
to any hooks (should be add_meta_boxes
and save_post
, respectively) so they never get called.
Upvotes: 1