My plugin uses wp_enqueue_style() and wp_enqueue_script() to load resources, but I have kept in mind not to degrade the performance for the user and only load these when the current set of posts contain the shortcode for my plugin. I do this by using:
the_posts is called before wp_head, but it is also called a few times afterwards (probably for recent posts in the widgets etc).
The problem here is that when I am searching the post contents for my shortcode and signalling the enqueuing process, there are times when:
- the_post is called before wp_head (what I want)
- but that set of posts does not have my shortcode and I don't enqueue.
- later on, another call to the_posts tries to enqueue but it's too late.
Is there a definite way to check what will appear on a page before wp_head is called so that I can be confident that I will enqueue only when needed but not miss the opportunity?
But it doesn't take into account multiple calls to the_posts in a single page request :(
What I've tried is to use:
wp action is only used once right after the request is finalised.
And inside this function:
$posts = $wp_query->posts;
I'm not sure how the $wp_query variable changes over the loop however. If the_posts filter is being called again and again, would it imply the original query may be changing as the widgets send across their own requests? Obviously I don't really want to check for shortcodes in the posts of "recent posts", since they may not be displaying on the page at the moment. So will using the
wp action hook instead of
the_posts filter contain the posts requested for that given page, whether it were a single or the home page?