Show a “Please Wait” Message or a Progress Bar whi

2020-05-09 01:06发布

问题:

I use the following WordPress admin notice to prompt users to download some files. I would like to include either a progress bar or at least a "Downloading - Please wait" message while files are downloading.

Any ideas?

I've tried several jQuery solutions but could get nothing to work. I'm a total noob when it comes to jQuery.

/* Ask user to download GeoIP database files. */
add_action( 'admin_notices', 'lsmi_dl_admin_notice' );
add_action( 'network_admin_notices', 'lsmi_dl_admin_notice' ); // also show message on multisite
function lsmi_dl_admin_notice() {
    $dir = dirname( __FILE__ );
    $localfilev4 = $dir . '/data/GeoIPv4.dat';
    $localfilev6 = $dir . '/data/GeoIPv6.dat';
    $ctx = stream_context_create( array( 'http' => array( 'timeout' => 120 ) ) ); 
    if ( !file_exists( $localfilev4 ) ) {
        if ( current_user_can( 'install_plugins' ) ) {
            echo
            '<div class="notice notice-warning is-dismissible"><p>Notice: This plugin uses Maxmind Geolite databases for better accuracy. Click the download button to install now.
            <form action="" method="get">
            <input type="submit" class="button" name="download" value="download" />
            </div>';
            if($_GET){
                if(isset($_GET['download'])){
                    $newfilev4 = file_get_contents( "https://sourceforge.net/projects/geoipupdate/files/GeoIPv4.dat/download", 0, $ctx );
                    file_put_contents( $dir . '/data/GeoIPv4.dat', $newfilev4 );
                    if ( !file_exists( $localfilev6 ) ) {
                        $newfilev6 = file_get_contents( "https://sourceforge.net/projects/geoipupdate/files/GeoIPv6.dat/download", 0, $ctx );
                        file_put_contents( $dir . '/data/GeoIPv6.dat', $newfilev6 );
                    }
                }
                echo '<meta http-equiv="refresh" content="0">';
            }
        }
    }
}

回答1:

Try giving your button an ID like so:

<input type="submit" class="button" name="download" value="download" id="download" />

Also give your div an id like so:

<div class="notice notice-warning is-dismissible" id="download-div">

Then we can use a basic jQuery onClick function and change the 's innerhtml like so:

    $("#download").click(
         function () {
             $('#download-div').html("Please wait...");
         }            
     );
 });

Hopefully this helps :)