1

I have several .js and .css files which I created for my theme. (not a child theme)

Styles and scripts are at the moment hard-coded in the head and work as wished. Now I read a lot about register and enqueue and move to footer.
(I am also aware that loading scripts into the footer is for better page loading.)

My question:

  • Do I need to first register and then enqueue them both (.js and .css)
  • If yes, why also the need to register them

An example which imho could work, or?

add_action( 'wp_enqueue_scripts', 'our_theme_scripts_styles' );
function the_theme_scripts_styles() {
    // extra stylesheet (In parent theme)
    wp_register_style( 'my-style-css', get_template_directory_uri() . '/assets/css/my-style.css' );
    wp_enqueue_style( 'my-style-css', get_template_directory_uri() . '/assets/css/my-style.css' );

    // move script(s) into footer (In parent theme)
    wp_register_script( 'my-script', get_template_directory_uri() . '/assets/js/my-script.js', array(), false, true );  
    wp_enqueue_script(  'my-script', get_template_directory_uri() . '/assets/js/my-script.js', array(), false, true );
}

I am pretty new in the WP 'world' so it would be nice to find some answers for my questions.

0

If you are registering the styles/scripts, then you will be setting all the parameters at that point. No need to pass them all again when enqueuing - by then you will only need to pass the handle.

If you are definitely only going to call your styles/scripts in one place, then you can skip the registration entirely. Instead, just enqueue, and pass all the params on the enqueue.

| improve this answer | |
  • Both, .js and .css files will be used in 3 different custom post types and for 1 category. I assume the function as shown above is correctly? Could you enlight me a little more about the 'in one place' please? – Jerry.S Jul 22 '15 at 21:00
  • In one place in the code. – vancoder Jul 22 '15 at 21:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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