Best explanation for shortcodes in wordpress

Estimated Reading Time: 5 min

In WordPress, shortcodes are small code snippets enclosed in square brackets ([ ]) that allow you to execute predefined functionality or embed dynamic content within posts, pages, or widgets without writing complex code. Here’s a quick overview:


Common Uses of Shortcodes

  1. Embedding Media: [g allery], [v ideo], [a udio]
  2. Forms: Embedding forms using plugins like Contact Form 7 or WPForms (e.g., [contact-form-7 id="123" title="Contact form"])
  3. Buttons: Adding styled buttons.
  4. Sliders: Inserting image or content sliders.
  5. Columns: Creating multi-column layouts.
  6. Custom Features: Embedding custom functionality via third-party plugins or custom code.

Examples of Built-in WordPress Shortcodes

  1. Gallery: [g allery ids="1,2,3"]
  2. Audio: [a udio src="audio.mp3"]
  3. Video: [v ideo src="video.mp4"]
  4. Embed Content: [e mbed]URL[/embed]

How to Add Shortcodes

  • Simply insert the shortcode in the post/page editor where you want the functionality.
  • Example: [my_shortcode]

Creating Custom Shortcodes

You can define your own shortcode using the add_shortcode function in your theme’s functions.php file:



function my_custom_shortcode() {
    return "Hello, this is my custom shortcode!";
}
add_shortcode('custom_hello', 'my_custom_shortcode');



  
  • Use [custom_hello] to display “Hello, this is my custom shortcode!” in your content.

Shortcode Best Practices

  • Avoid nesting too many shortcodes for better readability.
  • Use plugins like Shortcoder or Custom Shortcodes to manage and organize custom shortcodes easily.
  • Test shortcodes thoroughly to ensure compatibility with themes and plugins.

Let me know if you’d like help creating a specific shortcode!

Examples of custom WordPress shortcode functions, which you can add to your theme’s functions.php file or a custom plugin to create useful features:


1. Hello World Shortcode

Displays a simple “Hello, World!” message.



function hello_world_shortcode() {
    return "Hello, World!";
}
add_shortcode('hello_world', 'hello_world_shortcode');




  

Usage: [hello_world]


2. Current Year

Displays the current year dynamically.



function current_year_shortcode() {
    return date('Y');
}
add_shortcode('current_year', 'current_year_shortcode');




  

Usage: [current_year]


3. Button Shortcode

Creates a styled button with a customizable URL.

function button_shortcode($atts) {
    $atts = shortcode_atts([
        'url' => '#',
        'text' => 'Click Here',
    ], $atts);
    return "<a href='{$atts['url']}' class='button'>{$atts['text']}</a>";
}
add_shortcode('button', 'button_shortcode');

Usage: [button url="https://example.com" text="Learn More"]


4. YouTube Embed

Embeds a responsive YouTube video.

function youtube_embed_shortcode($atts) {
    $atts = shortcode_atts(['id' => ''], $atts);
    return "<iframe width='560' height='315' src='https://www.youtube.com/embed/{$atts['id']}' frameborder='0' allowfullscreen></iframe>";
}
add_shortcode('youtube', 'youtube_embed_shortcode');

Usage: [youtube id="dQw4w9WgXcQ"]


5. User Greeting

Greets the logged-in user or displays a default message for guests.

function user_greeting_shortcode() {
    if (is_user_logged_in()) {
        $user = wp_get_current_user();
        return "Hello, " . esc_html($user->display_name) . "!";
    } else {
        return "Hello, Guest!";
    }
}
add_shortcode('user_greeting', 'user_greeting_shortcode');

Usage: [user_greeting]


6. Current Date

Displays the current date in a specific format.

function current_date_shortcode($atts) {
    $atts = shortcode_atts(['format' => 'F j, Y'], $atts);
    return date($atts['format']);
}
add_shortcode('current_date', 'current_date_shortcode');

Usage: [current_date format="l, F j, Y"]


7. Recent Posts

Displays a list of recent posts with links.

function recent_posts_shortcode($atts) {
    $atts = shortcode_atts(['count' => 5], $atts);
    $posts = wp_get_recent_posts(['numberposts' => $atts['count'], 'post_status' => 'publish']);
    $output = '<ul>';
    foreach ($posts as $post) {
        $output .= "<li><a href='" . get_permalink($post['ID']) . "'>" . esc_html($post['post_title']) . "</a></li>";
    }
    $output .= '</ul>';
    return $output;
}
add_shortcode('recent_posts', 'recent_posts_shortcode');

Usage: [recent_posts count="3"]


8. Custom Google Map

Embeds a Google Map with a specified address.

function google_map_shortcode($atts) {
    $atts = shortcode_atts(['address' => ''], $atts);
    $encoded_address = urlencode($atts['address']);
    return "<iframe width='600' height='450' style='border:0' loading='lazy' allowfullscreen src='https://www.google.com/maps/embed/v1/place?q={$encoded_address}&key=YOUR_API_KEY'></iframe>";
}
add_shortcode('google_map', 'google_map_shortcode');

Usage: [google_map address="1600 Amphitheatre Parkway, Mountain View, CA"]


9. FAQ Toggle

Creates a collapsible FAQ section.

function faq_toggle_shortcode($atts, $content = null) {
    $atts = shortcode_atts(['title' => 'FAQ'], $atts);
    return "<div class='faq'><h3>{$atts['title']}</h3><div class='faq-content' style='display:none;'>{$content}</div></div>";
}
add_shortcode('faq', 'faq_toggle_shortcode');

Usage: [faq title="What is WordPress?"]WordPress is a content management system.[/faq]


10. Custom Image

Displays an image with a caption.

function custom_image_shortcode($atts) {
    $atts = shortcode_atts([
        'src' => '',
        'alt' => '',
        'caption' => '',
    ], $atts);
    return "<figure><img src='{$atts['src']}' alt='{$atts['alt']}'><figcaption>{$atts['caption']}</figcaption></figure>";
}
add_shortcode('custom_image', 'custom_image_shortcode');

Usage: [custom_image src="image.jpg" alt="An example image" caption="This is an example image."]


These examples demonstrate how shortcodes can simplify complex tasks and add dynamic content to your WordPress site. Let me know if you’d like to explore a specific shortcode in more detail!

Photo of author

Flora

Best explanation for shortcodes in wordpress

Published

Update

I am Flora, the publisher and founder of *Be-Smart*, a platform dedicated to sharing insights and inspiration for living a fulfilling life. With a strong background in the web, my goal is to empower people to genuinely recognize and celebrate admirable actions big or small in themselves and others.