Hooks Flashcards
What are the two types of hooks and how are they different?
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.
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.
<?php </p>
add_action( ‘wp_footer’, ‘footer_message’, 100 );
function footer_message() {
echo ‘This site is built using WordPres’;
}
?>
How do you manually execute all actions registered to ‘wp_head’ action hook?
<?php </p>
do_action( ‘wp_head’ );
?>
Register an action on ‘pre_get_posts’ to randomly order posts on the blog home page rather than the default ordering by post date.
<?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’ );
}
}
?>
What hook should WP plugins do their setup on?
‘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.
Write action to add the ability for users to write an excerpt for pages.
// 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’ ) );
}
?>
Add a sub-menu item labeled BOJ Settings to the Settings menu in the WordPress admin.
<?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’
);
}
?>
Which hook fires at the point where WP knows which page a user is viewing?
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.
Write action to load a style sheet file ONLY for a singular post view.
<?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’
);
}
}
?>
Write action to add the WP site description as a meta tag(description) in the header.
.
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>’;
}
}
?>
Which hooks do you use to add inline JS and to add JS files to the head?
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.
Write filter function that filters the output of $title by appending the site’s name to the end of page title.
<?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 do you remove filters from a hook?
<?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 do you remove actions from a hook?
<?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 do you check if a hook has any filters?
<?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.