How to Create a Simple WordPress Plugin?

How to Create a Simple WordPress Plugin

WordPress plugin is beloved by numerous, and truly so. It’s exceptionally simple to use, totally free (or open-source), versatile, and the very best part, rather extensible, WordPress plugin for your website attractively and get more attention, you can extend and include virtually any performance you want.

If you’re the best novice, WordPress plugins that are much like the apps on your phone. Your phone works terrific as it is, however you can include more performance utilizing apps. The exact same uses to the WordPress content management system (CMS).

The WordPress platform is currently effective, however, you can attain a lot more utilizing plugins. For circumstances, the WordPress plugin enables you to include additional performance such as e-commerce, SEO, spam security, advanced types, social networks, much better security, live chat, visual page structure, and a lot more.

There are countless totally free and premium WordPress plugins out there. The official WordPress plugin repository alone has more than 59,000 totally created free plugins at the time of composing! Other markets, such as CodeCanyon, use countless premium WordPress plugins.

That goes to state that there is potentially a plugin for almost any function you require to include in your WordPress site. Still, sometimes, you may require something not available from the abovementioned plugin sources. In that case, you may require to WordPress plugin from scratch or customize (or fork) an existing plugin.

And in today’s post, we will reveal to you how to compose a WordPress plugin, which is great if you desire to create something simple or dive headlong into plugin advancement. With that preamble, let us begin since there is a lot of things to cover.

Writing a Simple WordPress Plugin: The Basics

Before we get to the enjoyable part, there are a number of fundamental things you need to understand. As a novice, you may presume to produce WordPress plugins is a difficult nut to fracture. Well, not actually. Anybody can find out how to compose a plugin whether you have a degree in computer technology or not.

At the exact same time, some PHP coding understanding will go a long method if you prepare to compose more plugins in the future. That’s since WordPress plugins are coded in the PHP shows language. With this understanding, you will much better comprehend how PHP functions work, which makes your work a lot simpler. Still, it’s a simple thing.

Also, depending upon the functions you require, your plugin may consist of simply one file or several files consisting of CSS stylesheets, JavaScript scripts, and media files. Lastly, it’s important to acquaint yourself with WordPress Coding Standards and best practices to remain on the safe side.

Rest simple since today we won’t venture into the complex things. I will reveal you how to compose a simple plugin and share a number of tools and resources that will make your work a breeze. Are you thrilled yet? I sure do hope so 🙂

What You Need to Write a WordPress Plugin

You will require a number of things going on prior to composing your WordPress plugin. For beginners, don’t evaluate your plugin on your live site. If anything fails, you run the risk of knocking out your website, which leads to terrible downtimes while you repair things.

Instead, create a screening or staging site in your area or on your server. Here’s how to:

Alternatively, you can utilize tools such as Local by Flywheel or DevKinsta.

Other than that, you will require a full-screen editor such as Notepad++, SublimeText, or Atom. While it’s not a requirement, you can utilize tools such as Pluginplate or Plugin Boilerplate to accelerate advancement (more about this later).

And now the enjoyable part, let us compose a simple WordPress plugin. For this tutorial, I’m producing a plugin that will include a custom-made post type for a fictional dining establishment website I’m developing.

To the inexperienced, WordPress supports several post types consisting of page, post, accessory, modification, and navigation menu. For our case situation, we will create a simple plugin that includes the “Recipe” custom-made post type. For illustration functions, I will call my plugin “Hot Recipes.”

Let us dive right in.

How to Write a Simple WordPress Plugin

Each WordPress plugin has a primary file that you can create by hand or utilizing tools such as Plugin Boilerplate or Pluginplate. To conserve time, let us utilize Pluginplate (it’s more user-friendly, IMO) to produce the primary file and a number of additional files and folders that you may require later on. At the minute, nevertheless, we merely require the primary file.

Creating the Main File

Head over to Pluginplate.com, and click the Create Your Plugin button, as we highlight listed below.

