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.
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:
WordPress plugins are normally stored in the
wp-content/plugins/ directory.
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.
Open your WordPress installation and navigate to:
wp-content/plugins/
Create a folder named:
soopro-custom-plugin
You can create the folder using:
Inside the plugin folder, create a file named
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;
}
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. |
Let's add an informational notice to the WordPress administration area using an action hook.
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.
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].
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.[soopro_message]
Publish or update the page, then open it on your website to see the message.
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.
.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.
Below is the complete beginner plugin. Replace the contents of
soopro-custom-plugin.php with this code.
<?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'
);
[soopro_message].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:
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.
ABSPATH.esc_html() and esc_attr().| 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. |
soopro-custom-plugin.add_action().[soopro_message].wp-content/plugins/.wp_enqueue_style().Choose the correct answer for each question.