Title: wp_enqueue_script with add_action(&#039;init&#039;)
Last modified: August 19, 2016

---

# wp_enqueue_script with add_action('init')

 *  [AndySpartan](https://wordpress.org/support/users/andyspartan/)
 * (@andyspartan)
 * [15 years, 2 months ago](https://wordpress.org/support/topic/wp_enqueue_script-with-add_actioninit/)
 * Hi there,
 * I want to enqueue some scripts and styles on the ‘init’ action, but only if a
   certain condition is met i.e the user is viewing a category page with an ID of
   12. I’ve tried a couple of things to get it to work but am having no luck 🙁 
   Below is some sample code from my functions.php file:
 *     ```
       function the_quote_files() {
         if(is_category(12)) {
           wp_enqueue_script(<args here);
           ... more scripts and styles ...
         }
       }
       add_action('init','the_quote_files');
       ```
   
 * I just can’t seem to get it to fire, I’ve also tried using get_query_var(‘cat’)
   to no avail.
 * My only guess as to why it isn’t working is when the ‘init’ action is happening,
   no data has been processed.
 * Does anyone know how I can get the scripts to load only when the user is viewing
   a certain category archive page?
 * Thanks, Andy

Viewing 2 replies - 1 through 2 (of 2 total)

 *  Thread Starter [AndySpartan](https://wordpress.org/support/users/andyspartan/)
 * (@andyspartan)
 * [15 years, 2 months ago](https://wordpress.org/support/topic/wp_enqueue_script-with-add_actioninit/#post-1968207)
 * Ok I posted too soon, just found out how to do it if anybody else is interested..
   instead of hooking on to init just hook on to wp_head like so:
 * `add_action('wp_head','the_quote_files',0);`
 *  [Chip Bennett](https://wordpress.org/support/users/chipbennett/)
 * (@chipbennett)
 * [15 years, 2 months ago](https://wordpress.org/support/topic/wp_enqueue_script-with-add_actioninit/#post-1968219)
 * You should enqueue front-end scripts using the `wp_print_scripts` hook, rather
   than the `wp_head` hook. Otherwise, you figured it out!
 * (The `init` hook is too soon, I think, to perform conditionals based on $post
   data.)

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘wp_enqueue_script with add_action('init')’ is closed to new replies.

## Tags

 * [add_action](https://wordpress.org/support/topic-tag/add_action/)
 * [init](https://wordpress.org/support/topic-tag/init/)
 * [wp_enqueue_script](https://wordpress.org/support/topic-tag/wp_enqueue_script/)

 * 2 replies
 * 2 participants
 * Last reply from: [Chip Bennett](https://wordpress.org/support/users/chipbennett/)
 * Last activity: [15 years, 2 months ago](https://wordpress.org/support/topic/wp_enqueue_script-with-add_actioninit/#post-1968219)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
