How to use output buffering in WordPress hooked functions?

TL;DR: After the draft_to_publish hook is executed, the page is redirected, so you won’t see the echoed output.

Ref: https://wordpress.stackexchange.com/a/94011/71131

I think the problem is that the draft_to_publish action saves the post, which will call a redirect request to the browser, which loads the edit page from scratch again. Then whatever whatever scripts previously echoed at the bottom of page wouldn’t be included, as those were executed on the last page just instants before the redirect took place.

The solution should be to use a proper debug tool to output the echo’s to the javascript console. See: https://github.com/nekojira/wp-php-console