WordPress Shortcodes
After completing this section, you will understand how WordPress shortcodes work and how to create, register, customize, and use shortcodes in plugin development.
- Understand the purpose of shortcodes.
- Learn the WordPress Shortcode API.
- Create and register custom shortcodes.
- Use shortcode attributes.
- Generate dynamic HTML output.
- Display database content using shortcodes.
- Follow security and development best practices.
1. What Is a WordPress Shortcode?
A shortcode is a small text-based tag that allows users to execute plugin or theme functionality and display dynamic content without writing PHP code.
Shortcodes are written inside square brackets.
[my_shortcode]
WordPress identifies a registered shortcode, executes its callback function, and replaces the shortcode with the returned output.
- Contact forms
- Login and registration forms
- Course lists
- Products
- Test series
- Quizzes
- User dashboards
- Dynamic content
Real-World Example
Suppose you are developing an online examination plugin. You want administrators to display available tests on any WordPress page.
Instead of requiring administrators to write PHP code, your plugin can provide a shortcode:
[test_series]
The administrator simply inserts this shortcode into a page. Your plugin processes the shortcode and displays the test series automatically.
2. How Shortcodes Work
3. Shortcode API
WordPress provides the Shortcode API for registering and processing custom shortcodes.
Primary Function: add_shortcode()
add_shortcode( $tag, $callback );
| Parameter | Description |
|---|---|
$tag |
The shortcode name used inside square brackets. |
$callback |
The PHP function executed when the shortcode is processed. |
Example
add_shortcode(
'cse_hello',
'cse_hello_shortcode'
);
Here, cse_hello is the shortcode tag, while cse_hello_shortcode is the callback function.
4. Creating Your First Shortcode
1Create the Callback Function
function cse_hello_shortcode() {
return '<h2>Hello from my WordPress plugin!</h2>';
}
2Register the Shortcode
add_shortcode(
'cse_hello',
'cse_hello_shortcode'
);
3Complete Code
<?php
function cse_hello_shortcode() {
return '<h2>Hello from my WordPress plugin!</h2>';
}
add_shortcode(
'cse_hello',
'cse_hello_shortcode'
);
4Use the Shortcode
Add the following shortcode inside a WordPress post or page:
[cse_hello]
Hello from my WordPress plugin!
5. Understanding the Callback Function
The callback function is the PHP function executed when WordPress encounters the shortcode.
function cse_hello_shortcode() {
return '<h2>Hello from my WordPress plugin!</h2>';
}
| Code | Explanation |
|---|---|
function |
Defines a PHP function. |
cse_hello_shortcode |
The callback function name. |
return |
Returns the generated content to WordPress. |
<h2> |
HTML heading output. |
6. Shortcode Attributes
Shortcodes can accept attributes to customize their output.
Example
[welcome name="Sanjit"]
PHP Code
function cse_welcome_shortcode( $atts ) {
$atts = shortcode_atts(
array(
'name' => 'User',
),
$atts,
'welcome'
);
return '<h2>Welcome, '
. esc_html( $atts['name'] )
. '!</h2>';
}
add_shortcode(
'welcome',
'cse_welcome_shortcode'
);
Understanding the Code
1. Receive Attributes
function cse_welcome_shortcode( $atts )
The $atts parameter contains the attributes provided by the user.
2. Define Default Values
shortcode_atts(
array(
'name' => 'User',
),
$atts,
'welcome'
);
The shortcode_atts() function combines user-provided attributes with default values.
3. Escape Output
esc_html( $atts['name'] );
This safely escapes the value before displaying it as HTML text.
Examples
[welcome name="Sanjit"]
[welcome]
7. Multiple Shortcode Attributes
A shortcode can accept multiple attributes to customize its output.
Example Shortcode
[product_card name="Laptop" price="50000" category="Electronics"]
PHP Code
function cse_product_shortcode( $atts ) {
$atts = shortcode_atts(
array(
'name' => 'Product',
'price' => '0',
'category' => 'General',
),
$atts,
'product_card'
);
$name = esc_html( $atts['name'] );
$price = esc_html( $atts['price'] );
$category = esc_html( $atts['category'] );
return '
<div class="product-card">
<h3>' . $name . '</h3>
<p>Price: ₹' . $price . '</p>
<p>Category: ' . $category . '</p>
</div>
';
}
add_shortcode(
'product_card',
'cse_product_shortcode'
);
Laptop
Price: ₹50000
Category: Electronics
8. Shortcodes with Database Content
Shortcodes become especially useful when displaying content stored in the WordPress database.
For example, suppose a plugin creates a Custom Post Type named courses. A shortcode can retrieve and display published courses.
Example Code
function cse_courses_shortcode() {
$query = new WP_Query(
array(
'post_type' => 'courses',
'posts_per_page' => 5,
'post_status' => 'publish',
)
);
if ( ! $query->have_posts() ) {
return '<p>No courses found.</p>';
}
$output = '<div class="course-list">';
while ( $query->have_posts() ) {
$query->the_post();
$output .= '<div class="course-item">';
$output .= '<h3>'
. esc_html( get_the_title() )
. '</h3>';
$output .= '</div>';
}
$output .= '</div>';
wp_reset_postdata();
return $output;
}
add_shortcode(
'course_list',
'cse_courses_shortcode'
);
Usage
[course_list]
This shortcode displays the latest five published courses.
Important Functions
| Function | Purpose |
|---|---|
WP_Query |
Retrieves posts from the WordPress database. |
have_posts() |
Checks whether posts are available. |
the_post() |
Sets up the current post. |
get_the_title() |
Retrieves the post title. |
wp_reset_postdata() |
Restores the original post data after a custom query. |
9. Real-World Plugin Examples
| Plugin Feature | Example Shortcode |
|---|---|
| Test Series | [exam_list] |
| Specific Exam | [exam id="15"] |
| Course List | [course_list] |
| Login Form | |
| Registration Form | |
| User Dashboard | [user_exam_dashboard] |
| Exam Results | [exam_results] |
10. Security Best Practices
Escape Output
Always escape dynamic values before displaying them in HTML.
| Function | Purpose |
|---|---|
esc_html() |
Escapes text displayed inside HTML. |
esc_attr() |
Escapes values used in HTML attributes. |
esc_url() |
Escapes URLs. |
wp_kses_post() |
Allows safe HTML based on WordPress post-content rules. |
Example
return '<h2>'
. esc_html( $atts['title'] )
. '</h2>';
11. Common Shortcode Mistakes
Using echo Instead of return
function my_shortcode() {
echo 'Hello';
}
function my_shortcode() {
return 'Hello';
}
Forgetting to Register the Shortcode
Defining a callback function is not enough. You must register it using add_shortcode().
Using Generic Shortcode Names
Names such as [login] or [list] may conflict with other plugins.
Use unique names such as .
Not Escaping User Input
Never insert untrusted attribute values directly into HTML.
Forgetting to Reset Custom Queries
When using WP_Query, call wp_reset_postdata() after the loop.
12. Shortcodes vs Gutenberg Blocks
| Shortcodes | Gutenberg Blocks |
|---|---|
| Text-based syntax | Visual editor interface |
| Easy to insert manually | More convenient for non-technical users |
| Simple implementation | Can require more advanced development |
Example: [course_list] |
Course List Block |
Shortcodes remain useful for lightweight functionality, compatibility, and plugin features that need to be inserted easily into content.
13. Technical Reference
add_shortcode()
Registers a shortcode.
add_shortcode( $tag, $callback );
shortcode_atts()
Combines user-provided attributes with default values.
shortcode_atts(
$defaults,
$atts,
$shortcode
);
do_shortcode()
Processes shortcode text and converts registered shortcodes into their output.
$content = do_shortcode( '[cse_hello]' );
remove_shortcode()
Removes a registered shortcode.
remove_shortcode( 'cse_hello' );
remove_all_shortcodes()
Removes all registered shortcodes.
remove_all_shortcodes();
Key Takeaway
A shortcode provides a simple interface that allows WordPress users to execute plugin functionality and display dynamic content without writing PHP code.
Workflow:
- Create a callback function.
- Generate the required output.
- Return the output.
- Register the shortcode using
add_shortcode(). - Insert the shortcode into a post or page.
- WordPress processes the shortcode and displays the output.
Core Example
function cse_hello_shortcode() {
return '<h2>Hello from my plugin!</h2>';
}
add_shortcode(
'cse_hello',
'cse_hello_shortcode'
);
Usage:
[cse_hello]