Backend Development 7 min read

How to Create Custom WordPress Shortcodes in Three Simple Steps

This guide walks you through creating a custom WordPress shortcode by installing the Code Snippets plugin, writing a PHP function to display the current date, registering the shortcode, and inserting it into a page, providing clear step‑by‑step instructions and example code.

php中文网 Courses
php中文网 Courses
php中文网 Courses
How to Create Custom WordPress Shortcodes in Three Simple Steps

Have you ever wanted to add unique functionality to your WordPress site but couldn’t find a suitable plugin? This tutorial shows you how to create and run a personal WordPress shortcode in three simple steps, enabling easy customization of site features.

What Is a WordPress Shortcode

A WordPress shortcode is a lightweight markup tool that lets users quickly insert specific functionality or content into posts, pages, or widgets using a simple tag surrounded by square brackets, e.g., [shortcode] . When the page is rendered, WordPress replaces the placeholder with the corresponding HTML, CSS, JavaScript, or PHP output, allowing dynamic content without writing complex code.

How to Create Your Own Shortcode

Building a custom shortcode is straightforward and consists of three steps:

Install a plugin such as “Code Snippets” (or a similar one) that provides an interface for creating and managing custom shortcodes.

Create a PHP code snippet that contains the functionality you want the shortcode to execute. Ensure the snippet follows proper PHP syntax.

Add the shortcode to the desired page or post by typing the shortcode tag (e.g., [your_shortcode] ) in the editor. When the page loads, WordPress runs the associated PHP code and displays the result.

Note: Creating custom shortcodes requires some PHP and WordPress development experience. Beginners should familiarize themselves with these technologies first.

1. Install the “Code Snippets” Plugin

The plugin lets you embed custom PHP or HTML code anywhere on your site. To install, go to the WordPress dashboard, click “Plugins,” choose “Add New,” search for “Code Snippets,” and install and activate it.

Search for “code” in the plugin repository, click “Install Now,” and activate the plugin after installation.

2. Create the PHP Code for the Shortcode

For illustration, we’ll build a shortcode that outputs the current date, naming it “current_date.” After installing the plugin, go to the dashboard, hover over “Snippets,” and click “Add New.”

Insert the following PHP code to generate the date string:

function display_current_date() {
    $current_date = date('F j, Y'); // Format: Month name day, year
    // Return a nicely formatted HTML span
    return '
' . $current_date . '
';
}

Next, register the shortcode so that WordPress knows which function to run when the shortcode is encountered:

add_shortcode('current_date', 'display_current_date');

This tells WordPress to execute display_current_date whenever it finds the [current_date] tag.

The final snippet looks like this:

Scroll down and click “Save Changes and Activate.”

3. Use the Shortcode in Your Content

Finally, insert the shortcode tag where you want the date to appear, for example:

[current_date]

Publish the page and view it; the current date should be displayed.

Conclusion

This simple guide demonstrates how to build a custom WordPress shortcode, laying the groundwork for more complex shortcodes that can handle custom post types and display them elegantly to meet evolving site requirements.

PluginPHPCustomizationWordPressshortcode
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.