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.
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.
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.
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.
Nonce Security Check
We use the WordPress nonce value to secure our saving process and determine if we should continue or not. Let’s talk about the nonce field and validation check in this episode. Watch this episode! We use the WordPress generated nonce for the following reasons: Validate if our meta box is on this screen. Validate that our meta box is (for the most part) valid. Nonce is a technique to validate that we are receiving what we expect. Resources WordPress Nonce wp_verify_nonce
Save Process & $_POST
This episode is VERY important as you’ll learn how a form in the browser is processed back on the web server. The entire back-end interface on the edit post and page screens is an HTML form. The meta box you built earlier is a component of input fields within that form. When you click on Publish or Update, all of the content on the page is sent back to the web server and stored in the $_POST super global in PHP. Big points here for you to master: Saving a post, page, custom post type, etc. triggers a post back […]
Meta Box HTML
When the screen in the back-end renders, WordPress calls our meta box callback, which we called render_meta_box. In your code, that function does the following tasks: Grabs the metadata out of the database. Does processing if you need it. Loads our view file, which gets rendered out to the browser. Make sure you do this episode with me, as I walk through the code, HTML, and thought processes. You’ll see how little code it takes to create the meta box’s HTML. Resources Variable scope when including files (such as the view file) wp_nonce_field get_post_meta