PMG Digital Made for Humans

WordPress How-To: Adding a Custom Checkbox to the Post "Publish" Box

3 MINUTE READ | December 16, 2015

WordPress How-To: Adding a Custom Checkbox to the Post "Publish" Box

Author's headshot

Emily Fox

Emily Fox has written this article. More details coming soon.

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

add_action('post_submitbox_misc_actions', createCustomField);add_action('save_post', saveCustomField);
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.

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

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}
Once the field is created, we need a way to save the field data. This is where the saveCustomField function is needed.

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

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');    }}
It’s that simple! Click here to view the gist with these examples in it.

– Emily Fox

Interested in working with us? See our open engineering roles here.

Stay in touch

Bringing news to you

Subscribe to our newsletter

By clicking and subscribing, you agree to our Terms of Service and Privacy Policy


Related Content

thumbnail image

AlliPMG CultureCampaigns & Client WorkCompany NewsDigital MarketingData & Technology

PMG Innovation Challenge Inspires New Alli Technology Solutions

4 MINUTES READ | November 2, 2021

thumbnail image

Applying Function Options to Domain Entities in Go

11 MINUTES READ | October 21, 2019

thumbnail image

My Experience Teaching Through Jupyter Notebooks

4 MINUTES READ | September 21, 2019

thumbnail image

Working with an Automation Mindset

5 MINUTES READ | August 22, 2019

thumbnail image

3 Tips for Showing Value in the Tech You Build

5 MINUTES READ | April 24, 2019

thumbnail image

Testing React

13 MINUTES READ | March 12, 2019

thumbnail image

A Beginner’s Experience with Terraform

4 MINUTES READ | December 20, 2018

ALL POSTS