How to create your first WordPress plugin and settings page

By the end of this tutorial, we’ll be able to:

Initiate a WordPress plugin

Create an options/settings page for this plugin

Requirements:

Some basic knowledge of HTML

To get you guys started, browse the root of your site or local installation, and then browse wp-content/plugins/ folder, and then, create a new folder there with the name of your plugin. I am going to name it my-awesome-plugin

Creating my-awesome-plugin wordpress plugin folder

Next, open this file you have just came to create, and there, we’re going to open a new PHP file where we could add our basic code.

In the folder my-awesome-plugin, create a new PHP file and name it what ever you could think of, index.php, awesome.php etc; WordPress will look for this file where our actions and filters will reside..

I am going to name it loader.php for the sake of this tutorial. I am running a WAMP server on windows 7, to let you know

creating main plugin file WordPress

Now, there are some basic requirements for a WordPress plugin to work, some tags that you need to add information about your plugin to, in order for WordPress to accept it:

Great!! and we have some exciting news, WordPress has recognized the plugin and you can activate it:

my awesome plugin recognized by WordPress, activated

OK great, again. Now, this tutorial is not supposed to teach you what you should do in your plugin; you should be creating and think of something cool and then, play around and have fun with the code and get that done. Instead, we’re going to show you how to add an admin settings page in WordPress admin interface, and add options fields which you can update.

First off, in the process of adding this options page, we will apply admin_menu action hook and add_options_page to register an admin menu and an admin page in the settings section:

The saving the code, we can see that a new item is added to settings menu. That’s our awesome plugin!

my awesome plugin settings menu item

Great. now we want to get started with the settings updating process. First, we call admin_init action hook to register a settings group for our plugin, thus we register the first option “my awesome plugin name”:

Now we will add an HTML form with fields, the form action will be options.php, method will be post, and we will add input fields and set their name attributes to match our options that we registered through register_settings function. We will also want to call settings_fields and do_settings_sections functions inside our form and add the settings field name we registered, as a parameter for those functions: