This might be kind of simple, but I couldn't find a way to do so in the WP docs. My plugin currently uses custom fields for things I could just have radio buttons/check boxes for each post. How do I add a little section where I can have such options rather than custom fields?
2 Answers
Below is the example of a couple of checkboxes to set custom field values:
// register the meta box
add_action( 'add_meta_boxes', 'my_custom_field_checkboxes' );
function my_custom_field_checkboxes() {
add_meta_box(
'my_meta_box_id', // this is HTML id of the box on edit screen
'My Plugin Checkboxes', // title of the box
'my_customfield_box_content', // function to be called to display the checkboxes, see the function below
'post', // on which edit screen the box should appear
'normal', // part of page where the box should appear
'default' // priority of the box
);
}
// display the metabox
function my_customfield_box_content() {
// nonce field for security check, you can have the same
// nonce field for all your meta boxes of same plugin
wp_nonce_field( plugin_basename( __FILE__ ), 'myplugin_nonce' );
echo '<input type="checkbox" name="my_plugin_paid_content" value="1" /> Paid Content <br />';
echo '<input type="checkbox" name="my_plugin_network_wide" value="1" /> Network wide';
}
// save data from checkboxes
add_action( 'save_post', 'my_custom_field_data' );
function my_custom_field_data($post_id) {
// check if this isn't an auto save
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
// security check
if ( !wp_verify_nonce( $_POST['myplugin_nonce'], plugin_basename( __FILE__ ) ) ) // spelling fix
return;
// further checks if you like,
// for example particular user, role or maybe post type in case of custom post types
// now store data in custom fields based on checkboxes selected
if ( isset( $_POST['my_plugin_paid_content'] ) )
update_post_meta( $post_id, 'my_plugin_paid_content', 1 );
else
update_post_meta( $post_id, 'my_plugin_paid_content', 0 );
if ( isset( $_POST['my_plugin_network_wide'] ) )
update_post_meta( $post_id, 'my_plugin_network_wide', 1 );
else
update_post_meta( $post_id, 'my_plugin_network_wide', 0 );
}
Following following hooks and functions are required for adding a meta box, see below references for more information about them:
Hooks
- admin_init (action)
- save_post (action)
- add_meta_boxes (action) (you don't need admin_init in WordPress 3.0+)
Functions
-
By the way - do you have a recommendation for API reference for WP? Their main docs I was looking at seem absurdly incomplete in the way of getting information about each action Commented May 26, 2011 at 14:54
-
Also having trouble getting the values to show their current value in the Edit Post page Commented May 26, 2011 at 16:48
-
Here's my current source. I apologize for it being bit clunky, I made a lot of changes at the coffee shop... and I'm new to WP plugin creation github.com/jphenow/WP-Difficulty-and-Platform/blob/experimental/… Commented May 26, 2011 at 16:55
-
1The code on github looks fine to me, can't test it right now. For the API docs, you can use: adambrown.info/p/wp_hooks Or just put the action/filter name on google and you will find a pretty good tutorial about it. Commented May 26, 2011 at 17:32
This is call in the WordPress world a MetaBox you need to use add_meta_box() function to add your own.
there is a great tutorial and a PHP class you can use in your plugin at deluxeblogtips.com I would suggest you use that for a starting point which will make your life easier.