• Hi,

    First I want to tell you sorry for my bad english, it’s not my native language.

    I’m actually in an internship and in charge to solve the differents problems of a company’s blog on WordPress. One of the main problem is the time of consultation. I noticed that a lot of external scripts and ressources are called before the content of the blog, what leaves an empty contents for long seconds.

    I’m just starting out with WordPress, I have not a lot of notion in WP back-end. But I wonder if it was possible to order the different scripts of those elements (Google Analytics, Google Adsense, Facebook, Twitter) to make them load after the content of the page, with an extension or in the theme code. I tried the extension “Re-Order” but I can’t find a script in the list that refered to social networks or google API… Most of them are generated by anothers extensions (Social Share Buttons, Google Analytics extension) or embedded code in “text” widgets.

    Thank you in advance,

    N.C.

    • This topic was modified 7 years, 7 months ago by Nokaa.
    • This topic was modified 7 years, 7 months ago by Nokaa.
Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    All external scripts are supposed to be referenced through the use of wp_enqueue_script(). This function has a parameter that indicates the script should load in the footer instead of head. When enqueuing your own script, this is a good option for many scripts.

    In the case of scripts loaded through plugins, you could hack their code to use that same parameter, but your change will be overwritten when the plugin is updated. All scripts enqueued are stored in the global $wp_scripts. You could alter the parameter through this array. Var_dump the array to study its structure and to determine how to access the in footer parameter.

    You’ll need to do this alteration after all scripts have been enqueued, but before the script references are output in head. There’s some optimal action hook for doing this, but I don’t know what it is. What should work is to hook the same “wp_enqueue_scripts” action with a very large priority number so your callback that alters the in footer parameter executes last. The largest integer you could use is represented by the constant PHP_INT_MAX.

    Thread Starter Nokaa

    (@nokaa)

    Indeed, it’s embarrassing…

    But, I noticed that the other part of a page (header, footer and aside) were generated before those scripts and the content.
    I wonder if it was possible to do the opposite. Make the content react as the others elements of the page, make it load before the others scripts ?

    I don’t really understand how this priority is managed because, there’s a get_header and get_footer, for example in the single.php file, which seem to be directly loaded, but the post traitement between them is loaded after all thoses others scripts I mentionned before (Google API, Facebook, Ads, etc.)…

    • This reply was modified 7 years, 7 months ago by Nokaa.
    • This reply was modified 7 years, 7 months ago by Nokaa.
    Moderator bcworkz

    (@bcworkz)

    Priority is part of how hook callbacks are managed. When a hook event fires, the smallest numbered callbacks are executed first, largest last. For those callbacks with the same priority, the order is officially undefined, but evidence indicates they are called alphabetically.

    Generally speaking, content is rendered as it is encountered when possible, but in many cases there is render blocking data that is needed before content is rendered. This is usually something in the main content — images with no size information need the actual image loaded first, or sliders need their scripts loaded, etc. Ancillary content often is less complex, so it shows up first. Additionally, such content may be cached by the browser from previous pages, so display is almost instantaneous. Main content is generally new and different and must wait until enough data comes in.

    The browser decides on its own what to display when. While there may be a way to withhold data to force one thing to load before another, the main result will be significantly slowing the page load speed. Not a good approach IMO. Aside from loading not immediately needed data in the footer, the best thing you can do is speed up the entire process in general. Significant speed increases are usually easily accomplished by installing a caching plugin and configuring it for optimal performance on your site. The installation defaults are frequently not optimal settings.

    Google’s page insight tool will analyze your page and make recommendations on how to improve loading speed. This link is not the latest version, but gives better information IMO: https://developers.google.com/speed/pagespeed/insights/
    Some things will not be easy to address, or even extremely impractical. But some may be very easy. Do what you can within reason.

    Thread Starter Nokaa

    (@nokaa)

    Yes, I agree with you on this point.
    I already used those speed tools.

    The second person and me in charge of the website optimization have already do the best we could do. This was the last problem, but it will be difficult to convince the claimants that the task is more or less impossible to manage (based on the permise that they don’t like too much the fact we touch the files of the theme), and the time we can save on this should not be very profitable…

    Hope your answer will better enlighten them on this subject.
    Thank you anyway for your help !

    • This reply was modified 7 years, 7 months ago by Nokaa.
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Change scripts order (Google Analytics, Ads, and Social Networks)’ is closed to new replies.