Main Content

How to Use Action Hooks in WordPress

1. do_action, add_action, remove_action, & add_filter

  • do_action() – where the “hooked” functions are run

  • add_action() – attaches a function to a hook as defined by do_action

  • remove_action() – removes a function attached to a specified action hook

So the do_action functions are already added to the theme and will run whenever something is hooked into them via add_action. So you, as the user, will be using the add_action and remove_action functions only to make your modifications.

2. AIOS Starter Theme

List of custom hook

  • action_name: aios_starter_theme_before_inner_page_content
    funciton_name|priority: action_name_action | 11
    filter|args|priority: action_name_filter | $content | 10
    existing_filter: aios_starter_theme_add_breadcrumbs

  • action_name: aios_starter_theme_before_entry
    funciton_name|priority: action_name_action | 11
    filter|args|priority: action_name_filter | $content | 10
    existing_filter: ai_starter_theme_add_post_meta

  • action_name: aios_starter_theme_after_entry_content
    funciton_name|priority: action_name_action | 11
    filter|args|priority: action_name_filter | $content | 10
    existing_filter: ai_starter_theme_add_comments_section

Remember the “do_action” function is going to run any functions that are “hooked” to it via the “add_action” function. For example (looking at the screenshot above) any function that is hooked into “aios_starter_theme_before_inner_page_content” will run before the theme’s header code.

3. How to use hook?

On the example above we will use add_action and add_filter to achieve the same result.

Add extra text using add_action:

We will add text below the default content of inner page content after the breadcrumb.

add_action( 'aios_starter_theme_before_inner_page_content', 'add_text_on_action', 13 );
function add_text_on_action() {
	echo 'This is test for additional content under breadcrumb';
}

We will add text above the default content of before end of inner page content.

add_action( 'aios_starter_theme_after_entry_content', 'add_text_on_action_bottom', 11 );
function add_text_on_action_bottom() {
	echo 'This is test for additional content before end of content';
}
Remember that custom hooks have default priority of 11 which mean if you use add_action you need to define their priority the default is 10, if you add text it will show above the default content or if higher than 11 it will below the default content.

Add extra text using add_filter:

add_filter( 'aios_starter_theme_before_inner_page_content_filter', 'add_text_on_action', 11 );
function add_text_on_action( $content ) {
	return $content . 'This is test for additional content under breadcrumb';
}
add_filter( 'aios_starter_theme_after_entry_content_filter', 'add_text_on_action_bottom', 11 );
function add_text_on_action_bottom( $content ) {
	return 'This is test for additional content before end of content' . $content;
}
Remember the “add_filter” function is going to modify any value that are “hooked” to it. When the filter is later applied, each bound callback is run in order of priority, and given the opportunity to modify a value by returning a new value.

4. Removing an Action & Filter

We hook into “init” so that it runs after the theme has a chance to add it’s own actions. The hook uses class instance: $aios_starter_theme_hook_action

Action

add_action( 'init', 'remove_parent_theme_actions' );
function remove_parent_theme_actions() {
	global $aios_starter_theme_hook_action;
	remove_action( 'aios_starter_theme_before_inner_page_content', array( $aios_starter_theme_hook_action, 'aios_starter_theme_before_inner_page_content_action' ), 11 );
	remove_action( 'aios_starter_theme_before_entry', array( $aios_starter_theme_hook_action, 'aios_starter_theme_before_entry_action' ), 11 );
	remove_action( 'aios_starter_theme_after_entry_content', array( $aios_starter_theme_hook_action, 'aios_starter_theme_after_entry_content_action' ), 11 );
}
(Priorities must match) Interesting thing is that if a priority parameter is used while adding an action, remove_action() call must have the same priority argument in order to remove the action properly. Take a look:

Filter

add_action( 'init', 'remove_parent_theme_actions' );
function remove_parent_theme_actions() {
	global $aios_starter_theme_hook_action;
	remove_filter( 'aios_starter_theme_before_inner_page_content_filter', array( $aios_starter_theme_hook_action, 'aios_starter_theme_add_breadcrumbs' ) );
}