Save Process & $_POST
Lab: WordPress Meta Box Basics
Video Runtime: 12:33
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 from the browser to the server, as the HTML form is processed.
- PHP, running on the web server, stores all of that data into a super global called
- The form field names are the keys within the
$_POSTwill include things like the post ID, post title, content, post status, author, categories, tags, all of the custom fields (post metadata), etc.
In this episode, we look at the saving processing and explore what’s in
$_POST after updating a post.
For our meta box, we register our saving function to an action event called
save_post. When WordPress fires that event, our meta box saving function is called.
But here’s the thing. What if our meta box isn’t on that screen? We’ve registered our saving function to an event that fires for ALL saving activities, regardless if our meta box is on that screen or not. Hmm, that means we need to check:
- Is our meta key in the
$_POSTsuper global variable? If no, bail out.
- Does it have the right nonce security? If no, bail out.
If we pass both of those checks, then we want to continue processing the form fields and then updating the database accordingly.
Keep It Simple, Stupid (KISS) - the best kiss you'll get in code.
Total Lab Runtime: 02:21:18
- 1 Lab Introductionfree 07:20
- 2 What is a Meta Box?free 07:28
- 3 Add a New Meta Boxpro 11:46
- 4 Meta Box HTMLpro 18:39
- 5 Save Process & $_POSTfree 12:33
- 6 Bail Out if Not Our Meta Boxpro 05:03
- 7 Nonce Security Checkpro 10:16
- 8 On Save - Update or Delete Custom Field?pro 12:03
- 9 Validate & Sanitize BEFORE Updating Databasefree 14:41
- 10 Add Another Custom Fieldpro 08:49
- 11 Making Save Easier with $_POST Keypro 05:32
- 12 Better Saving Strategy for Multiple Custom Fieldspro 14:47
- 13 Changing Where Meta Box Appearspro 06:02
- 14 Passing Arguments to Renderpro 02:18
- 15 Wrap & Making it Reusablepro 04:01