Discussion:
Loading scripts in WordPress
Dave Navarro
2013-12-10 20:20:13 UTC
Permalink
At what point does WordPress process short codes in content?

I have a short-code I created for displaying a custom Google map and I
only want to load the necessary javascript files if the short-code is
used on a page. Are short-codes processed before the headers are sent
to the browser or after?

Thanks!
Chip Bennett
2013-12-10 20:22:28 UTC
Permalink
Shortcodes are parsed at the the_content filter.

That said: you can simply call wp_enqueue_script() inside your shortcode
callback, which will ensure that your scripts only get enqueued when the
shortcode is parsed.
Post by Dave Navarro
At what point does WordPress process short codes in content?
I have a short-code I created for displaying a custom Google map and I
only want to load the necessary javascript files if the short-code is used
on a page. Are short-codes processed before the headers are sent to the
browser or after?
Thanks!
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
Dave Navarro
2013-12-10 20:23:34 UTC
Permalink
Thank you for your quick response. I'll take a look at that.
Post by Chip Bennett
Shortcodes are parsed at the the_content filter.
That said: you can simply call wp_enqueue_script() inside your shortcode
callback, which will ensure that your scripts only get enqueued when the
shortcode is parsed.
Post by Dave Navarro
At what point does WordPress process short codes in content?
I have a short-code I created for displaying a custom Google map and I
only want to load the necessary javascript files if the short-code is used
on a page. Are short-codes processed before the headers are sent to the
browser or after?
Thanks!
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
Haluk Karamete
2013-12-10 22:20:10 UTC
Permalink
Post by Dave Navarro
Post by Chip Bennett
you can simply call wp_enqueue_script() inside your shortcode
callback, which will ensure that your scripts only get enqueued when the
shortcode is parsed.

That's interesting Chip. I thought that would have been too late.
Isn't it that when the "the_content()" is running, wp_head action hook has
already been executed?
Post by Dave Navarro
Thank you for your quick response. I'll take a look at that.
Post by Chip Bennett
Shortcodes are parsed at the the_content filter.
That said: you can simply call wp_enqueue_script() inside your shortcode
callback, which will ensure that your scripts only get enqueued when the
shortcode is parsed.
At what point does WordPress process short codes in content?
Post by Dave Navarro
I have a short-code I created for displaying a custom Google map and I
only want to load the necessary javascript files if the short-code is used
on a page. Are short-codes processed before the headers are sent to the
browser or after?
Thanks!
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
John Blackbourn
2013-12-10 22:25:20 UTC
Permalink
Post by Chip Bennett
Post by Chip Bennett
you can simply call wp_enqueue_script() inside your shortcode
callback, which will ensure that your scripts only get enqueued when the
shortcode is parsed.
That's interesting Chip. I thought that would have been too late.
Isn't it that when the "the_content()" is running, wp_head action hook has
already been executed?
If you call wp_enqueue_script() after the_header() then the script will be
enqueued in the footer. Dependencies will work as expected. (This wasn't
always the case, it was added in 3.3)

John
Haluk Karamete
2013-12-10 22:45:44 UTC
Permalink
it is quite clear now... thank you...


On Tue, Dec 10, 2013 at 2:25 PM, John Blackbourn
Post by John Blackbourn
Post by Chip Bennett
Post by Chip Bennett
you can simply call wp_enqueue_script() inside your shortcode
callback, which will ensure that your scripts only get enqueued when the
shortcode is parsed.
That's interesting Chip. I thought that would have been too late.
Isn't it that when the "the_content()" is running, wp_head action hook
has
Post by Chip Bennett
already been executed?
If you call wp_enqueue_script() after the_header() then the script will be
enqueued in the footer. Dependencies will work as expected. (This wasn't
always the case, it was added in 3.3)
John
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
Continue reading on narkive:
Loading...