Would you like to learn how to create a WordPress Plugin simple?

WordPress is the darling of many, and rightly so. It's incredibly easy to use, free (or open-source), flexible, and the best part, quite extensible. With plugins, you can extend WordPress to add virtually any functionality you have in mind.

If you are the perfect beginner, the WordPress plugins are like the apps on your phone. Your phone works fine as is, but you can add more features using apps. The same goes for the WordPress content management system (CMS).

The WordPress platform is already powerful, but you can do so much more using plugins. For example, the WordPress plugins allow you to add additional features such as e-Commerce, SEO, spam protection, advanced forms, social networks, better security, live chat, visual page creation and much more .

The existe des milliers de WordPress plugins free and premium. THE official WordPress plugin repository alone has over 59 free plugins at the time of writing! Other marketplaces, such as CodeCanyon, offer you thousands of premium WordPress plugins.

This goes to say that there might be a plugin for almost every feature you can add to your WordPress website. Still, sometimes you might need something unavailable from the aforementioned plugin sources. In this case, you may need to create a WordPress Plugin from scratch or modify (or fork) an existing plugin.

And in today's article we are going to show you how to write a WordPress Plugin simple, which is good if you want to create something simple or dive headlong into plugin development. With this preamble, let's get started.

But before, if you have never installed WordPress discover How to Install a WordPress Blog in 7 Steps et How to search, install and activate a WordPress theme on your blog 

Then back to why we are here.

Writing a Simple WordPress Plugin: The Basics

Before we get to the fun part, there are a few basic things you need to know. As a beginner, you might assume that creating WordPress plugins is difficult. Well, not really. Everybody can learn to write a plugin, whether you have a degree in computer science or not.

At the same time, some PHP coding knowledge will help you further if you plan to write more plugins in the future. With this knowledge, you will better understand how PHP functions, which makes your job much easier. Still, it's easy.

Also, depending on the functionality you need, your WordPress plugin may contain one or more files, including CSS style sheets, JavaScript scripts, and media files. Finally, it is important to familiarize yourself with the WordPress coding standards and best practices to stay on the safe side.

Rest easy because today we will not venture into complex things. We'll show you how to write a simple WordPress plugin and share some tools and resources that will make your job easy.

What you need to write a WordPress plugin

You will need a few things before writing your WordPress plugin. For starters, don't test your plugin on your website. If something goes wrong, you risk breaking your website, which can lead to horrible downtime while you fix things.

Instead, create a local test website or on your server. Here's how:

Other than that you will need a text editor such as Notepad ++SublimeText ou Atom. Although not a requirement, you can use tools such as pluginplate ou Boiler plate plugin to speed up development (more on this later).

Now let's write a simple WordPress plugin. For this tutorial, we are going to create a plugin that will add a custom post type for a restaurant website that we are going to create.

For the uninitiated, WordPress supports multiple post types, including pages, posts, attachments, reviews, and navigation menu. For our scenario, we'll create a simple WordPress plugin that adds a "Recipe" custom post type. For illustration purposes, we will call this plugin “Hot Recipes”.

How to Write a Simple WordPress Plugin

Each WordPress plugin has a main file that you can create manually or using tools like Plugin Boilerplate or Pluginplate. To save time, use Pluginplate (it's more intuitive) to generate the main file and some additional files and folders you might need later. For now, we just need the main file.

Creation of the main file

Visit pluginplate.com And click on the button Create your plugin, as we highlight below.

Next, fill in your plugin information as shown below. Towards the bottom of the page, you will notice the section Modules which allows you to add additional functionality to your plugin. Also note that you can customize each module by clicking on the Plus (+), as we detail below. Once you are satisfied, click the button Generate le Plugin :

create a simple WordPress plugin

After that, click on the button Download and save your plugin on your computer.

Now we have all the base files we need from the main file. Our WordPress plugin will not do anything as it is. We need to add the code that will run when we activate the plugin. Based on our example, our main file is hot-recipes.php, which is the file we will edit in the next section.

Adding functions

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

create a simple WordPress plugin

Inside the folder, you should see your main file, which in our case – again – is hot-recipes. Php:

create a simple WordPress plugin

In the plugin folder you can see a bunch of other files, but we don't need them right now. Next, let's add some functions to the main file. Open the main file (hot recipes.php) in your favorite text editor (We use Visual Studio Code).

See also: How to Disable Comments in WordPress

You will see the following code snippet or something similar depending on how you filled out the form on Pluginplate:

<?php
/**
 * Recettes Chaudes
 *
 * @package       RECETTESCH
 * @author        Passi
 * @license       gplv2
 * @version       1.0.0
 *
 * @wordpress-plugin
 * Plugin Name:   Recettes Chaudes
 * Plugin URI:    https://blogpascher.com/
 * Description:   Recettes Chaudes permet de créer un type de post personnalisé pour un restaurant
 * Version:       1.0.0
 * Author:        Passi
 * Author URI:    https://blogpascher.com/
 * Text Domain:   recettes-chaudes
 * Domain Path:   /languages
 * License:       GPLv2
 * License URI:   https://www.gnu.org/licenses/gpl-2.0.html
 *
 * You should have received a copy of the GNU General Public License
 * along with Recettes Chaudes. If not, see <https://www.gnu.org/licenses/gpl-2.0.html/>.
 */

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) exit;
// Plugin name
define( 'RECETTESCH_NAME',			'Recettes Chaudes' );

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

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

