$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>hooks - Check if `do_action()` in WordPress returns any result|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin8PV0评论

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
}
Post a comment

comment list (0)

  1. No comments so far