WooHoo! You did it! I’m so proud of you. Whew that was a long, long lab. But it was more than just building a reusable meta box module. My intent was to help you learn about: the Reusable Mindset – developing how you think about software, quality, and costs Architecture – developing how you layout your code Advanced programming concepts for filtering, merging, recursively merging and replacing, and remapping of arrays, as well as assigning a callable to a variable and then invoking it. Tell me what you think Now I want to hear from you. What do you think? […]
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.
Walk Through & Test Our Save Function
We’re nearly done with our save functionality. Let’s walk through it and talk about how it works. We’ll also manually test each piece of it to ensure it’s performing as expected (i.e. expected behavior). We’ll fix a problem too. We’ll also talk about and explore how to assign a callable to a variable and then invoke it like this: Here’s the link to Callable in Docx for you.
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.
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.