Bird
0
0

Which is the correct way to add a function in functions.php that runs when WordPress initializes?

easy📝 Syntax Q3 of 15
Wordpress - Theme Structure and Basics
Which is the correct way to add a function in functions.php that runs when WordPress initializes?
Aadd_action('init', 'my_custom_function'); function my_custom_function() { /* code */ }
Badd_filter('init', 'my_custom_function'); function my_custom_function() { /* code */ }
Cadd_action('wp_head', 'my_custom_function'); function my_custom_function() { /* code */ }
Dadd_filter('wp_footer', 'my_custom_function'); function my_custom_function() { /* code */ }
Step-by-Step Solution
Solution:
  1. Step 1: Identify the correct hook for initialization

    The 'init' hook is used to run code when WordPress initializes.
  2. Step 2: Choose the correct hook function

    add_action is used to attach functions to hooks like 'init'. add_filter is for filters, not actions.
  3. Final Answer:

    add_action('init', 'my_custom_function'); function my_custom_function() { /* code */ } -> Option A
  4. Quick Check:

    Use add_action with 'init' for initialization [OK]
Quick Trick: Use add_action('init', function) to run code on init [OK]
Common Mistakes:
MISTAKES
  • Using add_filter instead of add_action for hooks
  • Using wrong hook names like 'wp_footer' for init

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Wordpress Quizzes