Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WordPress: save output of do_action in variable

I want to save the output of do_action in a variable to use it later. How could I save these output?

like image 687
Cray Avatar asked Aug 02 '16 14:08

Cray


People also ask

What does do_ action do in WordPress?

do_action( string $hook_name, mixed $arg ) Calls the callback functions that have been added to an action hook.

What is action hook?

Action Hooks are a very useful tool in WordPress and they are used to perform functions (actions) in specific places of a theme or plugin. Many themes and plugins, such as Total, use action hooks as an easy way for users to modify the output of the project or to add their own custom code.


1 Answers

use ob_start() and ob_get_contents() and ob_end_clean() see example #1 on the following page in the PHP manual http://php.net/manual/en/function.ob-get-contents.php

It looks scary the first time, but it works well. Just make sure to always use ob_end_clean() for every time you use ob_start()

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.
like image 100
user158443 Avatar answered Sep 22 '22 13:09

user158443