Title: wp_register_script / wp_enqueue_script errors
Last modified: December 28, 2020

---

# wp_register_script / wp_enqueue_script errors

 *  [jon424](https://wordpress.org/support/users/jon424/)
 * (@jon424)
 * [5 years, 3 months ago](https://wordpress.org/support/topic/wp_register_script-wp_enqueue_script-errors/)
 * I am getting the following errors:
 * > wp_register_script was called incorrectly. Scripts and styles should not be
   > registered or enqueued until the wp_enqueue_scripts, admin_enqueue_scripts,
   > or login_enqueue_scripts hooks. This notice was triggered by the some_handle
   > handle. Please see Debugging in WordPress for more information. (This message
   > was added in version 3.3.0.)
 * > wp_enqueue_script was called incorrectly. Scripts and styles should not be 
   > registered or enqueued until the wp_enqueue_scripts, admin_enqueue_scripts,
   > or login_enqueue_scripts hooks. This notice was triggered by the some_handle
   > handle. Please see Debugging in WordPress for more information. (This message
   > was added in version 3.3.0.)
 * I’ve narrowed it down to some logic I’ve included in my functions.php file. What
   I am doing here is querying the database of users to see which users selected
   a certain checkbox on their registration form, though I must not be enqueueing
   the scripts correctly. Any idea on what I am doing wrong?
 *     ```
       //display conditional link:
       //1. get User Status
        $user_id   = get_current_user_id();
        $user_output =  get_user_meta($user_id, 'needs_extra_item', true);
        //echo $user_output[0];
   
       //2. Register the script
       wp_register_script( 'some_handle', './js/jqueryLaPuerta.js' );
   
       //3. Localize the script with new data
       $some_array = array(
           'some_string' => __( $user_output, 'plugin-domain' ),
           'a_value' => '10'
       );
       wp_localize_script( 'some_handle', 'object_name', $some_array );
   
       //4. Enqueued script with localized data.
       wp_enqueue_script( 'some_handle' );
       ```
   

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

 *  [Joy](https://wordpress.org/support/users/joyously/)
 * (@joyously)
 * [5 years, 3 months ago](https://wordpress.org/support/topic/wp_register_script-wp_enqueue_script-errors/#post-13841718)
 * The error message indicates the problem: your code is calling the function on
   a hook that is before the scripts hook. You have to put the code on the `wp_enqueue_scripts`
   hook (for front-end) or `admin_enqueue_scripts` hook (for admin).
 * Also, your call to register the script needs an absolute path.
    Also, your code
   shows you calling a translation function on a variable. This won’t work at all.
   Also, you can enqueue the script without registering it. The localization doesn’t
   happen until the script is output, so you might want to put it into the footer.
 *  Thread Starter [jon424](https://wordpress.org/support/users/jon424/)
 * (@jon424)
 * [5 years, 3 months ago](https://wordpress.org/support/topic/wp_register_script-wp_enqueue_script-errors/#post-13844664)
 * Thanks for the feedback. I fixed this issue by wrapping everything inside an 
   add_action hook:
 *     ```
       add_action('wp_enqueue_scripts', function(){
             //1. get User Status
             $user_id   = get_current_user_id();
             $user_output =  get_user_meta($user_id, 'needs_extra_item', true);
             //echo $user_output[0];
   
             //2. Register the script
             wp_register_script( 'some_handle', './js/jqueryLaPuerta.js' );
   
             //3. Localize the script with new data
             $some_array = array(
                 'some_string' => __( $user_output, 'plugin-domain' ),
                 'a_value' => '10'
             );
             wp_localize_script( 'some_handle', 'object_name', $some_array );
   
             //4. Enqueued script with localized data.
             wp_enqueue_script( 'some_handle' );
       });
       ```
   
 *  [Joy](https://wordpress.org/support/users/joyously/)
 * (@joyously)
 * [5 years, 3 months ago](https://wordpress.org/support/topic/wp_register_script-wp_enqueue_script-errors/#post-13844753)
 * You still can’t use a variable on a call to `__()`.
    _and the other things still
   apply_

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

The topic ‘wp_register_script / wp_enqueue_script errors’ is closed to new replies.

## Tags

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

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 3 replies
 * 2 participants
 * Last reply from: [Joy](https://wordpress.org/support/users/joyously/)
 * Last activity: [5 years, 3 months ago](https://wordpress.org/support/topic/wp_register_script-wp_enqueue_script-errors/#post-13844753)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
