0

Do I place add_action before or after function myTheme_script {enqueue functions here} when enqueueing a script or stylesheet into functions.php, or does it matter? I'm seeing both from moderators on WP forums.

So...

function profondo_scripts() {
    wp_enqueue_style( 'twentyfifteen-style', get_template_directory_uri() . '/style.css' );
    wp_enqueue_style( 'profondo-child-style', get_stylesheet_uri() );
} 

add_action( 'wp_enqueue_scripts', 'profondo_scripts' );

or...

add_action( 'wp_enqueue_scripts', 'profondo_scripts' );

function profondo_scripts() {
    wp_enqueue_style( 'twentyfifteen-style', get_template_directory_uri() . '/style.css' );
    wp_enqueue_style( 'profondo-child-style', get_stylesheet_uri() );
} 
1
  • 1
    Does not matter what so ever. At least from what I've seen. Others do it the other way and others the other. I like to put the add_action after the function so I "know" that I have already declared the function before calling it. But works both ways.
    – jimihenrik
    Commented Apr 25, 2015 at 19:44

1 Answer 1

1

Makes no difference. Despite the fact add_action doesn't actually check if the callback is indeed callable (i.e. is a valid function/class/method), PHP will first "load" function and class definitions before executing inline code, hence why you can do something like:

wpse_185390_function(); // Perfectly fine, even though the function is defined "afterwards"

function wpse_185390_function() {
    echo 'I\'m down here!';
}

It's entirely up to you if you "hook" before or after the definition - I personally prefer after for consistency (it's how pretty much all WordPress core does it).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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