I’m building a plugin that has a meta box. Some of the fields in the meta box are required. Is using jQuery the only method for achieving this? Can I require that a field is filled in using PHP?
Leave a Reply
You must be logged in to post a comment.
You can use Javascript to create a first-line convenience warning, but that is not a secure solution. You will need to interrupt the post save to truly create a required field.
That will also reset the post to ‘Draft’ if the meta field is deleted.
There is no way to require input per PHP. Only the browser can do that, and the browser gets the output after PHP is done.
You can add the attribute
required
:But not all browsers support that, and you have to check the value still in your validation handler.
I know this is an old question but it’s first result on Google… Here my two cents:
Remember add all javascript fields validations before.
Thanks to @s_ha_dum answer, this is my custom meta box validation method:
Add filter:
Due WordPress is showning post saved message, I need to filter redirect post location query. This is the method:
And finally we add our custom message:
%s can be replaced with a transient data, or other way you want to save not valid meta box fields.
Remember to add filter: