Viewing 6 replies - 1 through 6 (of 6 total)
  • something like this with jQuery :

    $(function() {
      $('a[data-auto-download]').each(function(){
        var $this = $(this);
        setTimeout(function() {
          window.location = $this.attr('href');
        }, 2000);
      });
    });
    <p>The download should start shortly. If it doesn't, click
    <a data-auto-download href="/your/file/url">here</a>.</p>
    Thread Starter lutsukomm

    (@lutsukomm)

    Thank you very much for your reply. This one is working:

    <p>The download should start shortly. If it doesn’t, click
    here.</p>

    but where do I have to enter this one:
    $(function() {
    $(‘a[data-auto-download]’).each(function(){
    var $this = $(this);
    setTimeout(function() {
    window.location = $this.attr(‘href’);
    }, 2000);
    });
    });

    Inside the HTML or Page Custom CSS, Scripts? I was trying to add it but not working. The page shows just this script like it’s written.

    Moderator bcworkz

    (@bcworkz)

    Ideally, the jQuery script is in an external file and it’s loaded with wp_enqueue_script(). Note that you either need a noConflict wrapper or to use jQuery in place of the $ shortcut. The main purpose of doing it this way is the enqueue function allows you to specify jQuery as a dependency so that all the needed jQuery files are loaded along with your script.

    Another external file for a simple snippet certainly seems like overkill. You can just enqueue jQuery itself and output the jQuery snippet inside of <script> tags. The noConflict accommodations still apply.

    @lutsukomm use the ‘code’ tab when you put code on topics it’s better,
    i made the same mistake until the arrival of a moderator

    • This reply was modified 7 years, 10 months ago by lolo343.
    Thread Starter lutsukomm

    (@lutsukomm)

    Thank you for the replies.

    I was adding this one to the HTML:

    <p>The download should start shortly. If it doesn’t, click
    here.</p>

    and this one I was adding on Custom Page CSS and also tried to add it here Custom Page Scripts, but it’s not working:

    $(function() {
      $('a[data-auto-download]').each(function(){
        var $this = $(this);
        setTimeout(function() {
          window.location = $this.attr('https://www.mysite.com/image.png');
        }, 2000);
      });
    });

    What I am doing wrongly?

    Moderator bcworkz

    (@bcworkz)

    Check you page’s source code and confirm there is a script tag in the head section with a src attribute linked to /wp-includes/js/jquery/jquery.js and another one to jquery-migrate-min.js. If not, see my previous comment about wp_enqueue_script().

    Also, use jQuery in place of the $ shortcut, for example
    jQuery(function() { //etc.
    unless the entire code block starts off with
    jQuery( document ).ready( function( $ ) {
    in that case the $ shortcut is fine. Note the $ passed in function(). Without that, you must use the full jQuery.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Force a page to download a file automatically?’ is closed to new replies.