• Seems like all the examples that I can find in codex show the wp_register_script and the wp_enqueue_script in the same function. If I only want to use a particular script in a single page it seems to me that I need to place that wp_enqueue_script in that page header. Otherwise the script will be loaded all the time. Right?
    So, wouldn’t I register the script in my function.php and then enqueue it in my page header?

    Very confusing…..!

    Thanks

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    You typically use the actions ‘wp_enqueue_scripts’ or ‘admin_enqueue_scripts’ to enqueue scripts. You are right in that this ends up loading scripts for all such pages. The correct way to limit where scripts are loaded is by some sort of conditional structure. You can use template tags such as is_home() or is_archive() to determine if the script should be enqueued. A more elaborate conditional is illustrated here to only load for plugin admin screens. This example leverages the nature of variable hooks as a sort of conditional.

    Thread Starter lwoods

    (@lwoods)

    Thanks, bcworkz….

    In my case I have a specific template that is being used to generate some non-WP data and is using jQuery for some “stuff”. So I plan on putting my “enqueue” in the PHP header area of this template. O.K. so far? BUT, I would put the “register” for this script in my function.php so that I only register it once, right?

    Believe it or not I have been using jQuery extensively for the last 7-8 years but this is the first time that I have run up against WP!

    Frustrating….and any help is MUCH appreciated!

    Moderator bcworkz

    (@bcworkz)

    WP does a few things differently where experienced coders get caught out doing things the “normal” way and can’t determine where things went wrong. Frustrating indeed.

    Yes, you can register your script using the same hooks as for enqueuing, and that code can reside in functions.php. Depending what else you do with this script, you may not actually need to register it at all. If nothing depends on this script and you don’t need to localize any data, you probably do not need to register it.

    Ideally, the script should also only be registered when needed, but since registering is really just adding a value to an array, it’s not a big deal if it is registered needlessly.

    I can’t say exactly why, but enqueuing your script in header.php seems like a bad idea to me. Maybe it’s just not done, maybe it’ll work fine, go ahead and try if you like. I still think you are best off using the conventional hooks and somehow determining if your template is being used. Another way you might make this determination is to check the global $wp_query, the main query object. It would certainly contain data somewhere that would indicate your template is being used. I think your time is better spent pursuing this angle than goofing around with header calls.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Where to place my wp_enqueue_script’ is closed to new replies.