// Plugin base
define( 'RECETTESCH_PLUGIN_BASE',	plugin_basename( RECETTESCH_PLUGIN_FILE ) );

// Plugin Folder Path
define( 'RECETTESCH_PLUGIN_DIR',	plugin_dir_path( RECETTESCH_PLUGIN_FILE ) );

// Plugin Folder URL
define( 'RECETTESCH_PLUGIN_URL',	plugin_dir_url( RECETTESCH_PLUGIN_FILE ) );

/**
 * Load the main class for the core functionality
 */
require_once RECETTESCH_PLUGIN_DIR . 'core/class-recettes-chaudes.php';

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

RECETTESCH();

The code above will tell WordPress the name of the plugin along with the version, author, license, and other details. You don't need to modify anything. Let's go to the next step.

Just below the code above, add the following code:

/**
 * Registers the recettes post type.
 */
function recettes_chaudes_register_post_types() {

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

	// Set other arguments for the recettes post type.
	$args = array(
		'label' => __( 'recettes', 'recettes_chaudes' ),
		'description' => __( 'recettes.', 'recettes_chaudes' ),
		'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 recettes post type.
	register_post_type( 'recettes', $args );

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

The code above simply registers our custom “recipes” post type with an array of options. It also adds features like support for reviews, custom fields, excerpts, comments, featured images, and more. These are the features you will see in the post editor when adding a new recipe.

Zip your plugin folder

Save all your changes. Compress the folder hot recipes (this is the folder where you found the main file hot recipes.php  that we just edited) in an archive hot-recipes.zip (on a Mac it's as simple as right-clicking, zipping the file - and on a PC I believe it's very similar). Just make sure your folder is saved with the .ZIP extension or the plugin won't install.

See also: How to add infinite scrolling on a WordPress

Next, log in to your testing website and navigate to Extensions> Add , as shown below.

Then click on the button Upload plugin, choose your plugin ZIP folder on your computer and click install now:

create a simple WordPress plugin

Then, activate The plugin

Now if you check your WordPress admin menu you will notice your new type publication Recipes, with the possibility of adding new recipes:

create a simple WordPress plugin

Congratulations on writing your first simple WordPress plugin! With this introduction, you can go deeper and play around with the code to see what you can achieve. 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 manual) and lots of practice and you'll be coding in no time.

Lire aussi How to Remove the “Proudly Powered by WordPress” Link

Programming and writing WordPress plugins can seem daunting at first, especially as a beginner. But with the right tools and a few learning resources, you can develop plugins like a boss. It will be enough to have some notions of coding and determination.

Other recommended resources

We also invite you to consult the resources below to go further in the grip and control of your website and blog.

Conclusion

That's it for this tutorial. We hope it pointed you in the right direction when it comes to understanding plugins. This article should serve as a springboard for developing complex WordPress plugins that do everything you want them to.

If you have any concerns or suggestions, please let us know within Comments. However, you can also consult our resources, if you need more elements to carry out your projects of creation of Internet sites, by consulting our guide on the WordPress blog creation or the one on Divi: the best WordPress theme of all time.

Waiting, share this article on your different social networks.   

...