WordPress Empty Plugin

How to create a Wordpress plugin

When I have to create a new WordPress functionality I always use an empty plugin to start with. Here I’m going to explain how to develop your WordPress plugin. If you are not interested in this tutorial you can just download the empty plugin:

STEP 1: create the plugin

First of all choose a name for your plugin: in this tutorial is Empty Plugin; let’s create a folder called empty-plugin inside wp-content/plugins. Then in this folder create a PHP file with the same name of the plugin folder; in my case I named it empty-plugin.php. To make WordPress recognize your plugin, you have to place the following comment in the head of the file you just created:

<?php
/*
Plugin Name: Empty plugin
Plugin URI: http://www.yourwebsitename.com/visit_plugin_website
Description: An empty wordpress plugin
Author: John Doe
Author URI: http://www.yourwebsitename.com/plugin_by
Version: 1.0.0
*/
If go to the plugin section you can see your plugin ready to be activated: wordpress_empty_plugin_screenshot

STEP 2: create the menu entries

The following code creates the entry Empty Plugin in the administration menu and the sub entry Add new:

function empty_plugin_create_menu_entry() {

add_menu_page('Empty Plugin’,
'Empty Plugin’,
'edit_posts’,
'main-page-empty-plugin’,
’empty_plugin_show_main_page’,
plugins_url('/images/empy-plugin-icon-16.png’, __FILE__) );

add_submenu_page('main-page-empty-plugin’,
'Add New’,
'Add New’,
'edit_posts’,
'add-edit-empty-plugin?action=add_new’,
’empty_plugin_add_another_page’ );
}

add_action('admin_menu’, ’empty_plugin_create_menu_entry’);

where add_menu_page inserts the entry in the admin menu:

add_menu_page($page_title, $menu_title, $capability, $menu_slug, $function, $icon_image)
  1. Page title (required): in my case Empty Plugin
  2. Menu title (required): in my case Empty Plugin
  3. Capability (required): the capability of the logged user must have to interact with the plugin (for more info click here: https://codex.wordpress.org/Roles_and_Capabilities)
  4. Menu slug (main-page-empty-plugin): a unique slug to identify the menu
  5. Function that has to be triggered (my case: empty_plugin_show_main_page): function name to output the page content (check the STEP 3)
  6. Icon image path

To add a sub menu entry we used add_submenu_page: add_submenu_page($parent_slug, $page_title, $menu_title, $capability, $submenu_slug, $function)

  1. Parent menu slug: this field links the sub menu entry with the parent menu.
  2. Page title
  3. Menu title
  4. Capability
  5. Submenu slug
  6. Function to trigger to output the content for this page (check the STEP 3)

Now, we have to tell to wordpress to execute our function empty_plugin_create_menu_entry when it is loading the admin menu:

add_action('admin_menu’, ’empty_plugin_create_menu_entry’);

with add_action you are attaching the function named empty_plugin_create_menu_entry to the action admin_menu. This is the final result:

wordpress plugin admin menu

STEP 3: create the plugin pages

When we used add_menu_page we choose to trigger the function empty_plugin_show_main_page and in add_submenu_page we choose to trigger empty_plugin_add_another_page; so let’s declare these two functions:

function empty_plugin_show_main_page() {
include('main-page-empty-plugin.php’);
}

function empty_plugin_add_another_page() {
include('another-page-empty-plugin.php’);
}

The files included within the two functions above contains the PHP/HTML code of the page. In the administration pages you can also include CSS and JS files in this way:

wp_enqueue_style(’empty-plugin-style’, plugins_url( '/css/style.css’, __FILE__ ) );
wp_enqueue_script(’empty-plugin-scripts’, plugins_url( '/js/scripts.js’, __FILE__ ) );

If you write those two functions in the beginning of an administration page (eg: main-page-empty-plugin.php) you include the CSS and the JS file only in that administration page. But if you write those two functions in the main plugin file you will include the CSS/JS file in all the pages of your website. So be very careful were you place wp_enqueue_style and wp_enqueue_script.

Other tips

To avoid to declare two different functions with the same name, you can add a prefix to your functions like my_plugin_get_option().
When you build your plugin don’t put PHP, CSS and JS file in the same folder; give a clean structure to the plugin grouping every kind of file in a different folder.
Don’t write all the classes and functions in one file only; better to give to each class its own file _

Related Articles

WP Social Importer

Category: tools

WP Social Importer is a wordpress plugin that imports news from Facebook, Instagram, Twitter into your website. This plugin transforms automatically the news from your social networks into wordpress ...

Brainy: a simple PHP class for machine learning

Category: resources

Brainy is a PHP class that helps you to create your neural network. I built Brainy just for fun during my artificial intelligence studies. If you are a web developer and you have just started ...

Matrix arithmetic PHP Class

Category: resources

During my studies in AI I wanted to solve the XOR problem; I wanted just to do a quick test so I started writing the code in PHP. Solving that problem I ended up to write a class that handles matrix ...

Coming soon page

Category: resources

If you need a coming soon page, don't waste your time on developing it because you are going to use it for a short period. This coming soon page has a minimal design and it's made with PHP, HTML and ...

Free the bean!

Category: resources

This is a JavaScript game. The aim of the game is to free the dots (or the beans) by clicking on the sticks. When the sticks are removed the bean is free, and with every free bean you get a point and ...

Social share buttons

Category: resources

Do you see the social share buttons on the bottom left corner? Ok, I'm going to explain how to build them. It isn't difficult... so if you don't want to read you can download the ...