Search
Close this search box.
Search
Close this search box.

WordPress Shortcodes

A shortcode that simply returns something

add_shortcode('return_something', 'return_something_shortcode');

function return_something_shortcode() {
    return 'something';
}

Using ob_start

function return_something_shortcode() {
    ob_start();
    ?>
    <div>test</div>
    <?php
    $output_string = ob_get_contents(); 
    ob_end_clean(); 
    return $output_string; 
    wp_reset_postdata();
}

Using the shortcode’s content

function use_shortcode_content($atts, $content = null) {
    // eg. echo $atts['id'];
    return $content;
}

Using PHP to execute a shortcode

echo do_shortcode('[return_something]');

Learning Resources

  • Getting Started With WordPress Shortcodes article.
  • How to create your own WordPress shortcodes article.