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

Custom Meta Box for Custom Post Type WordPress

  • SOLVED

Hi,

I require the full php code to create a <strong>Custom Meta Box</strong> for a <strong>Custom Post Type</strong>.

My category for my custom post type is named "ourteamcat"

<strong>Fields required:</strong>
<strong>Text Box</strong> for job title with title "Job Title"
<strong>Text Box</strong> for email address with title"Email"
<strong>Text Box</strong> for phone number with title "Phone"
<strong>Radio Button</strong> for sex with title "Sex" with options "Male" & "Female"
<strong>Check Box</strong> for regions with the title "Regions" with options: "South West" "North West" "Central" "Eastern"

I am after the full code, and the method to echo the data.

Answers (1)

2011-03-19

Denzel Chia answers:

Hi,

This is what you wanted.
I had tried in twenty ten theme and confirm that it is working.

This is my register_post_type, I know you already had this,
so this is just for you to know that I am testing using your post type "ourteamcat"


add_action( 'init', 'create_my_post_types' );

function create_my_post_types() {
register_post_type( 'ourteamcat',
array(
'labels' => array(
'name' => __( 'ourteamcat' ),
'singular_name' => __( 'ourteamcat' )
),
'public' => true,
)
);
}



This is the custom meta box code


/* Use the admin_menu action to define the custom boxes */
add_action('admin_menu', 'myplugin_add_custom_box');

/* Use the save_post action to do something with the data entered */
add_action('save_post', 'myplugin_save_postdata');

/* Adds a custom section to the "normal" Post and Page edit screens */
function myplugin_add_custom_box() {

if( function_exists( 'add_meta_box' )) {

add_meta_box( 'myplugin_sectionid', __( 'Personal Details', 'myplugin_textdomain' ),
'myplugin_inner_custom_box', 'ourteam', 'normal' ,'high');

}

}


function myplugin_inner_custom_box() {

// Use nonce for verification

echo '<input type="hidden" name="myplugin_noncename" id="myplugin_noncename" value="' .
wp_create_nonce( plugin_basename(__FILE__) ) . '" />';


global $post;
if($post->post_type == 'ourteam'){
$post_id = $post->ID;
}

$job_title = get_post_meta($post_id, 'job_title', true);
$email_address = get_post_meta($post_id, 'email_address', true);
$phone_number = get_post_meta($post_id, 'phone_number', true);
$sex = get_post_meta($post_id, 'sex', true);
$regions = get_post_meta($post_id, 'regions', true);

echo '<p><label for="job_title">' . __("Job Title", 'myplugin_textdomain' ) . '</label> ';
echo "<input type=\"text\" name=\"job_title\" value=\"$job_title\" style=\"width:50%\" /></p>";

echo '<p><label for="email_address">' . __("Email", 'myplugin_textdomain' ) . '</label> ';
echo "<input type=\"text\" name=\"email_address\" value=\"$email_address\" style=\"width:50%\" /></p>";

echo '<p><label for="phone_number">' . __("Phone", 'myplugin_textdomain' ) . '</label> ';
echo "<input type=\"text\" name=\"phone_number\" value=\"$phone_number\" style=\"width:50%\" /></p>";

echo '<p><label for="sex">' . __("Sex", 'myplugin_textdomain' ) . '</label> ';
echo '<input type="radio" name="sex" value="male"';
if($sex == 'male'){echo 'checked';}else{echo '';};
echo '/> Male';
echo'<input type="radio" name="sex" value="female"';
if($sex == 'female'){echo 'checked';}else{echo '';};
echo '/> Female';

echo '<p><label for="regions">' . __("Regions", 'myplugin_textdomain' ) . '</label> ';
echo '<input type="checkbox" name="regions" value="south_west"';
if($regions == 'south_west'){echo 'checked';}else{echo '';};
echo' /> South West';
echo '<input type="checkbox" name="regions" value="north_west"';
if($regions == 'north_west'){echo 'checked';}else{echo '';};
echo '/> North West ';
echo '<input type="checkbox" name="regions" value="central"';
if($regions == 'central'){echo 'checked';}else{echo '';};
echo '/> Central ';
echo '<input type="checkbox" name="regions" value="eastern"';
if($regions== 'eastern'){echo 'checked';}else{echo '';};
echo '/> Eastern ';


}

/* When the post is saved, saves our custom data */

function myplugin_save_postdata( $post_id ) {

// verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times

if ( !wp_verify_nonce( $_POST['myplugin_noncename'], plugin_basename(__FILE__) )) {
return $post_id;
}

if ( 'ourteam' == $_POST['post_type'] ) {
if ( !current_user_can( 'edit_page', $post_id ))
return $post_id;
}

// OK, we're authenticated: we need to find and save the data

$job_title = $_POST['job_title'];
$email_address = $_POST['email_address'];
$phone_number = $_POST['phone_number'];
$sex = $_POST['sex'];
$regions = $_POST['regions'];

//update post meta into database

update_post_meta($post_id, 'job_title', $job_title);
update_post_meta($post_id, 'email_address', $email_address);
update_post_meta($post_id, 'phone_number', $phone_number);
update_post_meta($post_id, 'sex', $sex);
update_post_meta($post_id, 'regions', $regions);

}



To use on theme template, using get_post_meta,
These are the examples;


<?php
global $post;
$post_id = $post->ID;
$job_title = get_post_meta($post_id, 'job_title', true);
$email_address = get_post_meta($post_id, 'email_address', true);
$phone_number = get_post_meta($post_id, 'phone_number', true);
$sex = get_post_meta($post_id, 'sex', true);
$regions = get_post_meta($post_id, 'regions', true);

echo $job_title; //etc to print out all values.
echo $email_address;
echo $phone_number;
echo $sex;
echo $regions;
?>



You may need to style the custom meta box labels and inputs to suit your designs.

Thanks.
Denzel


parksey18 comments:

Sorry I am having trouble,
This is the full code for what I registered the custom post type as

function h141_post_type_ourteam() {
register_post_type( 'ourteam',
array(
'label' => __('Our Team'),
'public' => true,
'show_ui' => true,
'show_in_nav_menus' => true,
'rewrite' => true,
'hierarchical' => true,
'menu_position' => 5,
'supports' => array(
'title',
'editor',
'thumbnail',
'excerpt',
'custom-fields',
'revisions')
)
);
register_taxonomy('ourteamcat', 'ourteam', array('hierarchical' => true, 'label' => __('Our Team Categories'), 'singular_name' => 'Category'));
}

add_action('init', 'h141_post_type_ourteam');


What am I doing wrong?


Denzel Chia comments:

Hi,

Your register post type is ourteam not ourteamcat,
You have given me wrong information.

However I had updated the above custom metabox code, by changing all "ourteamcat" to "ourteam"
Please use it again.

Thanks.
Denzel


Denzel Chia comments:

Hi,

For your information, custom meta box, relates to custom post type and not registered taxonomy,
So in your case it relates to "ourteam" and not "ourteamcat".
I thought what your provided was the post type, I did not realize you provided the taxonomy.

Never mind, there is not much difference.
Just use the above updated custom meta box code.
I had changed all "ourteamcat" to "ourteam", it should work for you now.

Thanks.
Denzel