Home How to create an admin page for your WordPress plugin
Post
Cancel

How to create an admin page for your WordPress plugin

In this tutorial, we are going to create an admin page for your WordPress plugin.

If you are using some famous plugins such as Yoast SEO, you can see that they have their own admin menu page that can be seen in the WordPress sidebar menu.

Step 1: Create a plugin file.

We will create a file named as admin.php and the content as follows:

1
2
3
4
5
6
7
8
9
<?php
/* 
Plugin Name: My WordPress Plugin 
Plugin URI: https://wordpress.org/plugins/ 
Description: Just another WordPress plugin. 
Version: 1.0.0 
Author: David Angulo
Author URI: https://www.davidangulo.xyz/
*/

Step 2: Create a function.

1
2
3
function adminPageContent() {     
  echo '<h2>My WordPress Plugin</h2>Hello world!'; 
}

The content of this function will be displayed when we visit the admin page of our plugin.

Step 3: Add an action hook.

1
add_action('admin_menu', 'addAdminPageContent');

Since we are going to add an admin menu to our plugin, we have used the admin_menu in the first parameter.

This hook will allow WordPress to process a specific action. The specific function that I am talking about will be tackled next step.

Step 4: Call the add_menu_page function.

1
2
3
function addAdminPageContent() {    
  add_menu_page('My Plugin', 'My Plugin', 'manage_options', __FILE__, 'adminPageContent', 'dashicons-wordpress'); 
}

The text My Plugin will be the name of our menu. adminPageContent is the name of the function that we have declared in Step 2. dashicons-wordpress will be the icon for our menu. You can select for more in https://developer.wordpress.org/resource/dashicons.

Complete code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php
/*
Plugin Name: My WordPress Plugin
Plugin URI: https://wordpress.org/plugins/
Description: Just another WordPress plugin.
Version: 1.0.0
Author: David Angulo
Author URI: https://www.davidangulo.xyz/
*/

function addAdminPageContent() {
  add_menu_page('My Plugin', 'My Plugin', 'manage_options', __FILE__, 'adminPageContent', 'dashicons-wordpress');
}

function adminPageContent() {
  echo '<h2>My WordPress Plugin</h2>Hello world!';
}

add_action('admin_menu', 'addAdminPageContent');

That’s it, just simply activate the plugin that we have created and it must add a menu in the sidebar with a WordPress logo as the icon and My Plugin as the menu title.

This post is licensed under CC BY 4.0 by the author.

How to create custom WordPress shortcode plugin from scratch

How to upload files in WordPress programatically