[wp-hackers] checking if post has shortcode, before header is output?
CaptainN at unFocus.com
Mon Oct 26 20:46:54 UTC 2009
This is almost what I ended up doing. Outputting the <style> tag might
violate some standard, but it does work, so I'll keep it that way - and
adjust for HTML5 later (thanks Ken!). I'm inline outputting the scripts
too, but moving those to the footer sounds like a good idea.
Getting any of this into the header on only the right pages sounds
exceedingly difficult, and costly in terms of necessary code bloat, for
little (any?) benefit.
Thanks for all the suggestions.
On 10/26/09 2:59 PM, Aaron D. Campbell wrote:
> 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);"
>>> 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.
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
More information about the wp-hackers