DocsStation
WordPress Plugin Development

WordPress Shortcodes

By csestudy247 Updated November 23, 2026 7 min read
7 min read
Learning Objectives

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.

Shortcode Example
[my_shortcode]

WordPress identifies a registered shortcode, executes its callback function, and replaces the shortcode with the returned output.

Common Uses of Shortcodes
  • 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:

WordPress 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

User inserts shortcode into a page
↓
WordPress processes page content
↓
WordPress identifies the registered shortcode
↓
The shortcode callback function executes
↓
The callback generates HTML output
↓
WordPress replaces shortcode with output
↓
Visitor sees the content

3. Shortcode API

WordPress provides the Shortcode API for registering and processing custom shortcodes.

Primary Function: add_shortcode()

PHP Syntax
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

PHP
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

PHP
function cse_hello_shortcode() {

    return '<h2>Hello from my WordPress plugin!</h2>';

}

2Register the Shortcode

PHP
add_shortcode(
    'cse_hello',
    'cse_hello_shortcode'
);

3Complete Code

PHP
<?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:

Shortcode
[cse_hello]
Expected Output

Hello from my WordPress plugin!

5. Understanding the Callback Function

The callback function is the PHP function executed when WordPress encounters the shortcode.

PHP
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.
Important: Shortcode callback functions should generally return the generated content instead of directly echoing it.

6. Shortcode Attributes

Shortcodes can accept attributes to customize their output.

Example

Shortcode
[welcome name="Sanjit"]

PHP Code

PHP
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

PHP
function cse_welcome_shortcode( $atts )

The $atts parameter contains the attributes provided by the user.

2. Define Default Values

PHP
shortcode_atts(
    array(
        'name' => 'User',
    ),
    $atts,
    'welcome'
);

The shortcode_atts() function combines user-provided attributes with default values.

3. Escape Output

PHP
esc_html( $atts['name'] );

This safely escapes the value before displaying it as HTML text.

Examples

Custom Name

[welcome name="Sanjit"]

Welcome, Sanjit!
Default Value

[welcome]

Welcome, User!

7. Multiple Shortcode Attributes

A shortcode can accept multiple attributes to customize its output.

Example Shortcode

Shortcode
[product_card name="Laptop" price="50000" category="Electronics"]

PHP Code

PHP
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

PHP
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

Shortcode
[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
CSE Study247

Welcome back

Sign in to continue to your account.

Forgot password? Create account

Registration Form
CSE Study247

Create your account

Join us and keep your learning progress in one place.

Adding a mobile number is optional. You can provide one later if a payment feature needs it.

Already have an account? Sign in

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

PHP
return '<h2>'
    . esc_html( $atts['title'] )
    . '</h2>';

11. Common Shortcode Mistakes

Using echo Instead of return

Incorrect PHP
function my_shortcode() {
    echo 'Hello';
}
Correct PHP
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

CSE Study247

Welcome back

Sign in to continue to your account.

Forgot password? Create account

.

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.

PHP
add_shortcode( $tag, $callback );

shortcode_atts()

Combines user-provided attributes with default values.

PHP
shortcode_atts(
    $defaults,
    $atts,
    $shortcode
);

do_shortcode()

Processes shortcode text and converts registered shortcodes into their output.

PHP
$content = do_shortcode( '[cse_hello]' );

remove_shortcode()

Removes a registered shortcode.

PHP
remove_shortcode( 'cse_hello' );

remove_all_shortcodes()

Removes all registered shortcodes.

PHP
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:

  1. Create a callback function.
  2. Generate the required output.
  3. Return the output.
  4. Register the shortcode using add_shortcode().
  5. Insert the shortcode into a post or page.
  6. WordPress processes the shortcode and displays the output.

Core Example

PHP
function cse_hello_shortcode() {

    return '<h2>Hello from my plugin!</h2>';

}

add_shortcode(
    'cse_hello',
    'cse_hello_shortcode'
);

Usage:

Shortcode
[cse_hello]
Scroll to Top