[wp-hackers] questions on best practices for wp_enqueue_script and wp_enqueue_style

Nathan Rice ncrice at gmail.com
Tue Apr 7 19:10:10 GMT 2009


Mike,

1. In your function, just do a conditional:
if(!is_admin()) {
wp_enqueue_script...
}

2. the src wants a url, not a path (I think). Yes, it could be a relative
url, but that can be determined based on the location of the file you're
using to do the wp_enqueue_script in (../../file.js or something similar).
But really, the full url should work fine, and will probably be your best
bet.

Nathan


My Website
http://www.nathanrice.net/

My Twitter
http://twitter.com/nathanrice


On Mon, Apr 6, 2009 at 8:27 AM, Michael Toppa <public at toppa.com> wrote:

> Hi all,
>
> Mike Toppa here - I'm a new member to the list. I'm trying to figure out a
> couple things relating to wp_enqueue_script and wp_enqueue_style. I've
> looked at a few different blog posts people have written trying to explain
> these, and I'm finding incorrect advise (for example, calling them with
> wp_print_scripts is not a good idea).
>
> 1. I know to wrap calls to them in my own function. To use them outside the
> admin pages, what is the best action to call my function from? The codex
> says to use the init action, but that causes them to also load in the admin
> pages, which is undesirable. What is the best action hook to use so that
> they'll load in the public pages but not the admin pages? I came across a
> couple support threads with people suggesting the use of template_redirect,
> but that struck me as an odd choice (even if works, it doesn't seem like a
> conceptual fit).
>
> 2. Both functions want the src path defined from the root of the wordpress
> installation. PLUGINDIR seems to do the trick for the plugin directory. What
> is the equivalent for the active theme directory? TEMPLATEPATH gives the
> absolute path, and get_stylesheet_directory_uri() gives the absolute URL.
> How do I get the relative path? In my plugin I give users the option of
> customizing the plugin's css and placing a copy in their active theme
> folder, so I can't extrapolate the path from the location of my script.
>
> I'll be happy to update the codex with the correct answers if anyone knows
> them.
>
> Thanks very much,
>
> Mike T
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>


More information about the wp-hackers mailing list