What’s the difference between add_action and add_filter in WordPress?

  • Update On January 6th, 2017
  • in WordPress

This post was last updated on January 6th, 2017 at 07:16 pm

add_filter( string $tag, callable $function_to_add, int $priority = 10, int $accepted_args = 1 )
“add_filter” is defined as a function that takes in some kind of input, modifies it, and then returns it.—for example, you might want to modify some of your pages blocks or insert another CSS class in a WordPress HTML element.

A simple add_filter() example

Let’s look at an easy example, which will change the url of the link in the logo. Add the following code in function.php file.

// Change url that is linked from logo
add_filter( 'tc_logo_link_url', 'change_site_main_link' );
function change_site_main_link() {
  return 'http://www.mydomainname.com';
}

Add the following in your header file where the logo is.

apply_filters( ‘tc_logo_link_url’, esc_url( home_url( ‘/’ ) ) );

Remember: When you use a filter, you must always return something.

 

add_action( string $tag, callable $function_to_add, int $priority = 10, int $accepted_args = 1 )
“add_action” allow you to add extra functionality at a specific point in the processing of the page. More specifically an action is just a place where you call a function, and you don’t really care what it returns — for example, you might want to add extra widgets or menus, or add a promotional message to your page.

Point to be noted, some actions have arguments too, but again, there’s still no return value. So the concept is, a WordPress action is just a filter without the first argument and without a return value.

How do_action work?

In simple terms, add_action() tells WordPress to do something when it arrives at the specified do_action() hook.

A simple add_action() example

Put the following code in function.php.  Let’s add some text after the header:

// Add some text after the header
add_action( '_after_header' , 'add_promotional_text' );
function add_promotional_text() {
  // If we're not on the home page, do nothing
  if ( !is_front_page() )
    return;
  // Echo the html
  echo "<p>Special offer! Dec only: Free entry for everyone!</p>";
}

add the following Inside your theme core code, at the very last action in header.php:

do_action ( '_after_header' );

This is where our add_action() is hooking itself.

 

Filters – filter things. Actions do not. This is critically important when you’re writing a filter.

Still confused find both of them in details
add_action
add_filter

Are Shortcodes also a filter?

Yes, Shortcodes are a type of filter. They take in content from the shortcode, they return replacement content of some sort. They are filters, by definition. Always, always keep that in mind.

Also store in mind that shortcodes are supposed to return the replacement content, not just echo it out.

About This Author

My name is Parameshwar Roy (P. Roy), and I am a web developer. This is my personal blog to record my own thoughts. Though I am not a natural writer, I love to share my experiences. Hope my experiences will be useful to you...read more about me

Leave A Reply