• About Us
    • New York
  • Work
  • Capabilities
  • Careers
  • Technology
  • Blog
  • Contact Us
  • About Us
    • New York
  • Work
  • Capabilities
  • Careers
  • Technology
  • Blog
  • Contact Us
December 16, 2015

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

Posted by Emily Fox

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.

Adding the field

First we will need to hook into post_submitbox_misc_actions to add the custom field, and save_post to save the field.

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.

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.

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.

DevelopmentwordpressWordpress how-to
Previous
Next

Latest White Papers

  • Shifting Plans for 2020 & Beyond
  • Game On: How Brands Can Log Into A Diverse Multi-Billion Dollar Industry
  • What CCPA Means For Brands
  • How Google is Improving Consumer Data Privacy
  • Ways to Prepare for the Cookieless Future
  • See all White Papers

Featured Posts

  • Ad Age Names PMG #1 Best Place to Work in 2021
  • Hindsight 2020 & Looking Ahead to 2021
  • Preparing for Streaming’s Growth & The Future of TV Buying
  • MediaPost Names PMG Independent Agency of the Year
  • PMG Client Portfolio Trends During Amazon Prime Day 2020

Categories

  • Consumer Insights
  • Content
  • Creative Design
  • Data Analytics
  • Development
  • Digital TV & Video
  • Ecommerce
  • Industry News
  • Local
  • Mobile
  • Paid Search
  • PMG Culture
  • Programmatic & Display
  • SEO
  • Social Media
  • Structured Data
Fort Worth

2845 West 7th Street
Fort Worth, TX 76107

Dallas

3102 Oak Lawn Avenue
Suite 650
Dallas, TX 75219

Austin

823 Congress Avenue
Suite 800
Austin, TX 78701

London

33 Broadwick Street
London
W1F 0DQ

New York

120 East 23rd Street
New York, NY 10010

Get in touch

(817) 420 9970
info@pmg.com

Subscribe to the PMG Newsletter
© 2021 PMG Worldwide, LLC, All Rights Reserved
  • Contact
  • Privacy Policy
 Tweet
 Share
 Tweet
 Share
 Tweet
 Share
 LinkedIn
We and our partners use cookies to personalize content, analyze traffic, and deliver ads. By using our website, you agree to the use of cookies as described in our Cookie Policy.