Hooks Flashcards

1
Q

What are the two types of hooks and how are they different?

A

Action hooks and filter hooks. The former enables you to execute a function at a certain point, and the latter enables you to manipulate the output passed through the hook.

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

Register an action for the wp_footer hook that adds a custom message to the footer - ‘This site is built using Wordpress’ with a priority of 100.

A

<?php </p>

add_action( ‘wp_footer’, ‘footer_message’, 100 );

function footer_message() {

echo ‘This site is built using WordPres’;

}

?>

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

How do you manually execute all actions registered to ‘wp_head’ action hook?

A

<?php </p>

do_action( ‘wp_head’ );

?>

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

Register an action on ‘pre_get_posts’ to randomly order posts on the blog home page rather than the default ordering by post date.

A

<?php </p>

add_action( ‘pre_get_posts’, ‘randomly_order_blog_posts’ );

function randomly_order_blog_posts( $query ) {

if ( $query->is_home && empty( $query->query_vars[‘suppress_filters’] ) ){

$query->set( ‘orderby’, ‘rand’ );

}

}

?>

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

What hook should WP plugins do their setup on?

A

‘plugins_loaded’ - it is executed when all the user’s activated plugins have been loaded by WordPress. It is also the earliest hook plugin developers can use in the loading process. Other actions should also be added within the callback function used on this hook.

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

Write action to add the ability for users to write an excerpt for pages.

A

// Do this on ‘init’ because the “page” post type is created at this point

<?php </p>

add_action( ‘init’, ‘add_excerpts_to_pages’ );

function add_excerpts_to_pages() {

add_post_type_support( ‘page’, array( ‘excerpt’ ) );

}

?>

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

Add a sub-menu item labeled BOJ Settings to the Settings menu in the WordPress admin.

A

<?php </p>

add_action( ‘admin_menu’, ‘boj_admin_settings_page’ );

function boj_admin_settings_page() {

add_options_page(

‘BOJ Settings’,

‘BOJ Settings’,

‘manage_options’,

‘boj_admin_settings’,

‘boj_admin_settings_page’

);

}

?>

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

Which hook fires at the point where WP knows which page a user is viewing?

A

template_redirect

It is executed just before the theme template is chosen for the particular page view. It is fired only on the front end of the site and not in the administration area. This is a good hook to use when you need to load code only for specific page views.

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

Write action to load a style sheet file ONLY for a singular post view.

A

<?php </p>

add_action( ‘template_redirect’, ‘boj_singular_post_css’ );

function boj_singular_post_css() {

if ( is_singular( ‘post’ ) ) {

wp_enqueue_style(

‘boj-singular-post’,

‘boj-example.css’,

false,

0.1,

‘screen’

);

}

}

?>

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

Write action to add the WP site description as a meta tag(description) in the header.

.

A

add_action( ‘wp_head’, ‘boj_front_page_meta_description’ );

function boj_front_page_meta_description() {

$description = esc_attr( get_bloginfo( ‘description’ ) );

if ( !empty( $description ) ){

echo ‘<meta></meta>’;

}

}

?>

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

Which hooks do you use to add inline JS and to add JS files to the head?

A

wp_enqueue_script() - JS files

wp_head() - inline JS

Many plugins incorrectly use the wp_head action hook to add JavaScript to the header when they should be using the wp_enqueue_script() function. The only time JavaScript should be added to this hook is when it’s not located in a separate JavaScript file.

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

Write filter function that filters the output of $title by appending the site’s name to the end of page title.

A

<?php </p>

add_filter( ‘wp_title’, ‘boj_add_site_name_to_title’, 10, 2 );

function boj_add_site_name_to_title( $title, $sep ) {

$name = get_bloginfo( ‘name’ );

$title .= $sep . ‘ ‘ . $name;

return $title;

}

?>

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

How do you remove filters from a hook?

A

<?php </p>

remove_filter( $tag, $function_to_remove, $priority, $accepted_args );

?>

NOTE: To successfully remove a filter, this function must be called after a filter has been registered using the add_filter() function.

The function returns true when the filter is successfully removed and returns false when the removal is unsuccessful. The $tag, $function_to_remove, and $priorityparameters must also match the parameters set with add_filter() exactly. Otherwise, the filter will not be removed.

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

How do you remove actions from a hook?

A

<?php </p>

remove_action( $tag, $function_to_remove, $priority, $accepted_args );

?>

NOTE: If your code runs before the action is registered, the action will not be removed from the hook.The function returns true when the action was successfully removed and false when the action could not be removed.

To successfully remove an action from a hook, the $tag, $function_to_remove, and $priority parameters must exactly match the parameters used in do_action(). Otherwise, the action will not be removed and remove_action() will return false.

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

How do you check if a hook has any filters?

A

<?php </p>

has_filter( $tag, $function_to_check );

?>

NOTE: The function returns false if no filter is found for the given hook. It returns true if a filter is found. However, if the $function_to_check parameter is set, it returns the priority of the filter.

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

How do you check if a hook has actions registered?

A

<?php </p>

has_action( $tag, $function_to_check );

?>

NOTE: The return value for has_action() varies between a Boolean value and an integer. If $function_to_check is not set, the function returns true if actions are added to the hook or false if no actions are added to the hook. If $function_to_check is set and the function has been added to the hook, the priority (integer) of the action will be returned. Otherwise, a value of false will be returned.

17
Q

How do you check if an action hook has already been executed or count the number of times one has been executed?

A

<?php </p>

did_action( $tag );

?>

NOTE: The function returns the number of times the hook has been fired or false if it hasn’t been fired. The most common use case of the function is to check if an action hook has already been fired and execute code based on the return value of did_action().

18
Q

How do you add a method(of a class, rather than a function) to a hook?

A

As an array:

<?php </p>

add_action( $tag, array( &$this, $method_to_add ) );

?>

19
Q

What are the available functions for creating custom action hooks?

A

do_action()

do_action_ref_array()

apply_filters()

apply_filters_ref_array()

20
Q

What type of action hook is ‘load-$pagenow’ and how is it used?

A

It is a variable hook - variable hooks are able to change based on context. The $pagenow variable changes depending on the WordPress admin page currently viewed.

do_action( “load-$pagenow” );

will become the page name being viewed. For example, the hook for the new post page in the admin would be load-post-new.php and the hook on the edit posts screen would be load-post.php. This enables plugins to run code only for specific page views in the admin.

21
Q
A
22
Q
A
23
Q
A