Advanced WordPress Interview Questions & Answers

Q1. What is the difference between an action hook and a filter hook in WordPress?

Answer:

  • Action Hook (do_action()) → Executes functions at specific points but does not modify data.

  • Filter Hook (apply_filters()) → Modifies data before it is returned or displayed.

Example:

// Action: Runs custom logic but does not modify data
add_action('wp_footer', function() { echo '<p>Footer Loaded</p>'; });

// Filter: Modifies the title before displaying
add_filter('the_title', function($title) { return $title . ' - Custom'; });

Q2. Can you explain how priority works in add_action() and add_filter()?

Answer:

  • Lower priority (1-10) executes earlier.

  • Higher priority (11+) executes later.

  • Default priority is 10.

Example:

add_action('wp_head', 'first_function', 5);
add_action('wp_head', 'second_function', 15);

function first_function() { echo '<!-- First -->'; }
function second_function() { echo '<!-- Second -->'; }

📌 Here, first_function runs before second_function.


Q3. How does the return value differ between an action hook and a filter hook?

Answer:

  • Action hooks do not return values (just execute a function).

  • Filter hooks must return a value after modification.


Q4. How do you remove an already added action or filter?

Answer:

remove_action('wp_head', 'wp_generator');
remove_filter('the_content', 'custom_filter_function');

🛑 Removing a function from a class:

remove_action('wp_head', array('My_Class', 'method_name'));

Q5. How can you execute a custom function only once using a hook?

Answer:

function my_function() {
if (!get_option('my_function_ran')) {
// Run once logic
update_option('my_function_ran', true);
}
}
add_action('init', 'my_function');

Q6. What are dynamic hooks, and how can you use them in WordPress?

Answer:
Dynamic hooks allow you to hook into specific instances of actions or filters dynamically.

Example: Hook into specific post types dynamically:

add_action("save_post_product", function($post_ID) {
error_log("Product $post_ID saved!");
});

📌 This only triggers when saving a product post type.


Q7. What is the purpose of apply_filters() and do_action()?

Answer:

  • apply_filters('hook_name', $value); → Modify and return data.

  • do_action('hook_name'); → Execute custom functions at specific points.

Example:

// Using apply_filters to modify data$title = apply_filters('modify_title', 'Original Title');


// Using do_action to trigger custom functions
do_action('custom_action');

Q8. Modify the post title using a filter hook

Answer:

function modify_post_title($title) {
return $title . ' - Custom Title';
}
add_filter('the_title', 'modify_post_title')

🛑 For specific post types:

function modify_custom_post_title($title, $post_id) {
if (get_post_type($post_id) === 'product') {
return $title . ' - Product Edition';
}
return $title;
}
add_filter('the_title', 'modify_custom_post_title', 10, 2)

Q9. Add a custom function to execute when a post is published

Answer:

function notify_admin_on_publish($post_ID, $post) {
if ($post->post_status == 'publish') {
wp_mail('[email protected]', 'New Post Published', 'A new post is live.');
}
}
add_action('publish_post', 'notify_admin_on_publish', 10, 2);

Q10. Remove an existing action hook

Answer:

remove_action('wp_head', 'wp_generator');

🛑 If added inside a class:

remove_action('wp_footer', array('My_Class', 'my_function'));

Q11. Create a custom action hook and trigger it

Answer:

function custom_login_function($user_login, $user) {
do_action('custom_after_login', $user_login, $user);
}
add_action('wp_login', 'custom_login_function', 10, 2);

// Hook into custom action
add_action('custom_after_login', function($user_login, $user) {
error_log('User ' . $user_login . ' logged in.');
}, 10, 2);

Q12. Change the excerpt length using a filter

Answer:

function custom_excerpt_length($length) {
return 20;
}
add_filter('excerpt_length', 'custom_excerpt_length');

🛑 For specific categories:

function category_excerpt_length($length) {
if (in_category('news')) {
return 10;
}
return $length;
}
add_filter('excerpt_length', 'category_excerpt_length');

Q13. If you have a plugin that registers an action at priority 10, and your theme registers the same action at priority 5, which one executes first?

Answer:
The theme’s action runs first because it has a lower priority (5 < 10).


Q14. How would you hook into woocommerce_after_add_to_cart_button and add a custom button?

Answer:

add_action('woocommerce_after_add_to_cart_button', function() {
echo '<a href="#" class="custom-btn">Compare</a>';
});

Q15. How would you temporarily disable all hooks for a specific request?

Answer:

remove_all_actions('init');

Q16. How can you make sure a function executes only if a specific plugin is active?

Answer:

if (is_plugin_active('woocommerce/woocommerce.php')) {
add_action('init', 'my_custom_function');
}

Q17. Which hook would you use to modify WooCommerce checkout fields dynamically?

Answer:

add_filter('woocommerce_checkout_fields', function($fields) {
$fields['billing']['billing_phone']['placeholder'] = 'Enter your phone';
return $fields;
});

Q18. Can you execute an action hook asynchronously in WordPress?

Answer:
Use wp_cron or wp_remote_post:

wp_schedule_single_event(time(), 'my_async_task');
add_action('my_async_task', function() {
// Your async code here
})

Leave a Reply

Your email address will not be published. Required fields are marked *