pluginplate wordpress plugin generator for custom plugins

Next, submit your plugin info as revealed listed below. Towards, the bottom of the page, you will discover the Modules area that enables you to include additional functions to your plugin. Notice, likewise, that you can tailor each module by clicking the Plus (+), as we information listed below. Once you’re pleased, click the Generate Plugin button:

create a custom plugin

After that, click the Download button and conserve your plugin to your computer system.

Now, we have all the fundamental files we require, consisting of the primary file. But don’t pop the bubbly yet, our plugin won’t do anything as it is. We should include the code that will carry out when we trigger the plugin. Based on our example, my primary file is hot-recipes.php, which is the file we will modify in the next area.

Adding Functions

To find the hot-recipes.php mail file, extract the ZIP folder you downloaded from Pluginplate:

Inside the folder, you need to see your primary file, which in our case – once again – is hot-recipes.php:

Inside the plugin folder, you can see a lot of other files however we don’t require those at the minute. Next, let us include some functions in the primary file. Open the primary file (hot-recipes.php) in your preferred full-screen editor (I’m utilizing Notepad++).

You will see the following piece of code or something comparable depending upon how you completed the type at Pluginplate:

<?php
/**
* Hot Recipes
*
* @package HOTRECIPES
* @author Freddy
* @license gplv2-or-later
* @version 1.0.0
*
* @wordpress-plugin
* Plugin Name: Hot Recipes
* Plugin URI: https://vistamedia.me
* Description: The Hot Recipes WordPress plugins adds a custom post type suitable for restaurants.
* Version: 1.0.0
* Author: Freddy
* Author URI: https://vistamedia.me
* Text Domain: hot-recipes
* Domain Path: /languages
* License: GPLv2 or later
* License URI: ;
*
* You should have received a copy of the GNU General Public License
* along with Hot Recipes. If not, see <https://www.gnu.org/licenses/gpl-2.0.html/>.
*/

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) exit;

// Plugin name
define( 'HOTRECIPES_NAME', 'Hot Recipes' );

// Plugin version
define( 'HOTRECIPES_VERSION', '1.0.0' );

// Plugin Root File
define( 'HOTRECIPES_PLUGIN_FILE', __FILE__ );

// Plugin base
define( 'HOTRECIPES_PLUGIN_BASE', plugin_basename( HOTRECIPES_PLUGIN_FILE ) );

// Plugin Folder Path
define( 'HOTRECIPES_PLUGIN_DIR', plugin_dir_path( HOTRECIPES_PLUGIN_FILE ) );

// Plugin Folder URL
define( 'HOTRECIPES_PLUGIN_URL', plugin_dir_url( HOTRECIPES_PLUGIN_FILE ) );

/**
* Load the main class for the core functionality
*/
require_once HOTRECIPES_PLUGIN_DIR . 'core/class-hot-recipes.php';

/**
* The main function to load the only instance
* of our master class.
*
* @author Freddy
* @since 1.0.0
* @return object|Hot_Recipes
*/
function HOTRECIPES() {
	return Hot_Recipes::instance();
}

HOTRECIPES();

Nice, everything looks great. The above code will tell WordPress the name of the plugin as well as the version, author, license, and other details. You don’t need to edit anything. Let us move to the next step.

Just below the above code, add the following code:

/**
 * Registers the recipes post type.
 */
