Is it possible? "do_action" within shortcode


#1

Does anyone know of a way to include a do_action within a shortcode? I’m guessing it may not be possible since shortcodes need to return data and do_action prints out data… but, it would be nice if there was a way!

…or, if anyone know of a crazy alternative to accomplishing the same thing, that would be cool too.

Thanks!


#2

It’s really easy actually, you just need to place the contents of the short code in an output buffer:

function your_shortcode($atts, $content = null) {
      ob_start();
      do_action('your_hook_name');
      return ob_get_clean();
}
add_shortcode('shortcode_name', 'your_shortcode');

#3

Sweet! I’ll give that a try. Thanks again for all the help. You’ve been a great resource for me. You’ll be getting a new subscriber to your blog soon!


#4

Glad to help!