Posted on by

Custom Field Validation

Setting up a custom validation method is relatively easy if you’ve got some PHP skills. If you’re familiar with using WordPress filters, then you’ve got the basic knowledge you need to set this up.

If you’re not sure what WordPress filters are and how to use them, there is a good intro article that should give you the basics: WordPress Actions, Filters, and Hooks : A guide for non-developers

Generally, however, doing this is going to require some knowledge of how to write some basic code because you’re going to have to figure out how to fill your particular need. I’m giving you a very simple example so you can see how all the pieces work.

How to Build a Custom Validation

A custom validation method consists of two parts: the validation code and the validation error message. The validation code is the part that looks at the submitted value and decides whether the input is OK or not. The error message is the user feedback message that is shown when a particular type of validation error occurs.

We will need to create a callback for each of these two things. It’s best to put these in a simple plugin, but your theme functions file will do also.

The Validation Callback

This callback is set up on the ‘pdb-before_validate_field’ action. The way this works is the action is triggered just before each field validation occurs. If you set up a callback that validates the field, that field won’t get validated by the normal methods because the plugin will see that the field has already been validated.

If you refer to the docs, you’ll see the properties of the object that is passed in with this action. All we need to do in our callback is check the field to see if it is one that we are validating, then check the value to see if it is valid, then set the ‘error_type’ property to either ‘valid’ or, if it is invalid, the type of validation error it is. That validation error type is the key to a validation error message which we will define later.

An Example Validation Callback

Let’s say we’re doing something really simple like checking to make sure the value is a valid URL. If the user submits a valid URL we mark it “valid” but if it is not, we mark it “invalid url”. Here is the callback code for that:

Pretty simple, huh? Of course, a real-world example would be more complex, but you can basically do anything you want inside that function to validate the field. For instance, if you wanted to validate the field against the value of another field, you can do that by inspecting the $_POST array, which will contain all the values that are currently getting submitted.

Setting Up the Validation Error Message

This one requires a filter callback that adds your validation error message to the array of validation methods the plugin uses to give the user feedback after they submit a form.

As per the docs, the filter passes in an array of validation error messages. We just need to add our message to that array and return it so it can be shown to the user if the field validation fails. Here’s a simple example of how to do that:

And that’s all there is to that. In a real application, the error message would likely be wrapped in a gettext function so that it can be translated.

Now, all that’s left to do is test it to make sure it works the way you want.

Leave a Reply