WordPress:将 do_action 的输出保存在变量中

WordPress: save output of do_action in variable

我想将 do_action 的输出保存在一个变量中以便以后使用。 我怎样才能保存这些输出?

使用 ob_start() 和 ob_get_contents() 以及 ob_end_clean() 请参阅 PHP 手册下一页的示例 #1 http://php.net/manual/en/function.ob-get-contents.php

第一次看起来很吓人,但效果很好。只要确保每次使用 ob_start()

时始终使用 ob_end_clean()
ob_start(); // start capturing output.
do_action('any_action_you_want');
$save_output_here = ob_get_contents(); // the actions output will now be stored in the variable as a string!
ob_end_clean(); // never forget this or you will keep capturing output.