Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Hello i have this shortcode that is generating a error.

Can someone help?

add_shortcode('do-action', 'do_action_shortcode');

function do_action_shortcode($atts, $content = '') {
  ob_start();
  do_action($content);
  $out = ob_get_contents();
  ob_end_clean();
  return $out;
}
share|improve this question
2  
What kind of error? Do you have the function do_action defined? –  birgire May 15 '13 at 14:28

1 Answer 1

do_action is a WordPress function for running action hooks. I assume that is what you are trying to do. Given that, The first parameter, the only one in your case, needs to be an action hook name. If you pass it a valid hook name and have a function attached to that hook, this code works.

In other words, if you set it up like this...

function do_action_shortcode($atts, $content = '') {
  ob_start();
  do_action($content);
  $out = ob_get_contents();
  ob_end_clean();
  return $out;
}
add_shortcode('do-action', 'do_action_shortcode');

function generic_cb() {
  echo 'generic content';
}
add_action('testhook','generic_cb');

... so that you have an action hooked to testhook, and use a shortcode like ...

[do-action]testhook[/do-action]

... in you post body. It does work.

You should not be using hyphens in the shortcode name though.

I don't know exactly which part of that you have wrong, but hopefully that working model (tested) will help you out.

The only way, I have found to actually generate an error is if generic_cb (in the example) is not defined. If you send a nonexistent hook name...

[do-action]notahook[/do-action]

... it just does nothing.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.