26. Overview of Code
1. Add actions
2. Create custom meta box
3. Create input fields
4. Save data
27. // Define the custom meta box
add_action( ‘add_meta_boxes’, ‘card_meta_box’ );
// Do something with the data entered
add_action( ‘save_post’, ‘card_save_postdata’ );
29. // Print the inner box contents
function card_inner_custom_box( $post ) {
// get the post meta and display it in our input
// the actual fields for data entry
// use nonce for verification
}
30. // get the post meta and display it in our input
$team = get_post_meta($post->ID, ‘_team_name’, true);
$condition = get_post_meta($post->ID, ‘_card_condition’,
true);
$rookie = get_post_meta($post->ID, ‘_rookie_card’, true);
31. get_post_meta( $post_id, $key, $single);
// returns the values of the custom fields with the
specified key from the specified post
32. // the actual fields for data entry
// text input
<label for=”_team_name”>What team?</label>
<input type=”text” name=”_team_name” id=”_team_name” value=<?php
echo $team; ?> />
33. // the actual fields for data entry
// select input
<label for=”_card_condition”>What condition?</label>
<select name=”_card_condition” id=”_card_condition”>
<option value=”good” <?php selected( $condition, ‘good’ ); ?>>
Good
</option>
<option value=”bad” <?php selected( $condition, ‘bad’ ); ?>>
Bad
</option>
</select>
35. // the actual fields for data entry
// checkbox input
<input type=”checkbox” name=”_rookie_card”
id=”_rookie_card” <?php checked( $rookie, ‘on’ ); ?> />
<label for=”_rookie_card”>Is it a rookie card?</label>
36. checked( $checked, $current, $echo);
// returns html (checked=‘checked’)
// if values are the same, it adds checked
37. // use nonce for verification
// validates that the contents of the form came from the
location on the current site
wp_nonce_field( plugin_basename( __FILE__ ),
‘sports_card_nonce’ );
40. // When the post is saved, saves our custom data
function card_save_postdata( $post ) {
// verify it wasn’t an auto save
// verify it came from our screen
// check permissions
// once authenticated, find and save the data
}
41. // verify is this is an autosave routine
if ( defined( ‘DOING_AUTOSAVE’ ) && DOING_AUTOSAVE )
return;
// verify it came from our screen with proper authorization
if ( !wp_verify_nonce( $_POST[‘sports_cards_nonce’],
plugin_basename( __FILE__) ))
return;
58. Use an _ to remove meta data from the
custom fields
_card_condition
59. Use more than one add_meta_box() if you need it on posts and pages.
add_meta_box(
‘card_meta_box’, // $id
‘Card Details’, // $title
‘card_inner_custom_box’, // $callback
‘sports_cards_cpt’ // $post_type
);
add_meta_box(
‘card_meta_box’, // $id
‘Card Details’, // $title
‘card_inner_custom_box’, // $callback
‘post’ // $post_type
);
61. Limit a meta box to one page by checking
post/page id
// get $post_id
$post_id = $_GET['post'] ? $_GET['post'] :$_POST['post_ID'] ;
// checks for post/page ID
if ($post_id == '84')
{
add_meta_box();
}
63. 1. Custom Metaboxes and Fields for WordPress
- a framework for easily creating custom metaboxes
- https://github.com/jaredatch/Custom-Metaboxes-and-
Fields-for-WordPress
64. 2. Meta Box
- a plugin for creating custom meta boxes
- http://wordpress.org/extend/plugins/meta-box/
65. 3. Reusable Custom Meta Boxes
- http://wp.tutsplus.com/tutorials/reusable-custom-meta-
boxes-part-1-intro-and-basic-fields/