mermberfix logo x
The MemberFix Team’s got you covered.
Membership plugins

WordPress Custom Plugin Development (Step By Step)

CUSTOM PLUGIN

Given the open-source nature of WordPress, it needs to be highly customizable.

Therefore the developers and the community added a plugin system that allows you to extend it in any way you want.

This includes everything from advanced design changes to the proper back-end work of integrating with some kind of 3rd party service.

And of course it also includes implementing any kind of custom functionality.

And that’s where WordPress custom plugin development comes in.

Why Develop A Custom WordPress Plugin?

Developing WordPress plugins is the core of being a professional WordPress developer.

You may need to integrate your site with a 3rd party app like ClickUp.

Or you may need to create a third-party payment gateway for MemberPress.

Or the crown jewel of WordPress, you may need to design some custom functionality that helps your WooCommerce shop.

What You Need To Create A Custom WordPress Plugin

1 – A computer with either Windows, Linux, or macOS

2 – Basic understanding of programming principles and OOP

3 – Basic understanding of PHP

4 – A code editor like Visual Studio Code or an IDE like PHPStorm

5 – A local environment that can run WordPress

WordPress Custom Plugin Development Key Terms

WordPress custom plugin development is a very complicated practice.

WordPress spent a lot of work and dedicated an entire, giant section on their site just to document it!

You can find all of the documentation here.

But on a simple level, here are the main concepts you should be looking out for.

1 – Actions & Filters – Gives you the ability to inject code into different processes that are running inside of your website.

2 – WP_User – A PHP class that helps you manage users and has a lot of helper methods that you can use.

3 – $wpdb – The interface that WordPress has so that you can work with the database.

Although there are better third-party alternatives you can use like Better WPDB.

4 – Options API – A set of functions that help you save different settings and options into the database.

5 – User Metadata – A set of functions that help you store different data about users.

6 – Post Types – The different types of posts you can add to your website.

7 – Post Metadata – A set of functions that help you store different data about different posts.

How To Develop A Custom WordPress Plugin

Step 1 – Create a folder

Create a new folder in your website’s plugins folder.

Usually, the path to that looks something like this:

/wp-content/plugins/my-new-plugin

Step 2 – Create your first file

Create a new file that has the same name as the folder.

That would look something like this:

/wp-content/plugins/my-new-plugin/my-new-plugin.php

Step 3 – Add the plugin header

Add the plugin header comment as required by WordPress. It would look something like this

/**
 * Plugin Name:       My New Plugin
 * Plugin URI:        https://memberfix.rocks
 * Description:       Handle the basics with this plugin.
 * Version:           1.10.3
 * Requires at least: 5.2
 * Requires PHP:      7.2
 * Author:            Sorin Marta @ MemberFx
 * Author URI:        https://sorinmarta.com
 * License:           GPL v2 or later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Update URI:        https://example.com/my-plugin/
 * Text Domain:       my-basics-plugin
 * Domain Path:       /languages
 */

Step 4 – Add your functionality

For example, here is a piece of code that’s going to add an admin page with a form for some settings that will then be saved in the database.

<?php

/**
 * Plugin Name:       My Basics Plugin
 * Plugin URI:        https://example.com/plugins/the-basics/
 * Description:       Handle the basics with this plugin.
 * Version:           1.10.3
 * Requires at least: 5.2
 * Requires PHP:      7.2
 * Author:            John Smith
 * Author URI:        https://author.example.com/
 * License:           GPL v2 or later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Update URI:        https://example.com/my-plugin/
 * Text Domain:       my-basics-plugin
 * Domain Path:       /languages
 */

class My_Basic_Plugin{
    public function __construct(): void
    {
        add_action('admin_menu', array($this, 'pages'));
        add_action('admin_post_my-settings-form', array($this, 'form_callback'));
    }

    public function pages(): void
    {
        add_menu_page('My Settings Page', 'My Settings Page', 'manage_options', 'my-settings-page', array($this->page_callback()));
    }

    public function page_callback(): void
    {
        ?>

            <form>
                <input type="hidden" name="action" value="my-settings-form">
                <input type="hidden" name="nonce" value="<?php echo wp_create_nonce('my-settings-form'); ?>">
                <div class="form-group">
                    <label for="setting-1">Setting 1*</label>
                    <input type="text" id="setting-1" name="setting-1" required>
                </div>
                <div class="form-group">
                    <label for="setting-2">Setting 2*</label>
                    <input type="text" id="setting-2" name="setting-2" required>
                </div>
                <div class="form-group">
                    <label for="setting-3">Setting 3</label>
                    <input type="text" id="setting-3" name="setting-3">
                </div>
                <div class="form-group">
                    <label for="setting-4">Setting 4</label>
                    <input type="text" id="setting-4" name="setting-4">
                </div>
                <div>
                    <input type="submit">
                </div>
            </form>

        <?php
    }

    public function form_callback(): mixed
    {
        if(!isset($_POST['setting-1']) || !isset($_POST['setting-2'])){
            return wp_redirect('/wp-admin');
        }

        $this->add_or_update_option('setting-1', $_POST['setting-1']);
        $this->add_or_update_option('setting-2', $_POST['setting-2']);

        if(isset($_POST['setting-3'])){
            $this->add_or_update_option('setting-3', $_POST['setting-3']);
        }

        if(isset($_POST['setting-4'])){
            $this->add_or_update_option('setting-4', $_POST['setting-4']);
        }

        return wp_redirect('/wp-admin');
    }

    private function add_or_update_option($option, $value): mixed
    {
        if(get_option($option)){
            return update_option($option, $value);
        }

        return add_option($option, $value);
    }
}

new My_Basic_Plugin();

Can You Customize A Plugin In WordPress?

If you did not build the plugin in the first place, I do not recommend that you customize anything plugins yourself.

Changing a plugin’s code may mean that you won’t be able to update it when the original developer releases an update.

Or, if you do update it, you might delete all of your changes.

Do You Need To License Your Custom WordPress Plugin?

Licensing your plugin isn’t required for the development itself.

However, it’s a good practice to use some kind of a license so that other developers and users know how to operate with your work.

If you just want it to be publicly available, you can use the GPL license.

Or, if you are planning to sell your plugin or do something else with it you can look up a premium license that you can use.

How To Get Your Plugin In The WordPress Plugin Directory

You need to make sure your plugin respects the WordPress guidelines and then you can submit it to the WordPress repository as detailed on this page.

Here is a list of the most important guidelines:

  • The plugin needs to be GPL licensed
  • Do not obfuscate or encrypt any part of the code
  • Do not have a trial offer
  • Respect GDPR
  • Should not require a 3rd-party server or application to operate
  • Should not have any not moral functionality
  • Should not contain any security vulnerabilities
  • Should not assume control of the administrator’s abilities
  • Should respect the WordPress coding standards

Final Thoughts

That should get you started with your WordPress custom plugin development journey.

Please let us know in the comments what plugin ideas you have or even share some work you’ve already done!

You may also enjoy...

WordPress based membership sites have certain requirements, and make use of certain applications that, in my experience, makes most of the popular hosting providers a poor choice.
In this artcle you will learn how to easily create a Divi child theme.
Embedding JWPlayer In An OptimizePress Video Lightbox Element.

Content Teaser Sneak Peek ???? In this article, I’ll show you how to create a content teaser with a nice fade away effect for your paid posts and premium content.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments