WordPress Tutorial
Lesson 43 of 50 (86%)
86% Complete

How to Create a Custom WordPress Plugin

Learn how to create your own WordPress plugin using PHP. You will build a simple plugin, add a custom admin notice, create a shortcode, and display dynamic content on your website.

Learning Objective: Understand WordPress plugins, create a plugin directory and main PHP file, use WordPress hooks, register a shortcode, and activate your custom plugin safely.

1. What Is a WordPress Plugin?

A WordPress plugin is a package of code that adds new features or extends the functionality of a WordPress website without requiring you to modify WordPress core files.

Plugins can be used to add features such as:

Website Features
  • Contact forms
  • Image galleries
  • SEO tools
  • Custom shortcodes
  • Custom post types
Business Features
  • Booking systems
  • Payment integrations
  • Student management
  • Membership systems
  • Custom reports

2. Why Create a Custom Plugin?

Benefits of Custom Plugins
  • Add functionality that your website needs.
  • Keep custom features separate from theme design.
  • Continue using the feature when you change themes.
  • Reuse your code on other WordPress websites.
  • Organize your PHP functions into a maintainable package.

3. WordPress Plugin Folder Structure

WordPress plugins are normally stored in the wp-content/plugins/ directory.

Example Folder Structure
wp-content/
└── plugins/
    └── soopro-custom-plugin/
        ├── soopro-custom-plugin.php
        ├── includes/
        │   └── functions.php
        └── assets/
            ├── css/
            └── js/

For this beginner lesson, we will start with one PHP file. Later, you can separate the code into multiple files as your plugin grows.

4. Step 1: Create a Plugin Folder

Open your WordPress installation and navigate to:

wp-content/plugins/

Create a folder named:

Plugin Folder
soopro-custom-plugin

You can create the folder using:

  • Your hosting File Manager.
  • An FTP client.
  • Your local WordPress development environment.

5. Step 2: Create the Main Plugin File

Inside the plugin folder, create a file named soopro-custom-plugin.php.

Basic Plugin Header
<?php
/**
 * Plugin Name: Soopro Custom Plugin
 * Description: A beginner-friendly custom WordPress plugin.
 * Version: 1.0.0
 * Author: Soopro Pathshala
 * Text Domain: soopro-custom-plugin
 */

if (!defined('ABSPATH')) {
    exit;
}

Explanation:

Code Purpose
Plugin Name The plugin name displayed in the WordPress admin area.
Description Explains what the plugin does.
Version Specifies the plugin version.
Author Identifies the plugin author.
ABSPATH Helps prevent direct access to the PHP file outside a normal WordPress loading context.

6. Step 3: Add a Custom Admin Notice

Let's add an informational notice to the WordPress administration area using an action hook.

Admin Notice Code
function soopro_custom_admin_notice() {

    echo '<div class="notice notice-success is-dismissible">';
    echo '<p>Soopro Custom Plugin is working!</p>';
    echo '</div>';

}

add_action(
    'admin_notices',
    'soopro_custom_admin_notice'
);

Add this code below the plugin header in the same PHP file. When the plugin is active, the message appears in the WordPress admin area when the admin_notices hook runs.

7. Step 4: Create a Shortcode

A shortcode allows you to display plugin-generated content inside a WordPress page or post.

For example, we can create a shortcode named [soopro_message].

Shortcode PHP Code
function soopro_custom_message() {

    return '<div class="soopro-message">
        <h3>Welcome to Soopro Pathshala!</h3>
        <p>Learn WordPress development step by step.</p>
    </div>';

}

add_shortcode(
    'soopro_message',
    'soopro_custom_message'
);

How does it work?

  • soopro_custom_message() generates the content.
  • add_shortcode() registers the shortcode.
  • soopro_message is the shortcode name.
  • The function returns the HTML that WordPress inserts into the page.
Use the Shortcode
Open a WordPress page or post and add:
[soopro_message]

Publish or update the page, then open it on your website to see the message.

8. Step 5: Add CSS for the Shortcode

We can style the message using CSS so it looks more professional. For this beginner example, add the following CSS to your active theme's custom CSS area.

Custom CSS
.soopro-message {
    background: #f1f5f9;
    border-left: 5px solid #6a11cb;
    padding: 20px;
    margin: 20px 0;
    border-radius: 8px;
}

.soopro-message h3 {
    color: #6a11cb;
    margin-top: 0;
}

.soopro-message p {
    color: #334155;
    margin-bottom: 0;
}

You can add this CSS through Appearance → Customize → Additional CSS if your active theme provides that option, or through an appropriate stylesheet.

