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
    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 Apr 25 '15 at 19:44
up vote 1 down vote accepted

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

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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