2

I think load-(page) is the first hook in the admin screen that deals with outputting HTML, but I'm not totally sure. Basically I'm looking for the equivalent to template_rediect or get_header, which don't work on the admin side (correct me if I'm wrong).

I'd like to hook into every admin page. My guess using either $page_now or get_current_screen() but my mind is going blank on how I'd implement them on all pages:

$page = get_current_screen()->parent_file;
add_action( 'load-$page.php', 'add_action_all_load_hook', 1, 0 );

On the front-end I would do this:

function link_rel_buffer_callback($buffer) {
    $buffer = preg_replace('ORIGINAL', 'NEW', $buffer);
    return $buffer;
}

function link_rel_buffer_start() {
    ob_start("link_rel_buffer_callback");
}
function link_rel_buffer_end() {
    ob_flush();
}
add_action('template_redirect', 'link_rel_buffer_start', -1);
add_action('get_header', 'link_rel_buffer_start');
add_action('wp_footer', 'link_rel_buffer_end', 999);

I'm thinking the equivalent would be

add_action('load-$page', 'link_rel_buffer_end', 1, 0);
add_action('in_admin_footer', 'link_rel_buffer_end', 999);

but I can't grasp how to do load-(page) on every load.

// UPDATE BASED ON @birgire example

add_action( 'admin_init', 'wpse_admin_init' );
function wpse_admin_init( $buffer )
{
    if( ! defined( 'DOING_AJAX') || ! DOING_AJAX )
        ob_start( 'wpse_buffering' );
}

function wpse_buffering( $buffer )
{   
    $buffer = preg_replace('FIND', 'REPLACE', $buffer);
    return $buffer;
}


function wpse_buffering_shutdown() {
        ob_flush();
    }
add_action('in_admin_footer', 'wpse_buffering_shutdown', 9999);
2
  • what are you trying to do?
    – Tomás Cot
    Sep 14, 2014 at 1:27
  • Output buffering. I updated my answer Sep 14, 2014 at 1:31

1 Answer 1

2

You can try this one:

/**
 * Fires as an admin screen or script is being initialized.
 *
 * Note, this does not just run on user-facing admin screens.
 * It runs on admin-ajax.php and admin-post.php as well.
 *  
 * This is roughly analgous to the more general 'init' hook, which fires earlier.
 *
 * @since 2.5.0
 */
do_action( 'admin_init' );

if you need a hook activated on every admin page.

For example:

add_action( 'admin_init', 'wpse_admin_init' );

function wpse_admin_init( $buffer )
{
    if( ! defined( 'DOING_AJAX') || ! DOING_AJAX )
        ob_start( 'wpse_buffering' );
}

function wpse_buffering( $buffer )
{       
    return $buffer;
}

ps: If you need to hook into the part after </html>, you can use the PHP function register_shutdown_function() to run your own callback after the PHP script has finished executing. But notice that then the output buffers have already be sent to the client, according to this comment on the PHP docs.

3
  • Thanks birgire. I'd mostly just for basic replacement of hardcoded strings where gettext wont work - codex.wordpress.org/Plugin_API/Filter_Reference/gettext Sep 16, 2014 at 6:48
  • I updated my answer... I wasn't sure on if the regster_shutdown_function() so I didn't feel like messing with it too much since I probably woudn't ever need afterr </html>. Is my update with the ob_flush(); how you'd suggest implementing it? Sep 16, 2014 at 6:49
  • I wonder if you need the ob_flush(), since the buffer is automatically flushed at the end of the script.
    – birgire
    Sep 16, 2014 at 9:08

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.