The first step is to register your new meta box with WordPress. You’ll need to register a callback to the action event admin_menu and then use the WordPress function add_meta_box. Let’s walk through the code, look at the function in WordPress core, and look at the HTML.
Labs
Labs are hands-on coding projects that you build along with Tonya as she explains the code, concepts, and thought processes behind it. You can use the labs to further your code knowledge or to use right in your projects. Each lab ties into the Docx to ensure you have the information you need.
Each lab is designed to further your understanding and mastery of code. You learn more about how to think about its construction, quality, maintainability, programmatic and logical thought, and problem-solving. While you may be building a specific thing, Tonya presents the why of it to make it adaptable far beyond that specific implementation, thereby giving you the means to make it your own, in any context.
What is a Meta Box?
Let’s recap what a meta box is and why and when you should build a custom one. A meta box is a UI (user interface) component to allow interactivity with content but without the technical aspects. Think about that statement. A meta box makes it easy to interact with content by adding, editing, deleting, etc. WordPress comes with a built Custom Field meta box. That interface is fine for simple projects or your own stuff. But for clients, we want to make it easier, more intuitive, and less technical. A meta box lets us abstract away the complexity such as […]
Lab Introduction
Let’s get you ready to start this lab. I’ll walk through your setup. Use the same setup and sandbox you created in the Post Metadata Basics lab, including having Kint installed. You need to clone the starting plugin from GitHub. You will need Git installed on your machine in order to do this. Follow along with me in the video to clone it to your machine. Make sure the Meta Box Basics plugin is on the “master” branch by typing: git checkout master Then activate the plugin.
WordPress Meta Box Basics
A meta box provides an interface for site owners, author, editors, admins, etc. to view, add, delete, and edit custom fields (post metadata). In this hands-on coding lab, you will create a meta box boilerplate from scratch, one that you can use on any project.
Wrap it Up
WooHoo!! You did it! Great job! Let’s review what you learned in this lab: Custom field is the same thing as post metadata, metadata, and post meta. Custom field refers to the interface, such as a form. Post metadata refers to the code and database. You can use the built-in meta box to add, update, and delete custom fields in the back-end. WordPress provides functionality to get, update, add, and delete metadata programmatically in your theme or plugin.
More Custom Field Functions
Up to this episode, you have learned how to get, update, add, and delete post metadata. But WordPress provides additional functionality. Let’s explore these functions together: the_meta() get_post_custom() get_post_custom_values() get_post_custom_keys()
WordPress Post Metadata (Custom Fields) Basics
Using post metadata, we can add additional information to built-in and custom post types (content). We use this information for specific filtering, querying, grouping, and processing tasks. In the back end, we build metaboxes whose custom fields provide an interface for authors to input the metadata. In this hands-on lab, you will learn the basics of WordPress post metadata and the custom fields you build for their interface.
Add a New Custom Field
Let’s add a new custom field, which is a post metadata, to the database. You’ll use add_post_meta() and walk through using it. You’ll see what it does in the database, how to use it, what the different arguments do, and even peak into WordPress Core. This function adds a new post metadata record WHEN: ‘unique’ is set to false (you can get duplicates with this one). ‘unique’ is set to true AND the meta value does not already exist in the database for that post ID. Huh? Watch the video and do it with me to better understand what this […]
Update an Existing Custom Field
Let’s update an existing custom field, which is a post metadata, in the database. You’ll use update_post_meta() and walk through using it. You’ll see what it does in the database, how to use it, what the different arguments do, and even peak into WordPress Core. This function does multiple tasks: If the post metadata exists, it updates the meta_value WHEN ‘unique’ is set to false. OR ‘unique’ is set to true and the existing meta_value matches what you pass in as the previous argument. If the post metadata doesn’t exist, then it adds it into the database. So this function […]
Get Subtitle Post Metadata
In this episode, you and I will add the subtitle to the Twenty Seventeen. Let’s walk through the strategy of: Finding where to add the new subtitle’s HTML in the theme. What HTML we will use. What functionality we need to get the subtitle out of the database and prepare it to render in the browser. Resources esc_html_e() get_post_meta() get_the_id()