3 MINUTE READ | December 16, 2015
WordPress How-To: Adding a Custom Checkbox to the Post "Publish" Box
There are times in WordPress when you need a custom field, but may not want to go through setting up a full custom meta box. Sometimes all you need is a simple checkbox that appears in the publish box. In this how-to, I’ll show you how to add a custom meta checkbox field to the existing post publish box.
This code can be placed in your theme’s functions.php file, however, I recommend adding it in a custom plugin. As a rule of thumb, if you will miss the functionality if you changed the theme, put it in a plugin – leave the theme for front end display functionality only… but that’s a bigger discussion.
First we will need to hook into post_submitbox_misc_actions to add the custom field, and save_post to save the field.
add_action.php
Then we need to create the functions being called within those, first we will create the custom field using the createCustomField function we specified above.add_action('post_submitbox_misc_actions', createCustomField);
add_action('save_post', saveCustomField);
This function gets the post ID from the post we’re currently in. We then want to check that the field only gets added to post types we want it to show on, in this example the custom field will only get added to WordPress posts. We then get the existing custom meta, set our nonce, and output the field.
createCustomField.php
Once the field is created, we need a way to save the field data. This is where the saveCustomField function is needed.function createCustomField()
{ $post_id = get_the_ID(); if (get_post_type($post_id) != 'post') { return; } $value = get_post_meta($post_id, '_my_custom_field', true); wp_nonce_field('my_custom_nonce_'.$post_id, 'my_custom_nonce'); ?> <div class="misc-pub-section misc-pub-section-last"> <label><input type="checkbox" value="1" <?php checked($value, true, true); ?> name="_my_custom_field" /><?php _e('My Custom Checkbox Field', 'pmg'); ?></label> </div> <?php }
This function checks that it is not an autosave, our nonce is set, and the user has the correct permissions to edit the post. If any of those fail, we return and don’t update the custom field. When we update/ save the post and our set conditions pass, we check the field is set and update the custom field post meta. If the post meta isn’t set, we remove it.
saveCustomField.php
It’s that simple! Click here to view the gist with these examples in it.function saveCustomField($post_id)
{ if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { return; } if ( !isset($_POST['my_custom_nonce']) || !wp_verify_nonce($_POST['my_custom_nonce'], 'my_custom_nonce_'.$post_id) ) { return; } if (!current_user_can('edit_post', $post_id)) { return; } if (isset($_POST['_my_custom_field'])) { update_post_meta($post_id, '_my_custom_field', $_POST['_my_custom_field']); } else { delete_post_meta($post_id, '_my_custom_field'); } }
– Emily Fox
Interested in working with us? See our open engineering roles here.
Stay in touch
Subscribe to our newsletter
By clicking and subscribing, you agree to our Terms of Service and Privacy Policy