[wp-hackers] checking if post has shortcode, before header is output?
Aaron D. Campbell
aaron at xavisys.com
Mon Oct 26 18:59:43 UTC 2009
I still think it would be better to add the JS from the shortcode
handler rather than trying to mess with the_content, the_post,
$wp_query, $post, etc. Since it's enqueued inside the shortcode handler
you KNOW the shortcode is being executed. The only drawback is that the
JS needs to go in the footer rather than the header, but as far as I
know this is actually the recommended way of loading JS so you should
code for that anyway.
Jeremy Clarke wrote:
> On Mon, Oct 26, 2009 at 1:41 PM, Aaron D. Campbell <aaron at xavisys.com> wrote:
>> That still doesn't fix the case where more than one query is ran on a page,
>> when someone does something like "$myQuery = new WP_Query($params);" you'd
>> process the main query, but you wouldn't get any of the shortcodes in the
>> custom query.
> That's a logical point but I suspect that in the majority of cases
> people are using extra queries to show only parts of other content,
> headlines or excerpts, rather than the actual the_content(). In these
> cases checking the extra queries will actually cause the js to be
> loaded way more often than desired. If 1/5 articles needs the JS and
> you have 10 "related" headlines visible in each single.php view then
> the likelihood that one of the headlines is a post that *would* need
> the js is very high, and you'll be constantly loading it even though
> the headlines have no use for it.
> If you do show extra loops with full content (or you need this to work
> on indexes/archives that show multiple posts with full content) then
> +1 for the idea of adding the js to the footer. I believe there's a
> hook on 'the_post' that would work pretty well to identify posts that
> get displayed, though if its not too late the_content() would be
> ideal, as it would only account for posts who's full content ( and
> thus expanded shortcodes ) are displayed.
More information about the wp-hackers