What happens when you need to add another custom field, meaning you have more than one? Let’s set the stage by adding a checkbox to our meta box.
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.
WordPress Meta Box Repeater
The repeater is a popular interface within a WordPress meta box. It allows your client to dynamically create new custom fields for a specific piece of content. In this hands-on lab, you’ll dive deep into JavaScript as you build your own repeater from scratch.
Reusable Meta Box Module
The bulk of the heavy lifting for working with meta boxes can be generated using a reusable module. In this hands-on coding lab, you will convert the meta box plugin you built in the Basics lab, making it configurable and reusable. Build it once. Then reuse it. For each custom meta box, build a configuration and view file. That’s it.
Wrap & Making it Reusable
Congratulations! You did it! You built a custom, from scratch meta box for a common task of providing a subtitle for your client. Looking back at the code, there’s a lot of boilerplate logic. That means with a few lines of code you were able to create a custom implementation. Notice that we hard-coded in the configuration information. That means we have an opportunity to create a reusable boilerplate and meta box generator function, that you can use on every project. We’ll do that in the next lab.
Passing Arguments to Render
The last parameter of the add_meta_box function gives you a mechanism to pass an array of arguments from the add/register component to the render function. It gets added into “args” element and is the second parameter in our render_meta_box function.
Changing Where Meta Box Appears
There are 2 arguments you can set to position your meta box on the screen: $context $priority – the priority within the above content For the context and a post type screen, you can position your meta box within the normal, side, or advanced locations.
On Save – Update or Delete Custom Field?
Next, we need to save the new raw subtitle value into the database. You already know how to do that, as you did it in the last lab (i.e. Post Metadata Basics). But we need to think about whether we should update the metadata when it’s empty, meaning no subtitle. Hmm, should we update to no value or delete it from the database? My vote: delete it. Let’s write the logic that handles both the update and delete tasks. As part of this process, we have to think about what “empty” means. Is 0 an empty? For a subtitle, yes. […]
Making Save Easier with $_POST Key
Think about an array. The data being posted back when you click publish or update is stored in an array within the superglobal $_POST (i.e. within the web server and PHP). It’s an array of content. Okay, now think about grouping and namespacing. Does it make sense in your mind that we have one key for our meta box and therefore, one entry in the $_POST? Think about that. That meta box key then would have an array with all of our custom fields’ values. Walking down that thought process, how could we implement this strategy? What are the advantages […]
Validate & Sanitize BEFORE Updating Database
BEFORE sending any data to the database, you first want to validate that data and then sanitize it. Why? To protect the integrity of the data. Before we save, we check that the data is what we think it should be. That’s the validation part of the process. Then we run it through a sanitizer process to strip out anything that shouldn’t be there, including nefarious bad stuff that could cause an expected and potentially dangerous experience in the browser. Let me show you in this episode what happens if a script gets stored into the database and then it’s […]
Bail Out if Not Our Meta Box
If we do not get our meta key back within the $_POST, then we can infer that our meta box does not exist on this screen. That means there’s nothing for us to process in the database. Let’s put code in to check that key. If it’s missing, then bail out.