Create a WordPress Admin Page

Your First WordPress Admin Page

Let’s create your first admin page. In order for your users to save settings, you need a setting form and a place to display it. So you need to create an admin page first. Let’s create one. It’s really simple.

Steps to Create a WordPress Admin Page

  1. Include the library
  2. Extend the library class
  3. Define the setUp() method
  4. Define the callback methods.
  5. Instantiate the extended class

Screenshot

Create a WordPress Admin Page with Admin Page Framework

Include the Library

First you need to include the bootstrap file of the framework. The path may be different in your environment. The file name to include is admin-page-framework.php. So make sure you set your correct path here.

Extend the Class

Next, we need to decide a class name to extend the framework abstract class. Here, as an example we use APF_CreatePage. It can be whatever you want but it should consist of alphabetical characters and underscores.

Define the setUp() Method

The setUp() method is defined by the framework internally and automatically triggered by the wp_loaded hook. In the method, we define pages, like which root menu, what sub-pages to add etc.

To get started, let’s create a page that displays simple text.

For that, we need to decide:

  1. which root (top-level) menu page that the creating page belongs to.
  2. the title of the page.
  3. the slug (part of url) of the page.

The created page will be a sub-page of the root page you specify. Let’s use the Settings page as the root page and “1. My First Setting Page” as the page title and “my_first_settings_page.” as the page slug. We are going to use the setRootMenuPage() and addSubMenuItems() methods. The used page slug should only consist of alphabets, numbers, and underscores. Dots(.) and hyphens, dashes(-) should be avoided because the slug becomes a part of the callback method name.

Define Callbacks

Next, we define callback methods that are triggered when a certain framework element is processed internally.

Here we use the do_ + page slug method as an example. This is triggered in the middle of framework’s rendering the content of the page. There are a lot of callback methods besides it. For the other hooks and callbacks, refer to the manual.

As a side node, the method name serves as a filter/action hook name. As you add more methods, the class becomes bigger and bigger and harder to maintain. In that case, you can use the WordPress core functions, add_filter() and add_action() and define the callbacks outside the class. For the above example, the action hook name is do_my_first_settings_page, the same as the method name.

Instantiate Your Extended Class

Lastly, we instantiate the class. Unless it is instantiated, it will do nothing.

See it’s easy and simple, huh?

Code

Try the code below and post comments if you get a problem.

10 thoughts on “Create a WordPress Admin Page”

  1. Nice plugin. I have one question regarding development of my custom plugin.
    If I developed a plugin using this framework is that accepted by WordPress and publish on plugin repository ?

  2. Hi, the thing looks very helpful but please add some info at the start of this tutorial. “Include the library” isn’t the step one but rather “create new php file”? Where?

    1. Hello,

      By hearing such a question, you must be unfamiliar with developing plugins or themes. This library is for WordPress plugin or theme developers who are already familiar with writing a plugin or theme.

      For you to get started, try these.

      https://developer.wordpress.org/themes/getting-started/
      https://developer.wordpress.org/plugins/the-basics/

      And come back when you find that you need a tool to speed up development with admin pages and forms.

  3. Hi Jason,

    I’m intrigued by your framework, it looks like it will be fun to play with. My question is how do you define single site when it applies to WPMU? Every has a different answer, I have to ask.

    Thanks

  4. Hi,

    This framework looks promising, but I cannot figure out how to use it correctly. I followed the above, but that class is deprecated.

    Is says to use the framework downloader, which I have, but all the classes in the main file are different than what is in the tutorial.

    Any other tutorials with up to date information?

    Thanks

    1. Hi Jason,

      The class name you extend should be prefixed with the prefix set in the form when you download the generated version. For example, if you set MyPlugin_ in the form of the generator, you need to extend the class MyPlugin_AdminPageFramewrok in your project files. It is recommended to read through the instruction accessed via Dashboard -> Admin Page Framework -> Help -> Getting Started.

      This tutorial was written before the framework generator was implemented and should be updated. Thanks for asking about this.

Leave a Reply

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