9. Complete Plugin Code

Below is the complete beginner plugin. Replace the contents of soopro-custom-plugin.php with this code.

soopro-custom-plugin.php
<?php
/**
 * Plugin Name: Soopro Custom Plugin
 * Description: A beginner-friendly custom WordPress plugin.
 * Version: 1.0.0
 * Author: Soopro Pathshala
 * Text Domain: soopro-custom-plugin
 */

if (!defined('ABSPATH')) {
    exit;
}

/**
 * Display a notice in the admin area.
 */
function soopro_custom_admin_notice() {

    echo '<div class="notice notice-success is-dismissible">';
    echo '<p>Soopro Custom Plugin is working!</p>';
    echo '</div>';

}

add_action(
    'admin_notices',
    'soopro_custom_admin_notice'
);

/**
 * Generate the custom shortcode content.
 */
function soopro_custom_message() {

    return '<div class="soopro-message">
        <h3>Welcome to Soopro Pathshala!</h3>
        <p>Learn WordPress development step by step.</p>
    </div>';

}

add_shortcode(
    'soopro_message',
    'soopro_custom_message'
);

10. Step 6: Activate the Plugin

Activation Steps
  1. Log in to your WordPress admin dashboard.
  2. Open Plugins → Installed Plugins.
  3. Find Soopro Custom Plugin.
  4. Click Activate.
  5. Check the admin area for the success notice.
  6. Create or edit a page and insert [soopro_message].
  7. Preview the page to test the shortcode.
Always test a new plugin on a local or staging website first. A PHP syntax error can cause a website error. Keep a backup and ensure you can access the files to disable a faulty plugin.

11. Add a Custom CSS File Through the Plugin

For a larger plugin, it is better to keep CSS in a separate file rather than placing it in the theme's custom CSS area.

Create this file:

soopro-custom-plugin/
├── soopro-custom-plugin.php
└── assets/
    └── css/
        └── style.css

Add the following code to your main plugin file:

Enqueue the Plugin Stylesheet
function soopro_custom_plugin_styles() {

    wp_enqueue_style(
        'soopro-custom-plugin-style',
        plugin_dir_url(__FILE__) . 'assets/css/style.css',
        array(),
        '1.0.0'
    );

}

add_action(
    'wp_enqueue_scripts',
    'soopro_custom_plugin_styles'
);

Add the CSS rules from Step 5 to assets/css/style.css. WordPress will enqueue the stylesheet on front-end pages.

For a larger plugin, you can improve performance by loading the stylesheet only on pages where the shortcode is used.

12. Basic Plugin Security

Security Best Practices
  • Check direct access where appropriate using ABSPATH.
  • Sanitize input received from users.
  • Validate submitted data before processing it.
  • Escape output using functions such as esc_html() and esc_attr().
  • Use nonces to help protect forms and actions against CSRF.
  • Check user capabilities before allowing privileged actions.
  • Use prepared database queries when working with user-provided values.
  • Keep the plugin updated and test it after WordPress updates.

13. Common Plugin Development Mistakes

Mistake Solution
Missing plugin header Add a valid plugin header comment.
Wrong plugin directory Place the plugin inside wp-content/plugins/.
Shortcode does not display Confirm the plugin is active and the shortcode name matches.
Duplicate function name Use unique function names to avoid naming conflicts.
PHP syntax error Check parentheses, braces, quotes, and semicolons.
Unsafe output Escape dynamic values appropriately.

14. Practical Assignment

Your Task
  1. Create a plugin folder named soopro-custom-plugin.
  2. Create its main PHP file and add the plugin header.
  3. Add an admin notice using add_action().
  4. Create a shortcode named [soopro_message].
  5. Add CSS to style the shortcode output.
  6. Activate the plugin and test it on a page.
  7. Move the CSS into a separate file and enqueue it.
  8. Explain why plugin functionality should be separate from theme design.

15. Lesson Summary

  • A plugin extends WordPress functionality.
  • Plugins are usually stored in wp-content/plugins/.
  • The plugin header identifies the plugin to WordPress.
  • Action hooks can run custom functions.
  • Shortcodes display plugin-generated content inside posts and pages.
  • Plugin styles can be loaded using wp_enqueue_style().
  • Security, testing, and maintenance are essential for plugin development.

Lesson 43 Quiz: Custom WordPress Plugins

Choose the correct answer for each question.

1. Where are WordPress plugins usually stored?

2. Which function registers a WordPress shortcode?

3. Why should plugin functionality generally be separated from theme design?