Back to Blog

WordPress Shortcodes: Display Dynamic Content Without Coding

Shortcodes let you insert dynamic content anywhere in WordPress. Display the current user's name, today's date, or content that only logged-in users see, all without touching PHP. Here's how to use them effectively.

What Are Shortcodes?

WordPress shortcodes are placeholders that get replaced with dynamic content when the page renders. They look like this:

[shortcode_name attribute="value"]

When WordPress encounters a shortcode, it runs the associated PHP function and outputs the result. The Shortcode API has been part of WordPress since version 2.5.

Under the hood, shortcodes use the add_shortcode() function:

add_shortcode('greeting', function($atts) {
    $user = wp_get_current_user();
    if ($user->ID) {
        return 'Hello, ' . esc_html($user->display_name);
    }
    return 'Hello, visitor';
});

You could write custom shortcodes yourself, but that requires PHP knowledge and ongoing maintenance. For common use cases like user data and conditional display, a shortcode plugin saves significant time.

User Information Shortcodes

These shortcodes pull data from the current logged-in user:

Username

[essesh_username]

Outputs the user's display name. For "John Smith", this returns "John Smith".

First Name

[essesh_firstname]

Returns just the first name from the user profile. If not set, falls back to username.

Email

[essesh_email]

The user's registered email address. Useful for confirmation messages or account pages.

User Avatar

[essesh_avatar size="96"]

Displays the user's Gravatar or custom avatar at the specified pixel size. Uses get_avatar() internally.

User Role

[essesh_role]

Returns the user's primary role: "Administrator", "Editor", "Subscriber", etc.

Handling Non-Logged-In Users

User shortcodes return empty or fallback values for visitors who aren't logged in. Wrap them in conditional shortcodes (see below) to handle this gracefully.

Date and Time Shortcodes

Display current date/time information dynamically:

Current Date

[essesh_date]                    // March 24, 2024
[essesh_date format="Y-m-d"]     // 2024-03-24
[essesh_date format="F j"]       // March 24
[essesh_date format="l"]         // Sunday

The format parameter uses PHP date format strings. Common formats:

Current Time

[essesh_time]                    // 2:30 PM
[essesh_time format="H:i"]       // 14:30

Current Year

[essesh_year]

Perfect for copyright notices that auto-update: "© [essesh_year] Your Company"

Conditional Content Shortcodes

These shortcodes show or hide content based on conditions:

Logged-In Users Only

[essesh_if_logged_in]
Welcome back! Here's your member content.
[/essesh_if_logged_in]

Content between the tags only appears for authenticated users.

Logged-Out Users Only

[essesh_if_logged_out]
Please log in to see exclusive content.
[/essesh_if_logged_out]

Role-Specific Content

[essesh_if_user_role role="administrator"]
Admin-only controls appear here.
[/essesh_if_user_role]

[essesh_if_user_role role="editor,administrator"]
Content for editors and admins.
[/essesh_if_user_role]

Mobile-Only Content

[essesh_if_mobile]
Tap to call: 555-1234
[/essesh_if_mobile]

[essesh_if_desktop]
Call us at 555-1234 or use the contact form.
[/essesh_if_desktop]

Uses wp_is_mobile() for device detection.

Time-Based Content

[essesh_show_after_date date="2024-04-01"]
Spring sale is now live!
[/essesh_show_after_date]

[essesh_show_until_date date="2024-03-31"]
Pre-order before April 1st for early access.
[/essesh_show_until_date]

Real-World Examples

Personalized Dashboard Greeting

<h1>Welcome back, [essesh_firstname]!</h1>
<p>Today is [essesh_date format="l, F j"]. Here's what's new:</p>

Output: "Welcome back, John! Today is Sunday, March 24. Here's what's new:"

Member vs. Visitor Content

[essesh_if_logged_in]
<a href="/account">My Account</a> | <a href="/logout">Logout</a>
[/essesh_if_logged_in]

[essesh_if_logged_out]
<a href="/login">Login</a> | <a href="/register">Sign Up</a>
[/essesh_if_logged_out]

Auto-Updating Copyright

© 2020-[essesh_year] Your Company. All rights reserved.

Never manually update your footer year again.

Limited-Time Offer

[essesh_show_until_date date="2024-12-31"]
<div class="banner">
  🎁 Holiday Sale: 30% off with code HOLIDAY30
</div>
[/essesh_show_until_date]

Banner automatically disappears after December 31st.

Role-Based Pricing

[essesh_if_user_role role="wholesale_customer"]
<p class="price">Wholesale: $45.00</p>
[/essesh_if_user_role]

[essesh_if_user_role role="subscriber,customer"]
<p class="price">Member: $59.00</p>
[/essesh_if_user_role]

[essesh_if_logged_out]
<p class="price">Retail: $79.00</p>
[/essesh_if_logged_out]

Using with Page Builders

Gutenberg

Use the Shortcode block: Add Block → Widgets → Shortcode. Paste your shortcode directly.

Elementor

Use the Shortcode widget. Elementor also has dynamic tags, but shortcodes work anywhere and aren't Elementor-dependent.

WPBakery / Visual Composer

Text Block elements support shortcodes directly. Or use the Raw HTML element for complex combinations.

Beaver Builder

Add an HTML module and insert your shortcodes. Preview to verify they render correctly.

Shortcode Nesting

WordPress supports nesting shortcodes, but not all combinations work. If you need to nest, test thoroughly. Example: [outer][inner][/inner][/outer] works if both shortcodes are designed for it.

Performance Considerations

Shortcodes execute on every page load. Keep these points in mind:

Essential Shortcodes Pro

40+ ready-to-use shortcodes for user data, dates, conditional content, and more. Built-in analytics dashboard shows which shortcodes are used where.

Get Essential Shortcodes Pro - $29

Summary

Shortcodes transform static content into dynamic, personalized experiences. Display user names, handle conditional logic, and automate date-based content without writing PHP.

For membership sites, course platforms, and personalized marketing, shortcodes are essential tools. They're simple to use, work everywhere, and keep your content dynamic.

H

Haohunter

WordPress developer building lightweight plugins that solve real problems. No bloat, no subscriptions, just tools that work.