-3

here is what i got so far:

  // my array
$this->headersArray = array(
    array('handle' => 'buy_mobile_funcs', 'src' => 'jsFunctions', 'extention' => 'js', 'deps' => '', 'media' => ''),

    array('handle' => 'css_main_style', 'src' => 'main_style', 'extention' => 'css', 'deps' => '', 'media' => 'screen')
);

// registering 
foreach($this->headersArray as $singleHeader){
    if($singleHeader['extention'] == 'js'){
        wp_register_script(
            $singleHeader['handle'], plugins_url('scripts/'.$singleHeader['src'].'.'.$singleHeader['extention'],__FILE__), 
            (!empty($singleHeader['deps']))? $singleHeader['deps']  : ''
        );
    }else{
        wp_register_style(
            $singleHeader['handle'], plugins_url('css/'.$singleHeader['src'].'.'.$singleHeader['extention'],__FILE__), 
            (!empty($singleHeader['deps']))? $singleHeader['deps']  : ''
        );
    }
}

// enqueuing 
foreach($this->headersArray as $singleHeader){
    if($singleHeader['extention'] == 'js'){
        wp_enqueue_script($singleHeader['handle']);
    }else{
        wp_enqueue_style( $singleHeader['handle'], '', '', '', $singleHeader['media'] );
    }
}

I know i could combine the 2 foreach. however i have a reason to keep it as it is now which is not relevant to the questions.

Error:

Notice: 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. Please see Debugging in WordPress for more information. (This message was added in version 3.3.) in /home1/stodeckc/public_html/orgadsite/wp-includes/functions.php on line 3012

Thanks in advanced.

6
  • 2
    1. As is, this seems like more of a PHP question. 2. You should probably explain what the error is instead of asking us to guess. 3. Why are you making this more complicated than it should be? Oct 28, 2013 at 23:21
  • it aint more complicated then it should be. instead of loading 8-15 different script manually i am doing it dynamically. updated the post with the error
    – Neta Meta
    Oct 28, 2013 at 23:23
  • 1
    It is more complicated because you're adding more lines of code, reducing readability while (in my opinion, of course) not getting anything out of it. Manually enqueing a script is 1 line of code. Based on your error code, this is a duplicate of: #78836 and #49339, not to mention having plenty of Google search results for this exact error. Oct 28, 2013 at 23:46
  • 1
    Well, indeed, googling for the error code is rule zero of debugging... But if there are 8/15 scripts, I don't see why not doing like you are. Suggestion, you already checked for the extension, why not reduce to [src].'.js',__FILE__)?
    – brasofilo
    Oct 28, 2013 at 23:52
  • helgathevikin - its not 1 line per script. its 2 lines for registering and enqueue. and if you have 8-15 scripts to load thats 16-30 lines/ and later editing them is a mass. instead you have them all in 1 array where they are organized. " that is what you get from it"
    – Neta Meta
    Oct 28, 2013 at 23:59

1 Answer 1

0

As the "Error Message" (which is in fact a "doing it wrong"-message) is telling you, you're using the wrong hook:

Scripts and styles should not be registered or enqueued until the wp_enqueue_scripts, admin_enqueue_scripts, or login_enqueue_scripts hooks.

Just read about hooks, wrap your code in a callback and then register it properly on one of the three hooks (hint: the one that fits).

1
  • Kaiser Thanks very much your answer helped alot.. i did not added an action it was just hanging out there.
    – Neta Meta
    Oct 29, 2013 at 0:21

Not the answer you're looking for? Browse other questions tagged or ask your own question.