Support » Plugin: WordPress Popular Posts » Print javascript in wp_footer

  • Resolved luke_antix

    (@luke_antix)


    Hi Hector,

    first of all, thank you a lot for your plugin, it has been a great help. I am using it on a custom themed page and run a custom database query to fetch the data I need.

    I updated today from 2.3.7 to 3.1.1, adapted my custom stuff to your new table structure and all worked well. However, I realized that no new pageviews were added. I investigated and saw that WPP prints its javascript (which relies on jQuery) in the head, while I load all my scrips in the footer. Therefore it threw an error and didn’t work.

    In order to fix it I went into you code and edited line 355 of wordpress-popular-posts.php from this:
    add_action( 'wp_head', array(&$this, 'print_ajax') );
    to this:
    add_action( 'wp_footer', array(&$this, 'print_ajax'), 100 );
    (the 100 is for making it print after all the other javascript is loaded)

    I thought you might want to change that for you next update. As far as I can see it won’t hurt and your ajax call is done after the page is loaded anyway.

    Blessings,
    Lukas

    https://wordpress.org/plugins/wordpress-popular-posts/

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Héctor Cabrera

    (@hcabrera)

    Hi Lukas,

    The truth is, as discussed here not long ago, I prefer to run the page views update as soon as possible – and that’s why the Ajax script is “hardcoded” in the header section. Edit: after that conversation, I added a CDN fallback to make sure jQuery is loaded when WPP requires it (see commit here).

    However, you made me notice that WPP indeed waits until the page is fully loaded before triggering the Ajax call which is not something I thought of before so I thank you for your comment. I’ll modify the script and remove the $(document).ready() jQuery hook.

    Regarding your request to move the Ajax call to the footer, again I prefer not to. However, I’ll think about it.

    I see your point. Put a thought into it and do what you consider best in your situation.

    Here maybe one more thought from my side: Since your javascript snippet is basically only a single ajax call, you might want to think about using vanilla javascript and not jQuery. Of course, most pages load jQuery anyway, but if not, they wouldn’t need to load a entire framework for a single ajax call.

    Blessings,
    Lukas

    Plugin Author Héctor Cabrera

    (@hcabrera)

    Hey Lukas,

    Thanks for the nice suggestion! Just switched to vanilla Javascript in order to reduce dependency on jQuery. This change (commit) will be available on the next version of the plugin.

    Edit: also dropped CDN jQuery.

    Excellent,
    I think this is the most optimal solution.
    Thank you.

    I had the Javascript to footer plugin enabled, which did not agree with your choice to load your scrip early. I would guess that almost all users with this particular problem (content not showing for popular posts) have some plugin or modification to load javascript in the footer (which is recommended to get content loaded fast).

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Print javascript in wp_footer’ is closed to new replies.