简体   繁体   中英

How to reload the page after successful upload in the Simple Local Avatars WordPress plugin?

I'm using Simple Local Avatars to allow my blog's authors to upload an avatar.

My aim is to refresh the page after successful upload. My first thought was to look in the simple-local-avatars.php source code for an action hook. I was hoping there would be a hook such as avatar_upload_success near the end of the edit_user_profile_update method but nothing like that is available. So I need to find an alternative way to refresh the page.

How else can I refresh the page after successful upload?

UPDATE: Just to make my question a little clearer, my problem isn't how to reload the page, my problem is how do I reload the page on upload success only. For example: On upload failure I don't want to reload.

UPDATE 2: So my next attempt was to do something like this:

function simple_local_avatar_reload() {
    $url = 'whatever the current page is';
    wp_redirect( $url );
    exit();
}
add_action( 'edit_user_profile_update', 'simple_local_avatar_reload', 99 );

But that causes the dreaded 'headers already sent' error.

My solution was to enable output buffering conditionally and then perform a page refresh after edit_user_profile_update has been completed. For example:

/**
 * Enable output buffering.
*/
function output_buffer() {
    if ( isset ( $_POST['submit'] ) )
        ob_start();
}
add_action( 'init', 'output_buffer' );

/**
 * Refresh page after avatar upload.
*/
function simple_local_avatar_reload() {
    header( 'Location: ' . $_SERVER['REQUEST_URI'] );
}
add_action( 'edit_user_profile_update', 'simple_local_avatar_reload', 99 );

Note: This performs a reload even if the upload fails. But it's a workaround I'm going to have to run with unless I modify the plugin's core (which I don't want to do).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM