[wp-hackers] Including javascript in the footer

Otto otto at ottodestruct.com
Tue May 17 19:37:29 UTC 2011


On Tue, May 17, 2011 at 12:44 PM, Dylan Kuhn <dylan.k.kuhn at gmail.com> wrote:
> Ah - I get how query_posts creates multiple main queries, what threw me for
> a loop in this thread is that I've always seen it done in template code
> (where I understand WP_Query is preferred). How would it be done before
> wp_enqueue_scripts fires?

You can create the query anywhere, really. Generally people create it
just before they use it, but if you know it's going to be used
multiple times, then you might as well create it earlier.

For example, I could hook to template_redirect, do my detection stuff
there, and if I determine I need my extra query, then just go ahead
and create it and pop it into a global for later use.

add_action('template_redirect','my_theme_query_check');
function my_theme_query_check() {
  if (i_need_my_query()) {
    global $my_custom_query;
    $my_custom_query = new WP_Query(whatever);
  }
}

add_action('wp_enqueue_scripts','my_theme_enqueue');
function my_theme_enqueue() {
  global $my_custom_query;
  // .. do a quick Loop and enqueue if needed ..
  $my_custom_query->rewind_posts();
}

.. later, in the template..
global $my_custom_query;
// .. do the full Loop to utilize the custom query


The WP_Query is just an object, and you can instantiate it in a
variable and use it wherever you like, later on.

> So likewise - I always do that in templates. Is the right way to instantiate
> the query prior to wp_enqueue_scripts, so the results can be analyzed for
> script dependencies?

If your script dependencies depend on the results of that query, then yes.

This is a special case though. Generally script enqueueing doesn't
really depend on the results of a query. The question you have to ask
yourself is whether or not it's worth the overhead of running the Loop
just to determine you need the script, or saying the hell with it and
just enqueueing the script all the time. For most scripts, they're
small, and static, and cached in the browser. So it rarely is worth
doing this sort of thing unless there is a substantial amount of
overhead involved with including the script.

Either way you have overhead. Is it faster to just include the script,
or is it faster to check if you need the script on every single page
load? Just including the script is often much quicker, when you look
at it from a overall site perspective. Scripts are static (or should
be), and servers are very fast at serving static files, and browsers
are very good at not asking for files they already have. Yes, if
you're using one of myriad page speed tools, then they'll say to
reduce the number of scripts you load, but are your visitors really
only looking at one page on your site at a time, or do they come back
often? Are you trying to save on bandwidth, or server execution time?

Checking whether or not to include scripts and CSS and other static
things is very often a case of premature optimization.

-Otto


More information about the wp-hackers mailing list