function hot_recipes_register_post_types() {

	// Set UI labels for the recipes post type.
	$labels = array(
		'name' => _x( 'Recipes', 'Post Type General Name', 'hot-recipes' ),
		'singular_name' => _x( 'Recipe', 'Post Type Singular Name', 'hot-recipes' ),
		'menu_name' => __( 'Recipes', 'hot-recipes' ),
		'parent_item_colon' => __( 'Parent Recipe', 'hot-recipes' ),
		'all_items' => __( 'All Recipes', 'hot-recipes' ),
		'view_item' => __( 'View Recipe', 'hot-recipes' ),
		'add_new_item' => __( 'Add New Recipe', 'hot-recipes' ),
		'add_new' => __( 'Add New', 'hot-recipes' ),
		'edit_item' => __( 'Edit Recipe', 'hot-recipes' ),
		'update_item' => __( 'Update Recipe', 'hot-recipes' ),
		'search_items' => __( 'Search Recipe', 'hot-recipes' ),
		'not_found' => __( 'Not Found', 'hot-recipes' ),
		'not_found_in_trash' => __( 'Not found in Trash', 'hot-recipes' ),
	);

	// Set other arguments for the recipes post type.
	$args = array(
		'label' => __( 'recipes', 'hot-recipes' ),
		'description' => __( 'Recipes.', 'hot-recipes' ),
		'labels' => $labels,
		'supports' => array(
			'title',
			'editor',
			'excerpt',
			'author',
			'thumbnail',
			'comments',
			'revisions',
			'custom-fields',
		),
		'taxonomies' => array(),
		'hierarchical' => false,
		'public' => true,
		'show_ui' => true,
		'show_in_menu' => true,
		'show_in_nav_menus' => true,
		'show_in_admin_bar' => true,
		'menu_position' => 5,
		'can_export' => true,
		'has_archive' => true,
		'exclude_from_search' => false,
		'publicly_queryable' => true,
		'capability_type' => 'post',
		'show_in_rest' => true,
	);

	// Registes the recipes post type.
	register_post_type( 'recipes', $args );

}
add_action( 'init', 'hot_recipes_register_post_types' );

The above code simply registers our custom post type “recipes” with an array of options. It also adds features such as support for revisions, custom fields, excerpts, comments, featured images, and so on. These are the features you’ll see inside the post editor when adding a new recipe.

Aside: Now, I wouldn’t worry much about the syntax at this point as a beginner because it’s beyond the scope of today’s post. But with some PHP knowledge, you will understand every part of the above code, and what each part does. Besides, there is plenty of resources and code lying around on the internet to learn and practice.

Compress Your Plugin Folder

Save all your changes. Compress the hot-recipes folder (it’s the folder where you found the hot-recipes.php main file we just edited) into a hot-recipes.zip archive (on a Mac it’s as easy as right-click, compress file – and on PC I believe it’s very similar). Just make sure your folder saves as a .ZIP extension or the plugin won’t install.

Next, log in to your test site and navigate to Plugins > Add New, as shown below.

then, click the Upload Plugin button, choose your plugin ZIP folder from your computer and click Install Now:

Next, Activate the plugin:

Now, if you check your WordPress admin menu, you will notice our new Recipes post type, complete with the ability to add new recipes:

Congrats on writing your first simple plugin! With this introduction, you can go further and play around with the code to see what you can get. Also, you can study the source code of other plugins (all WordPress plugins are open-source) to learn more.

Now all you need is a few more resources (check out the Plugin Handbook) and lots of practice and you will be coding away in no time like the legend you are.


Programming and writing WordPress plugins can seem intimidating at first, especially as a beginner. But with the right tools and some learning resources, you can develop plugins like a boss in no time. It just takes some coding chops and determination and you’re halfway there.

I hope this tutorial pointed you in the right direction as far as understanding plugins go. This post should serve as a stepping stone to developing complex WordPress plugins that do whatever you will. Don’t stop here, check out the resources I recommended above to increase your knowledge of WordPress plugin development.

If you found this tutorial helpful or if you have anything else to add we’d love to know. Please share your thoughts in the comments section below. See you around!

Share On:

Facebook
Twitter
LinkedIn
Pinterest

Leave a Reply

Your email address will not be published.

Get Fresh Content Everyday

Ultimate Guides To Start With

Starting A
Blog

WordPress
Performance

WordpPress
Security

WordPress
SEO

WordPress
Errors

Building an
Online Store

Get The Latest Updates

Subscribe To Our Weekly Newsletter

No spam, notifications only about new posts, updates.