Learn how to create an admin menu in WordPress and add a menu item to the dashboard menu using simple code. Step-by-step guide with examples.
How to Create a Shortcode with a WordPress Plugin

Table of Contents
- What is a WordPress Shortcode?
- Why Use Shortcodes in WordPress?
- How to Create a Shortcode with a WordPress Plugin
- Complete Shortcode Plugin Code
- Bonus: Add Shortcode Attributes
- Frequently Asked Questions
What is a WordPress Shortcode?
A shortcode is a small piece of code in square brackets [ ]
that lets you add dynamic content to posts, pages, and widgets without writing complex code.
Why Use Shortcodes in WordPress?
Shortcodes allow you to:
- Reuse code easily
- Add custom content anywhere
- Improve flexibility in your WordPress theme or plugin
How to Create a Shortcode with a WordPress Plugin
Follow these steps to create a shortcode using a custom plugin.
Step 1: Create a Custom Plugin
- Go to your WordPress site’s root directory.
- Navigate to
/wp-content/plugins/
. - Create a new folder, e.g.,
my-custom-shortcode
. - Inside the folder, create a PHP file, e.g.,
my-custom-shortcode.php
. - Add this code at the top of the file:
- Please visit this blog for more information.
<?php
/**
* Plugin Name: My Custom Shortcode
* Description: A simple WordPress shortcode plugin.
* Version: 1.0
* Author: Your Name
*/
if (!defined('ABSPATH')) exit; // Exit if accessed directly

Step 2: Register the Shortcode
Add this function to define a simple shortcode:
function my_shortcode_function() {
return '<p>This is my custom shortcode content.</p>';
}
add_shortcode('myshortcode', 'my_shortcode_function');
Step 3: Implement the Shortcode Function
You can customize the shortcode output. Example with attributes:
function my_custom_shortcode($atts) {
$atts = shortcode_atts(
array(
'text' => 'Default Text',
),
$atts,
'myshortcode'
);
return '<p>' . esc_html($atts['text']) . '</p>';
}
add_shortcode('myshortcode', 'my_custom_shortcode');
Step 4: Test the Shortcode
- Activate your plugin in WordPress.
- Add
[myshortcode]
to a post or page. - Refresh and check if the shortcode works.
Bonus: Add Shortcode Attributes
Modify the shortcode to accept parameters:
[myshortcode text="Hello World"]
This will display: Hello World
instead of Default Text
.
Complete Shortcode Plugin Code
Here’s the full code for your custom shortcode plugin:
<?php
/**
* Plugin Name: My Custom Shortcode
* Description: A simple WordPress shortcode plugin.
* Version: 1.0
* Author: Your Name
*/
if (!defined('ABSPATH')) exit; // Exit if accessed directly
function my_custom_shortcode($atts) {
$atts = shortcode_atts(
array(
'text' => 'Default Text',
),
$atts,
'myshortcode'
);
return '<p>' . esc_html($atts['text']) . '</p>';
}
add_shortcode('myshortcode', 'my_custom_shortcode');
Save this as my-custom-shortcode.php
, upload it to /wp-content/plugins/
, and activate it in WordPress.
FAQ
How do I disable a shortcode?
remove_shortcode(‘myshortcode’);
This Post Has 0 Comments