Friday , 17 November 2017
Home / Programming / WordPress / Plugins / WordPress Settings API Part 3 – WordPress Plugin Tutorial
WordPress Settings API
WordPress Settings API

WordPress Settings API Part 3 – WordPress Plugin Tutorial

This post is part of the WordPress Plugin tutorial series. To find a link to all tutorials in order, then please click here.

In our last tutorial, we created a functional WordPress plugin using the WordPress settings API. A cool feature that WordPress allows us to do is do some validating of our own. You may have an input field where you’ll only accept non numeric characters. You may want to strip out a certain word. WordPress does some validating, but you may want to go a step further and restrict the input data yourself.

WordPress Settings API

It’s very easy to do this.  Let’s revisit our plugin we created in previous tutorials and open up the display-admin.inc.php file. Search for the register_setting() function and update it to this.

As you can see, we passed in 1 more argument. This 3rd parameter is the name of the function that will validate the data inputted in the form. Let’s create this function and see what it does.

Alright, pretty simple for what’s going on here. You’ll notice we’re passed the value that the user inputted in the form.Remember that we used an array, so we’re given an array and must return an array. Before we return the array, we want to make sure that the user didn’t input any white space for their YouTube username as YouTube doesn’t allow any whitespace upon registration.

You’re more than free to add more validation. Let’s do the same thing to the rest of the values passed in the array.

Your plugin should look something like this by the end of it all.

WordPress Settings API
WordPress Settings API

Conclusion

The past 3 tutorials have been about the WordPress settings API. Of course, there’s more functions related to it, but these are the ones you’ll be using most. If you would like to know more about the WordPress Settings API then click here. It’s much easier to use the WordPress settings API. Even though it’s a bit of code to use, it’ll make your plugins run efficiently and easier to understand.

About Jasko Koyn

Check Also

WordPress Meta Boxes

WordPress Meta Boxes – WordPress Plugin Tutorial

WordPress meta boxes are used to display certain information or options in a page. It’s …

Leave a Reply

Your email address will not be published. Required fields are marked *

ULTIMATE WORDPRESS DEVELOPMENT COURSE
Want to learn how to create your own custom WordPress plugins and themes? Check out my course!
Get 93% off my course. You'll also learn how to integrate WooCommerce and BuddyPress into your projects. This is a limited time offer
Yes, I would like to get 93% off your course!
No Thanks!
close-link