[wp-testers] Custom post type and Custom fields, help
Patrick Edqvist
patrick.edqvist at gmail.com
Tue Mar 30 08:08:38 UTC 2010
Hello everyone,
What i'm trying to do is some nice custom fields added to my specific custom
post type, in this case it's called "case"..
Using add meta box i add some fields to the post type case and they show up
just fine. Problem is however that when i update the post no information is
saved in those fields. also i wondering i'm using the right technique to get
those fields. What am I doing wrong?
Here's the data that i'm using:
*Adding the fields*
/* Use the admin_menu action to define the custom boxes */
add_action('admin_menu', 'case_add_custom_box');
/* Use the save_post action to do something with the data entered */
add_action('save_post', 'case_save_postdata');
/* Adds a custom section to the "side" Post edit screens */
function case_add_custom_box() {
if( function_exists( 'add_meta_box' )) {
add_meta_box( 'case_sectionid', __( 'Case information',
'case_textdomain' ),
'case_inner_custom_box', 'case', 'side' );
} else {
add_action('dbx_post_advanced'
, 'case_old_custom_box' );
}
}
/* Prints the inner fields for the custom post/page section */
function case_inner_custom_box() {
// Use nonce for verification
echo '<input type="hidden" name="case_noncename" id="case_noncename"
value="' .
wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
// The actual fields for data entry
echo '<label for="case_new_client" style="margin: 5px 0; display:
block;">' . __("Who was the client?", 'case_textdomain' ) . '</label> ';
echo '<input type="text" name="case_new_client" value="" size="30"
/><br>';
echo '<label for="case_new_project" style="margin: 5px 0; display:
block;">' . __("What was the project name?", 'case_textdomain' ) . '</label>
';
echo '<input type="text" name="case_new_project" value="" size="30"
/><br>';
echo '<label for="case_new_year" style="margin: 5px 0; display: block;">'
. __("What year was this project released?", 'case_textdomain' ) . '</label>
';
echo '<input type="text" name="case_new_field" value="" size="30" /><br>';
echo '<label for="case_new_partner" style="margin: 5px 0; display:
block;">' . __("Who were the partner('s)?", 'case_textdomain' ) . '</label>
';
echo '<input type="text" name="case_new_partner" value="" size="30"
/><br>';
echo '<label for="case_new_link" style="margin: 5px 0; display: block;">'
. __("Is there any link to the project? website?", 'case_textdomain' ) .
'</label> ';
echo '<input type="text" name="case_new_link" value="" size="30" /><br>';
echo '<label for="case_new_pdf" style="margin: 5px 0; display: block;">' .
__("Is there a pdf available for this case?", 'case_textdomain' ) .
'</label> ';
echo '<input type="text" name="case_new_pdf" value="" size="30" /><br>';
}
/* Prints the edit form for pre-WordPress 2.5 post/page */
function case_old_custom_box() {
echo '<div class="dbx-b-ox-wrapper">' . "\n";
echo '<fieldset id="case_fieldsetid" class="dbx-box">' . "\n";
echo '<div class="dbx-h-andle-wrapper"><h3 class="dbx-handle">' .
__( 'My Post Section Title', 'case_textdomain' ) . "</h3></div>";
echo '<div class="dbx-c-ontent-wrapper"><div class="dbx-content">';
// output editing form
case_inner_custom_box();
// end wrapper
echo "</div></div></fieldset></div>\n";
}
/* When the post is saved, saves our custom data */
function case_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['case_noncename'], plugin_basename(__FILE__)
)) {
return $post_id;
}
// verify if this is an auto save routine. If it is our form has not been
submitted, so we dont want
// to do anything
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
return $post_id;
// Check permissions
if ( 'post' == $_POST['post_type'] ) {
if ( !current_user_can( 'edit_post', $post_id ) )
return $post_id;
} else {
if ( !current_user_can( 'edit_page', $post_id ) )
return $post_id;
}
// OK, we're authenticated: we need to find and save the data
$mycasedata = $_POST['case_new_client'];
update_post_meta($pageId, '_'.$name, $mycasedata);
// Do something with $mydata
// probably using add_post_meta(), update_post_meta(), or
// a custom table (see Further Reading section below)
return $mydata;
}
*Trying to get the data in a template using*
<?php echo get_post_meta( $post_id, 'case_new_client', 'true' ); ?>
Kind regards
--
Patrick Edqvist
More information about the wp-testers
mailing list