How to Create a Custom WordPress Plugin in Just 3 Steps

Custom WordPress Plugin in Just 3 Steps

How to create a custom wordpress plugin? Step-by-Step tutorial with Sample Code

In this tutorial, we will learn how to create your own custom WordPress plugin from scratch.

What Is WordPress Plugin?

Because you are here, you already know what the WordPress plugin is. But in sort, I tell you so that if you are a new WordPress Developer, then you should also know what the plugin is?

WordPress provides us the basic CMS functionality but we need such functionality that is not in the core WordPress, then we have to use the WordPress plugin in this case.

In short, WordPress plugins are used to increase the core functionality of WordPress.

Why would you want to create a custom WordPress plugin?

There are so many plugins available on the WordPress store but sometimes we have some special requirements or there are some special requirements of our client that do not match any plugin, in that case, we will have to make a custom plugin from scratch which our requirements are fulfilled. And you can also use the custom WordPress plugins in many other websites letter on.

Ok, So how to create your own custom wordpress plugin from the scratch? Well, you are in right place.

Here is a custom WordPress plugin functionality and what we are going to do:

  • We will create a custom WordPress plugin called “Hello MyCurio”.
  • The plugin will have the following functionality.
  • The plugin will have a new menu “Hello MyCurio” in the WordPress admin.
  • The plugin will have the admin page.
  • The plugin will have one text field and submit button.
  • Users can save options by submitting the form.

So let’s get started…

Step 1: Create The Main Plugin File

First, go to the plugins directory, you can find the plugins directory under the wp-content/plugins

Then create a new folder into the plugins directory called “Hello MyCurio

Now go to the “Hello MyCurio” directory and create a new PHP file called “hello-mycurio.php”

Custom WordPress Plugin in Just 3 Steps

Now paste the following code into the “hello-mycurio.php

<?php
/**
 * Hello MyCurio Plugin is the sample WordPress plugin for beginners.
 *
 * @wordpress-plugin
 * Plugin Name: Hello MyCurio
 * Description: Hello MyCurio Plugin is the sample WordPress plugin for beginner.
 * Contributors: MyCurio
 * Author: MyCurio
 * Version: 1.0
 * Text Domain: hello-mycurio
 * Plugin URI: https://mycurio.info/
 * Author URI: https://mycurio.info/
 */

That’s it. Now you can see your plugin under the plugins area in wordpress admin.

Custom WordPress Plugin in Just 3 Steps

Now you can activate the plugin by clicking to Activate.

Step 2: Create an admin menu and page

Okay, so now we’re going to create one admin menu and page from this plugin.

So to add an admin menu and page, we will use the following code.

Here I have used the wordpress action to add a menu in admin.

/**
 * Register a custom menu page.
 */
function mycurio_my_custom_menu_page(){
    add_menu_page( 
        __( 'Hello MyCurio', 'hello-mycurio' ),
        'Hello Mycurio',
        'manage_options',
        'mycurio',
        'mycurio_custom_menu_page',
        plugins_url( 'hello-mycurio/images/icon.png' ),
        6
    ); 
}
add_action( 'admin_menu', 'mycurio_my_custom_menu_page' );

Now you can see the one menu in the admin.

Custom WordPress Plugin in Just 3 Steps

Step 3: Create a Settings fields to the page

Ok, so now we’re going to create the options settings to the page. 

So to create options settings, we use the following code snippet 

/**
 * Settings page.
 */
function mycurio_custom_menu_page()
{
?>
<div class="wrap">
	<h1>Hello MyCurio Plugin</h1>
    <?php settings_errors(); ?>
	<form method="post" action="options.php">
        <?php
        settings_fields("hello_mycurio_settings");
        do_settings_sections("hello-mycurio");
        submit_button("Save Settings");
        ?>
    </form>
</div> 
<?php
}

/**
 * Add a settings.
 */
function hello_mycurio_settings() {
	add_settings_section("hello_mycurio_settings", "", null, "hello-mycurio");
	add_settings_field("hello-mycurio-text", "Add some text", "hello_mycurio_options", "hello-mycurio", "hello_mycurio_settings");
	register_setting("hello_mycurio_settings", "hello-mycurio-text");
}
add_action("admin_init", "hello_mycurio_settings");

/**
 * Set a options.
 */
function hello_mycurio_options() {
?>
    <div>
        <input type="text" name="hello-mycurio-text" value="<?php echo esc_attr(get_option('hello-mycurio-text')); ?>" />
    </div>
<?php
}
Custom WordPress Plugin in Just 3 Steps

All done !! now your plugin works fine. now you can use this sample as a plugin boilerplate and make changes as per your requirements.

Full sample plugin code

<?php
/**
 * Hello MyCurio Plugin is the sample WordPress plugin for beginners.
 * Take this as a base plugin and modify it as per your need.
 *
 * @wordpress-plugin
 * Plugin Name: Hello MyCurio
 * Description: Hello MyCurio Plugin is the sample WordPress plugin for beginner.
 * Contributors: MyCurio
 * Author: MyCurio
 * Version: 1.0
 * Text Domain: hello-mycurio
 * Plugin URI: https://mycurio.info/
 * Author URI: https://mycurio.info/
 */

/**
 * Register a custom menu page.
 */
function mycurio_my_custom_menu_page(){
    add_menu_page( 
        __( 'Hello MyCurio', 'hello-mycurio' ),
        'Hello Mycurio',
        'manage_options',
        'mycurio',
        'mycurio_custom_menu_page',
        plugins_url( 'hello-mycurio/images/icon.png' ),
        6
    ); 
}
add_action( 'admin_menu', 'mycurio_my_custom_menu_page' );

/**
 * Settings page.
 */
function mycurio_custom_menu_page()
{
?>
<div class="wrap">
	<h1>Hello MyCurio Plugin</h1>
    <?php settings_errors(); ?>
	<form method="post" action="options.php">
        <?php
        settings_fields("hello_mycurio_settings");
        do_settings_sections("hello-mycurio");
        submit_button("Save Settings");
        ?>
    </form>
</div> 
<?php
}

/**
 * Add a settings.
 */
function hello_mycurio_settings() {
	add_settings_section("hello_mycurio_settings", "", null, "hello-mycurio");
	add_settings_field("hello-mycurio-text", "Add some text", "hello_mycurio_options", "hello-mycurio", "hello_mycurio_settings");
	register_setting("hello_mycurio_settings", "hello-mycurio-text");
}
add_action("admin_init", "hello_mycurio_settings");

/**
 * Set a options.
 */
function hello_mycurio_options() {
?>
    <div>
        <input type="text" name="hello-mycurio-text" value="<?php echo esc_attr(get_option('hello-mycurio-text')); ?>" />
    </div>
<?php
}

You can also download this sample plugin from the Github repository.

I hope this article is helpful for you and you get the idea of how to create a custom wordpress plugin from scratch.

Our more popular articles.

MySQL Query to Migrate WordPress Multisite Database
MySQL queries to Change WordPress Website Domain in Database
How to Install WordPress Locally – WordPress Installation Tutorial For Beginner
How to Install JWT Auth in Laravel Project
How to Install and use WP CLI in the WordPress Website Easily
Share your love

Leave a Reply