Creating Custom Options Pages
https://rudrastyh.com/wordpress/creating-options-pages.html
Last updated
Was this helpful?
https://rudrastyh.com/wordpress/creating-options-pages.html
Last updated
Was this helpful?
First of all please check the screenshot, it is what we are going to create in this tutorial:
The first step for us is to decide where exactly in admin menu we are going to create an option page.The good news – you can create it anywhere, whether it is in the Settings section, Dashboard section or a new custom section with an icon.
There are primarily two functions for that:
add_menu_page()
– to create a new top-level menu page,
add_submenu_page()
– to create a submenu page.
To simplify the process, instead of add_submenu_page()
you can use add_dashboard_page()
, add_posts_page()
, add_pages_page()
, add_comments_page()
, add_theme_page()
, add_plugins_page()
, add_users_page()
, add_management_page()
or add_options_page()
depending on where you’re going to add your submenu page.
Okay, let’s dive into the examples.
Take a look at this code:
If you don’t understand how I have added an icon there, just check the Dashicons icon set.
Creating a new menu item with an icon is cool but the truth is when you create a plugin settings page, it is always better to add it as a submenu page under Settings.
It is the best practice and in most cases I recommend you to do it that way.
Now you already know how to add any custom HTML to your options pages, but in the next chapter I will show you how to properly create settings fields there.
First of all let’s populate our misha_page_content()
function which we created in the previous step.
We are not going to make any changes in this functions anymore. All you have to know is that we use two functions settings_fields()
and do_settings_sections()
to print all the settings fields and to populate them with data.
What is esc_attr()
function and why it is here? There is a separate tutorial on my blog about escaping outputs where I said that database is not a trusted source of data and that’s why we must use escaping functions before we output anything into the page content.
Let’s come back for a moment to a register_setting()
function from the previous piece of code.
The third parameter is the function name which is intended to sanitize (clean) the field data before saving into the database. For example sanitize_text_field()
is the default WordPress function that removes all the characters that are not allowed in use in an input text field. If you would like to to pass an integer, you can use intval()
or absint()
function names instead. Or create a custom sanitization function for your purpose.
Example for positive integers:
We have some code above. Actually a lot of code. And it is all about to create a single input field.
I want to be honest with you – I never do all that manually on my projects. Because it is so time consuming. What is the solutions then? I also don’t like plugins, especially ACF-like ones. Because I’d better spend a couple more hours and create a fast website instead of using slow bloated plugins.
Finally I came to a solution – I created my own option pages plugin with the bare minimum of the functionality. It is lightning fast just like you code everything in functions.php
, but at the same time it saves hours of development.
Just compare this code to everything we did above:
It is super simple, but if you insert it to your functions.php
file, a new admin page will appear.
Very similar to the previous example, isn’t it? We only don’t have an icon parameter anymore.
If you did everything correctly until this moment, you will have this:
And now we have a working options page:
And we have absolutely the same result as we have creating an option page from scratch in the first part of the tutorial.