【问题标题】:WordPress: save output of do_action in variableWordPress:将 do_action 的输出保存在变量中
【发布时间】:2019-05-28 05:58:19
【问题描述】:

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

【问题讨论】:

  • 在这里用你的函数解释更多
  • 我有这些来自插件的操作,并希望在短代码中使用这些操作的输出
  • 看,do_action 是一个钩子,你在其中附加一个函数(回调)。如果回调真的返回了一些东西,那么你可以调用它。例如do_action('init','your_function');然后你可以简单地调用 $x = your_function()。但是如果函数没有返回任何东西怎么办?可能你需要修改它们。但如果是其他插件,则不应该对其进行编辑,而是可以使用 start_ob。希望这会有所帮助。
  • 感谢您的评论。不幸的是我没有得到它......;)我必须找到另一种方法。

标签: wordpress function hook action


【解决方案1】:

使用 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.

【讨论】:

  • 您的回答中有错字。请将“ob_get_content()”改为“ob_get_contents()”
猜你喜欢
  • 1970-01-01
  • 2011-11-10
  • 1970-01-01
  • 2021-10-10
  • 2020-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-12
相关资源
最近更新 更多