Create a Custom Shortcode in WordPress

Oskar T. Dec 22, 2024 Shortcodes
How can I make a special code that lets me add some content easily on different pages?
What is the process to create a custom shortcode in WordPress that can output specific content or functionality?
Andy answered Dec 22, 2024

Creating a Custom WordPress Shortcode

Basic Custom Shortcode Solution

Here's how to create a simple shortcode that outputs custom content. Add this code to your theme's functions.php file or in a site-specific plugin:

Basic shortcode that outputs static content:

function my_custom_shortcode() {
    $output = '<div class="custom-content">';
    $output .= 'Your custom content here';
    $output .= '</div>';
    
    return $output;
}
add_shortcode('my_content', 'my_custom_shortcode');

Usage: [my_content]

Advanced Shortcode with Parameters

Shortcode that accepts parameters and returns dynamic content:

function my_advanced_shortcode($atts) {
    // Set default values
    $attributes = shortcode_atts(array(
        'title' => 'Default Title',
        'color' => 'blue',
        'size'  => 'medium'
    ), $atts);
    
    $output = '<div class="custom-content" style="color: ' . esc_attr($attributes['color']) . ';">';
    $output .= '<h2>' . esc_html($attributes['title']) . '</h2>';
    $output .= '<div class="size-' . esc_attr($attributes['size']) . '">';
    $output .= 'Your dynamic content here';
    $output .= '</div></div>';
    
    return $output;
}
add_shortcode('my_dynamic', 'my_advanced_shortcode');

Usage: [my_dynamic title="My Title" color="red" size="large"]

Shortcode with Content Between Tags

Shortcode that processes content between opening and closing tags:

function my_content_shortcode($atts, $content = null) {
    // Process the content
    $processed_content = do_shortcode($content);
    
    $output = '<div class="wrapped-content">';
    $output .= $processed_content;
    $output .= '</div>';
    
    return $output;
}
add_shortcode('wrap', 'my_content_shortcode');

Usage: [wrap]Your content here[/wrap]

Where to Place the Code

  1. In your theme's functions.php file
  2. In a site-specific plugin (recommended)
  3. Using a code snippets plugin

Plugin Solutions

If you prefer using a plugin, here are reliable options:

  1. Shortcodes Ultimate - Includes 50+ ready-to-use shortcodes
  2. Shortcoder - Create custom shortcodes easily through admin interface

Best Practices

  1. Always sanitize and escape output
  2. Use meaningful shortcode names
  3. Include default values for parameters
  4. Document usage for team members
  5. Test shortcodes thoroughly
  6. Use the shortcode_atts() function for parameters
  7. Return the output instead of echoing it

Tips

  • Use do_shortcode() to process nested shortcodes
  • Avoid using similar names to existing WordPress shortcodes
  • Keep shortcode names lowercase and use hyphens or underscores
  • Test shortcodes in both visual and text editors

This solution provides flexibility while maintaining WordPress coding standards and security practices.