Code Snippet to Create a Custom Shortcode for Dynamic Content in WordPress

Marek S Jan 24, 2025 Shortcodes
How can I make a special shortcut on my WordPress site that shows different content?
What is the code to register a custom shortcode in WordPress that allows for dynamic content output based on parameters?
Andy answered Jan 25, 2025

Creating a Custom Dynamic Shortcode in WordPress

Custom Code Solution

This code creates a shortcode that outputs dynamic content based on parameters. You can add this code to your theme's functions.php file or in a custom plugin file.

Basic shortcode registration with parameters:

function custom_dynamic_content_shortcode($atts) {
    // Set default parameters
    $defaults = array(
        'type' => 'default',
        'limit' => 5,
        'category' => ''
    );
    
    // Merge user parameters with defaults
    $atts = shortcode_atts($defaults, $atts, 'dynamic_content');
    
    $output = '';
    
    // Generate content based on type parameter
    switch($atts['type']) {
        case 'posts':
            $posts = get_posts(array(
                'posts_per_page' => $atts['limit'],
                'category_name' => $atts['category']
            ));
            
            foreach($posts as $post) {
                $output .= sprintf(
                    '<div class="dynamic-item"><h3>%s</h3><p>%s</p></div>',
                    $post->post_title,
                    wp_trim_words($post->post_content, 20)
                );
            }
            break;
            
        default:
            $output = '<div class="dynamic-default">Default Content</div>';
    }
    
    return $output;
}
add_shortcode('dynamic_content', 'custom_dynamic_content_shortcode');

Usage Examples

Use the shortcode in posts or pages like this:

[dynamic_content]
[dynamic_content type="posts" limit="3" category="news"]

Enhanced Version with More Features

This extended version includes additional functionality and styling:

function enhanced_dynamic_content_shortcode($atts) {
    // Set default parameters
    $defaults = array(
        'type' => 'default',
        'limit' => 5,
        'category' => '',
        'style' => 'list',
        'orderby' => 'date',
        'order' => 'DESC'
    );
    
    $atts = shortcode_atts($defaults, $atts, 'dynamic_content');
    
    // Add custom styles
    wp_enqueue_style('dynamic-content-style');
    
    $output = '<div class="dynamic-content ' . esc_attr($atts['style']) . '">';
    
    switch($atts['type']) {
        case 'posts':
            $args = array(
                'posts_per_page' => $atts['limit'],
                'category_name' => $atts['category'],
                'orderby' => $atts['orderby'],
                'order' => $atts['order']
            );
            
            $posts = get_posts($args);
            
            foreach($posts as $post) {
                $image = get_the_post_thumbnail_url($post->ID, 'thumbnail');
                $output .= sprintf(
                    '<article class="dynamic-item">
                        %s
                        <h3><a href="%s">%s</a></h3>
                        <p>%s</p>
                    </article>',
                    $image ? '<img src="' . esc_url($image) . '" alt="' . esc_attr($post->post_title) . '">' : '',
                    get_permalink($post->ID),
                    $post->post_title,
                    wp_trim_words($post->post_content, 20)
                );
            }
            break;
            
        default:
            $output .= '<div class="dynamic-default">Default Content</div>';
    }
    
    $output .= '</div>';
    
    return $output;
}
add_shortcode('dynamic_content', 'enhanced_dynamic_content_shortcode');

// Add custom styles
function register_dynamic_content_styles() {
    wp_register_style(
        'dynamic-content-style',
        false
    );
    
    $custom_css = "
        .dynamic-content { margin: 20px 0; }
        .dynamic-content .dynamic-item { margin-bottom: 20px; }
        .dynamic-content.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); gap: 20px; }
        .dynamic-content img { max-width: 100%; height: auto; }
    ";
    
    wp_add_inline_style('dynamic-content-style', $custom_css);
}
add_action('wp_enqueue_scripts', 'register_dynamic_content_styles');

Plugin Alternative

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

  1. Shortcodes Ultimate - Offers various pre-built shortcodes
  2. Generate Shortcode - Create custom shortcodes without coding

Both plugins provide user-friendly interfaces for creating and managing shortcodes.

Remember to backup your WordPress site before adding custom code or installing new plugins.