1

It doesn't seem that output buffering takes into account echo's from within hooked functions.

function buffer_start() { ob_start(); } 
function buffer_end() { ob_end_flush(); }
add_action('init', 'buffer_start');
add_action('admin_footer', 'buffer_end');

add_action("draft_to_publish", "my_hooked_function", 10, 1); // the hook
function my_hooked_function($post) {
    echo("<script>console.log('some stuff I want to output to the developer console, via the html page');</script>");
}

Somehow, this doesn't work. It will ignore the echo, and not update the source on the page. Even though the Wordpress execution cycle is: init, draft_to_publish, admin_footer.

If I input echo's into the buffer_start and buffer_end functions, it works as normal though.

What am I doing wrong? Is there any scope or context or something that I need to reference from within my_hooked_function to make sure the echo there goes to the page's output buffer?

I used this code as a starting point: http://www.dagondesign.com/articles/wordpress-hook-for-entire-page-using-output-buffering/

2 Answers 2

1

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

0

Try to simply use ob in your function like so:

add_action( 'draft_to_publish', 'my_hooked_function', 10, 10 );
function my_hooked_function( $post ) {
   ob_start();
   echo '<script>console.log("some stuff to output to the developer console, via html page");</script>';
   $output = ob_get_contents(); // Put ob content in a variable
   $ob_end_clean();
   echo $output; // Echo the variable
}

And I think it's better than add ob_start/en_flush on the full admin.

2
  • That doesn't work, since it will overwrite the entire html of the page, as well as give the Warning: Cannot modify header information - headers already sent error.
    – Magne
    Jul 6, 2015 at 14:36
  • Right, my bad. Maybe you can try using 'parse_request' action to test some query_var.
    – Dexter0015
    Jul 6, 2015 at 18:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.