Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.
If the asker does not get an answer then they have 10 days to request a refund.
$20
Custom Meta Box for Custom Post Type
I require the full php code to create a Custom Meta Box for a Custom Post Type.
My category for my custom post type is named "ourteamcat"
Fields required:
Text Box for job title with title "Job Title"
Text Box for email address with title"Email"
Text Box for phone number with title "Phone"
Radio Button for sex with title "Sex" with options "Male" & "Female"
Check Box 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.
This question has been answered.
parksey18 | 03/19/11 at 8:37pm
Edit
Previous versions of this question:
03/19/11 at 9:47pm
(1) Possible Answers Submitted...
See a chronological view of answers?
Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.
-

Last edited:
03/20/11
7:03amDenzel Chia says: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.
DenzelPrevious versions of this answer: 03/19/11 at 10:13pm | 03/19/11 at 10:15pm | 03/19/11 at 10:17pm | 03/19/11 at 11:05pm | 03/19/11 at 11:05pm | 03/20/11 at 5:48am | 03/20/11 at 5:51am
- 03/20/11 3:38am
parksey18 says: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?
- 03/20/11 5:53am
Denzel Chia says: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 - 03/20/11 6:00am
Denzel Chia says: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
- 03/20/11 3:38am
This question has expired.
Current status of this question: Completed
Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.
If the asker does not get an answer then they have 10 days to request a refund.
