• 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)
  • 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

    (@jon424)

    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' );
    });

    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.