WordPress Hooks

Basic Format

We create a function which contains a call to a hook which is then added as an action for WP to execute.

[php]
function example_function() {
// Call hook w/parameters.
}

add_action( $hook_name, $function_name, $priority, $arguments );
[/php]
  • $hook_name is the name of the hook we want to use.
  • $function_name is the name of our custom function.
  • $priority
  • $arguments is the number of arguments used by our custom function.
  • remove_action() and remove_all_actions() can be used to remove actions from a hook. See Unvers for details.
  • If we want to add functions to our plugin or theme we use do_action() or do_action_ref_array(). Again, see Unvers.
  • Checking to See if a Hook Has Any Actions Associated
[php]
if( has_action( 'some_action' ) {
add_action( 'some_action', 'my_custom_action' );
}
[/php]

Action Hooks

  • Occur in response to a specific event (such as publishing a post, changing a theme).
  • add_meta_boxes – Runs when add_meta_box() is executed.
  • after_setup_theme – Runs on every page load where a theme is loaded.
  • after_switch_theme – Runs after a theme is switched.
  • wp_default_scripts – Allows one to interact with default scripts WordPress includes.
  • draft_to_publish – When post status changes.
  • do_feed
  • get_footer
  • wp_footer – Runs when wp_footer() function is called.
  • get_header – Triggers on get_header().
  • wp_head – Triggered off function of the same name.
  • init – Occurs during WordPress initialization (aka, very early).
  • wp_list_categories – Triggers from function of same name; operates on categories widget.
  • login_enqueue_scripts
  • login_form – Runs when the login_form is loaded.
  • login_head – Edit header for login page.
  • wp_logout – Called when a user logs out.
  • manage_media_custom_column
  • manage_posts_custom_column
  • manage_users_custom_column
  • new_to_future – When post status changes.
  • plugins_loaded – This is where plugins should be initialized.
  • pre_get_posts – Runs when get_posts() is triggered.
    • Can be used to include Custom Post Types (CPTs) in search results.
  • pre_get_search_form – Executes before get_search_form.
  • publish_to_private – When post status changes.
  • save_post – Runs when a post is being saved.
  • send_headers – Allows for sending custom HTTP headers.
  • show_admin_bar – Allows the admin_bar to be shown under any desired circumstance – even when visitor is not logged in.
  • template_redirect – Fired when WP decides what page template to load.
  • transition_comment_status
  • transition_post_status – Whenever post status changes.
  • widgets_init – Runs when a widget is initialized.
  • wp – Runs after the query has been executed and content has been retrieved, but before generation of templates.
  • wp_authenticate – Runs when a user is logging in.
  • wp_default_styles
  • wp_enqueue_scripts – To add scripts to the page.
  • wp_enqueue_style – To add styles to the page.
  • wp_meta – Modify the meta widget.

Administrative

  • activated_plugin – Fires whenever a plugin is activated.
  • activity_box_end – Modify the Dashboard “At a Glance” widget.
  • wp_dashboard_setup
  • admin_enqueue_scripts
  • admin_footer
  • admin_head – Add code to header in administrative background.
    • admin_head-page.php – Where page is the name of the admin page you want to make a change to.
  • admin_init – Runs every time an admin page is loaded.
  • admin_menu – Add/remove items in the admin menu.
  • admin_notices – Displayed in the header of the admin pages.
  • wp_before_admin_bar_render – Before the bar at the top of admin pages renders.
  • delete_user
  • profile_update – Triggers right after user profile data has been updated in db.
  • set_current_user – Called during wp_set_current_user().

Action Functions

  • do_action()
  • do_action_ref_array()
  • add_action() – Tells WP where to call custom code.
  • has_action()
  • did_action()
  • remove_action() – Disables a core WordPress action or one created by a plugin.
  • remove_all_actions()

Filter Functions

  • Occur when data is being passed between the browser and DB or vice versa.
  • has_filter() – Is any filter already registered with hook?
  • add_filter()
  • apply_filters()
  • apply_filters_ref_array()
  • current_filter()
  • remove_filter()
  • remove_all_filters()

Bibliography