Cray
Cray

Reputation: 5483

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?

Upvotes: 8

Views: 5241

Answers (1)

user158443
user158443

Reputation: 262

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.

Upvotes: 12

Related Questions