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. However, when you have a custom field for say number of videos or number of views or whatever, then 0 is a valid value and is not “empty”. Therefore, it should be saved to the database.
Let’s talk about handling the difference between “empty” (meaning an empty string) verses a falsey state.
Your functions are bloated. Put them on a diet. Think "skinny" and "as few lines as possible."
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