Bird
0
0

Identify the error in this code snippet that tries to enqueue a JavaScript file:

medium📝 Debug Q14 of 15
Wordpress - Theme Structure and Basics
Identify the error in this code snippet that tries to enqueue a JavaScript file:
function add_my_script() {
  wp_enqueue_script('my-script', get_template_directory() . '/js/script.js');
}
add_action('wp_enqueue_scripts', 'add_my_script');
AUsing wrong function name add_action instead of add_script
BMissing the version parameter in wp_enqueue_script
CNot hooking to wp_head action
DUsing get_template_directory() instead of get_template_directory_uri() for URL
Step-by-Step Solution
Solution:
  1. Step 1: Check the URL function used

    get_template_directory() returns a server path, not a URL. For enqueuing scripts, a URL is needed, so get_template_directory_uri() should be used.
  2. Step 2: Verify other parts

    The version parameter is optional, so missing it is not an error. Hooking to wp_enqueue_scripts is correct. add_action is the right function to hook.
  3. Final Answer:

    Using get_template_directory() instead of get_template_directory_uri() for URL -> Option D
  4. Quick Check:

    Use URI function for URLs, not directory path [OK]
Quick Trick: Use get_template_directory_uri() for URLs, not get_template_directory() [OK]
Common Mistakes:
MISTAKES
  • Using server path instead of URL for script source
  • Confusing hooks like wp_head vs wp_enqueue_scripts
  • Thinking version parameter is mandatory

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Wordpress Quizzes