Hooks (Actions and Filters) Flashcards

1
Q

Q: What are hooks in WordPress?

A

A: Hooks are functions that allow developers to modify or add functionality to WordPress without modifying core files.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Q: What are the two types of hooks in WordPress?

A

A: Action hooks and filter hooks.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Q: What is an action hook?

A

A: A hook that allows you to add or execute custom code at specific points in the WordPress lifecycle.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Q: What is a filter hook?

A

A: A hook that allows you to modify data before it is output or saved.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Q: How do you add a function to a hook?

A

A: Using the add_action() or add_filter() functions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Q: What is the syntax for add_action()?

A

add_action( ‘hook_name’, ‘callback_function’, $priority, $accepted_args );

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Q: What is the purpose of the init action hook?

A

A: It is triggered after WordPress has finished loading but before headers are sent.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Q: What is the wp_head action hook used for?

A

A: To add custom code to the <head> section of a theme.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Q: What is the wp_footer action hook used for?

A

A: To add custom code to the footer of a theme.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Q: What is the purpose of the admin_init action hook?

A

A: It is triggered when the WordPress admin dashboard is initialized.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Q: What is the template_redirect action hook?

A

A: It runs before WordPress determines which template to load.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Q: What is the save_post action hook used for?

A

A: To execute custom code when a post is saved.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Q: What is the wp_enqueue_scripts action hook?

A

A: It is used to enqueue styles and scripts for the front end.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Q: What is the admin_enqueue_scripts action hook?

A

A: It is used to enqueue styles and scripts for the admin dashboard.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Q: What is the widgets_init action hook?

A

A: It is used to register custom widget areas.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Q: What is the syntax for add_filter()?

A

add_filter( ‘hook_name’, ‘callback_function’, $priority, $accepted_args );

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Q: What does the the_content filter hook do?

A

A: It allows you to modify the content of a post or page before it is displayed.

18
Q

Q: What is the excerpt_length filter hook used for?

A

A: To change the length of post excerpts.

19
Q

Q: What is the excerpt_more filter hook used for?

A

A: To change the “Read More” text for excerpts.

20
Q

Q: What does the upload_mimes filter hook do?

A

A: It allows you to add or modify the allowed MIME types for file uploads.

21
Q

Q: What is the body_class filter hook used for?

A

A: To add custom classes to the <body> tag.

22
Q

Q: What is the post_class filter hook used for?

A

A: To add custom classes to post containers.

23
Q

Q: What does the login_redirect filter hook do?

A

A: It modifies the URL users are redirected to after logging in.

24
Q

Q: What is the comment_form_defaults filter hook used for?

A

A: To modify the default fields and labels in the comment form.

25
Q

Q: What is the pre_get_posts filter hook?

A

A: It modifies the query before posts are retrieved from the database.

26
Q

Q: What is the default priority for hooks in WordPress?

A

A: 10.

27
Q

Q: How do priorities affect hook execution?

A

A: Hooks with lower priority numbers are executed first.

28
Q

Q: What is the $accepted_args parameter in hooks?

A

A: It defines the number of arguments passed to the callback function.

29
Q

Q: Can you remove a function from a hook?

A

A: Yes, using the remove_action() or remove_filter() functions.

30
Q

Q: How can you check if a function is attached to a hook?

A

A: Using the has_action() or has_filter() functions.

31
Q

Q: How do you create a custom action hook?

A

A: Use the do_action() function.

32
Q

Q: How do you create a custom filter hook?

A

A: Use the apply_filters() function.

33
Q

Q: What is the syntax for do_action()?

A

do_action( ‘hook_name’, $arg1, $arg2, … );

34
Q

Q: What is the syntax for apply_filters()?

A

$value = apply_filters( ‘hook_name’, $value, $arg1, $arg2, … );

35
Q

Q: Why are custom hooks useful?

A

A: They allow developers to extend and customise your plugin or theme without modifying its core code.

36
Q

Q: Why should you prefix your custom hook names?

A

A: To avoid conflicts with other plugins or themes.

37
Q

Q: What is the purpose of documenting your hooks?

A

A: To inform other developers how to use and interact with them.

38
Q

Q: How do you ensure your hooks do not break functionality?

A

A: Validate all inputs and sanitise outputs within hook callbacks.

39
Q

Q: Why is it important to use meaningful names for hooks?

A

A: To make the purpose of the hook clear to developers.

40
Q

A: To make the purpose of the hook clear to developers.

A

A: A hook whose name is generated dynamically using variables or parameters.