0

I want to check if do_action('some_hook') in WordPress returns/echoes any result for a specific post. I've tried using has_action('some_hook') but it only checks if 'some_hook' has any registered handler at global level, it does not check if a specific post has any result for that hook. Can anybody please tell me how to achieve what I want?

  • 1
    What exactly do you mean? actions (in contrast to filters) should not have any result (read: ouput/return/..) – kero Feb 4 '19 at 17:10
  • do_action() does echo the result, provided some some function is hooked into it via add_action(). I'm not sure I understand what you meant? – Faisal Khurshid Feb 4 '19 at 17:13
  • You're right, I was thinking in the wrong direction – kero Feb 4 '19 at 17:17
2

One possible way could be to use PHP's output buffering via ob_start() like so

ob_start();
do_action('my_hook');

$content = ob_get_contents();
ob_end_clean();

if (empty($content)) {
    // no output generated
} else {
    // had some output
}
| improve this answer | |
  • 1
    Thanks, I actually tried this method before but it was not working. it works now! – Faisal Khurshid Feb 4 '19 at 17:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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