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
- Include the library
- Extend the library class
- Define the setUp() method
- Define the callback methods.
- Instantiate the extended class
Screenshot
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.
1 |
include( dirname( __FILE__ ) . '/library/apf/admin-page-framework.php' ); |
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.
1 2 3 |
class APF_CreatePage extends AdminPageFramework { // ... our code goes here } |
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:
- which root (top-level) menu page that the creating page belongs to.
- the title of the page.
- 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.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
public function setUp() { // Create the root menu - specifies to which parent menu to add. $this->setRootMenuPage( 'Settings' ); // Add the sub menus and the pages. $this->addSubMenuItems( array( 'title' => '1. My First Setting Page', // page and menu title 'page_slug' => 'my_first_settings_page' // page slug ) ); } |
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.
43 44 45 46 47 48 49 50 |
public function do_my_first_settings_page() { ?> <h3>Action Hook</h3> <p>This is inserted by the 'do_' + page slug method.</p> <?php } |
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.
1 2 3 4 5 6 |
function doMyFirstSettingsPage( $oFactory ) { // do something here. } add_action( 'do_my_first_settings_page', 'doMyFirstSettingsPage' ); |
Instantiate Your Extended Class
Lastly, we instantiate the class. Unless it is instantiated, it will do nothing.
55 |
new APF_CreatePage; |
See it’s easy and simple, huh?
Code
Try the code below and post comments if you get a problem.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
<?php /* Plugin Name: Admin Page Framework Tutorial 01 - Create an Admin Page Plugin URI: http://en.michaeluno.jp/admin-page-framework Description: Creates an admin page with Admin Page Framework v3 Author: Michael Uno Author URI: http://michaeluno.jp Version: 1.0.2 Requirements: PHP 5.2.4 or above, WordPress 3.4 or above. Admin Page Framework 3.0.0 or above */ include( dirname( __FILE__ ) . '/library/apf/admin-page-framework.php' ); // Extend the class class APF_CreatePage extends AdminPageFramework { /** * The set-up method which is triggered automatically with the 'wp_loaded' hook. * * Here we define the setup() method to set how many pages, page titles and icons etc. */ public function setUp() { // Create the root menu - specifies to which parent menu to add. $this->setRootMenuPage( 'Settings' ); // Add the sub menus and the pages. $this->addSubMenuItems( array( 'title' => '1. My First Setting Page', // page and menu title 'page_slug' => 'my_first_settings_page' // page slug ) ); } /** * One of the pre-defined methods which is triggered when the page contents is going to be rendered. * * Notice that the name of the method is 'do_' + the page slug. * So the slug should not contain characters which cannot be used in function names such as dots and hyphens. */ public function do_my_first_settings_page() { ?> <h3>Action Hook</h3> <p>This is inserted by the 'do_' + page slug method.</p> <?php } } // Instantiate the class object. new APF_CreatePage; // That's it!! See, it's very short and easy, huh? |
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 ?
Yes. There are already bunch of plugins accepted using this framework.
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?
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.
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
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
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 classMyPlugin_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.
Thank you,
I will try that. Didn’t mean to bother, but I had no clue.
Thanks again!
“setRootMenuPage() and addSubMenuItems() methods”
The links are 404 errors.
Thanks for the heads up! Fixed them.