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

Cra*_*ray 5 wordpress hook action function

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

use*_*443 8

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

第一次看起来很吓人,但效果很好。只要确保每次使用 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.
Run Code Online (Sandbox Code Playgroud)

  • 你的答案有错别字。请将“ob_get_content()”更改为“ob_get_contents()” (2认同)