Using WordPress Settings API: Part I
Learn to create a form with sections and fields and let WordPress loop through them to display the settings page for the plugin.
Plugin settings page
In this lesson, we will create a settings page for the plugin. For example, maybe a user would only want to display the read time of a post. Or maybe some users want to show the word and paragraph count of the post along with the read time. This is where the plugin setting screen comes into play. We will let the user choose the following:
Select the position of the metrics at the start or end of the post.
Checkboxes to display the read time, word count, and paragraph count.
Enter the words per minute to calculate the read time.
We will create a settings page for the plugin and add a link in the admin sidebar that leads to our plugin settings page.
To add a settings page we will add an action to the admin_menu
hook. At this hook, we will call a function. Instead of coming up with a unique function name, we will use an alternate approach.
PHP classes
Coming up with unique function names is a tiring process. It also makes the names very long. By using classes we can get rid of the unique name restriction as the functions are scoped inside the class and will not clash with other functions across the WordPress platform.
We only need to ensure that the class name is unique. Let’s call our class PostMetricsPlugin
. Open the post-metrics-plugin.php
file created in the last lesson. Delete the add_metrics_to_post
method and in its place, define the PostMetricsPlugin
class below the comment section as follows:
<?php/*Plugin Name: Post Metrics PluginDescription: A plugin that shows different metrics about a post like the number of words and paragraphs and reading time.Version: 1.0Author: Datajek*/class PostMetricsPlugin {}
Below the class, we need to instantiate or create an object of the class. We are calling the object $postMetricsPlugin
.
<?php/**/class PostMetricsPlugin {}$postMetricsPlugin = new PostMetricsPlugin();
When the object of the class is created, PHP calls the constructor function to initialize the object. This function is defined as __construct()
. We can include any top level actions inside the constructor.
<?phpclass PostMetricsPlugin {function __construct(){}}
Add a menu option to admin panel
In WordPress, the admin_menu
action hook is used to add items to the administration menu. When it is triggered, it provides an opportunity to add new menus, submenus, or menu items to the WordPress dashboard. It can also be used to modify or remove existing menus. The admin_menu
hook is fired after the basic admin panel menu structure has been set up, but before any menu items are displayed. This means that it's an ideal place to add custom menu items and submenus.
We will use the add_action()
function to attach a callback function to the admin_menu
hook. This function will be called when the menu is displayed. Now the function name does not have to be long and unique. We can use a more intuitive function name like pmp_menu
. Since we are in a PHP class, we will use an array callable syntax to call the function. The first argument is an object which points to the current instance of this class using $this
variable. The second argument is the name of the method pmp_menu
.
<?phpclass PostMetricsPlugin {function __construct(){add_action('admin_menu', array($this, 'pmp_menu'));}}
Creating the plugin settings page
Inside the pmp_menu
function, we will write code to add a link inside the WordPress admin settings menu. ...