hooks - Check if `do_action()` in WordPress returns any result

admin2025-06-03  3

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?

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?

Share Improve this question edited Feb 4, 2019 at 17:15 Faisal Khurshid asked Feb 4, 2019 at 17:04 Faisal KhurshidFaisal Khurshid 4451 gold badge5 silver badges19 bronze badges 3
  • 1 What exactly do you mean? actions (in contrast to filters) should not have any result (read: ouput/return/..) – kero Commented Feb 4, 2019 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 Commented Feb 4, 2019 at 17:13
  • You're right, I was thinking in the wrong direction – kero Commented Feb 4, 2019 at 17:17
Add a comment  | 

1 Answer 1

Reset to default 3

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
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748943702a315034.html

最新回复(0)