If you are using WordPress for your blog or starting with WordPress development, this article is going help you. As you already know WordPress is the most popular CMS platform in the world empowering more than 28% sites of the web. There is something unique that WordPress done that it overshadowed other competitors in the market. And to make limitless customisations on it, is the main part of its success.
In this article I’ll discuss about WordPress development with action & filter hooks. What is this hooks & what is the difference between action hooks & filter hooks?
So let’s get started.
WordPress started as a blogging platform, but very soon it became popular & powerful as fully featured CMS. At its core WordPress provides coding stability and provides hooks to put custom PHP almost every corner of WordPress.
These hooks are great for customisations and give us developers the power to do whatever we want from it. By hooks WordPress gives us a way to put our custom PHP functions within WordPress to achieve any certain task.
Hooks are mainly divided into 2 categories.
- Action Hooks
- Filter Hooks
Action Hooks
Action hooks are WordPress defined way to execute our own set of codes during any particular event or action in our WordPress site. By this we can add, modify or delete any functionality during that particular action or from the whole site itself.
By using action hooks (see the WordPress codex reference for hooks), plugin or theme developers can do things like adding html meta tags to the head of a page, adding js files to footer, adding widget, register custom post types etc. Action hooks are mainly used for custom development. For plugin development this action hook is very important to know, however knowledge of it requires for theme development also.
add_action( ‘action_hook’, ‘custom_function_name’ );
In this function above, first parameter is the name of an action hook defined by WordPress (we can also define our own actions hooks) and the second one is the name of our custom PHP function.
Here is an example
add_action( ‘wp_head’, ‘custom_html_meta_function’ ); function custom_html_meta_function() { echo ‘<meta name="keywords" content=“WordPress, PHP, HTML,CSS,XML,JavaScript”>’; }
By this code I’m adding keywords meta tags to my WordPress theme. So here wp_head is the action to manipulate the code to head section of HTML page of the site.
add_action( ‘publish_post’, ‘send_mail_to_admin’ ); function send_mail_to_admin() { wp_mail( ‘admin@email.com’, ‘New Post Published’, ‘A New Post Has Been Published’ ); }
Here we are sending an email to admin after publishing post. So here publish_posts action hook is particularly hooking our custom function into the post publishing event.
add_action( ‘init’, ‘register_testimonial_post_type’ ); public function register_testimonial_post_type() { $args = array( 'labels' => array( 'name' => _x( 'Testimonials', 'testimonial' ), 'singular_name' => _x( 'Testimonial', 'testimonial' ), 'menu_name' => _x( 'Testimonials', 'testimonial' ), 'name_admin_bar' => _x( 'Testimonial', 'testimonial' ), 'add_new' => _x( 'Add New', 'testimonial' ), 'add_new_item' => __( 'Add New Testimonial', 'testimonial' ), 'new_item' => __( 'New Testimonial', 'testimonial' ), 'edit_item' => __( 'Edit Testimonial', 'testimonial' ), 'view_item' => __( 'View Testimonial', 'testimonial' ), 'all_items' => __( 'All Testimonials', 'testimonial' ), 'search_items' => __( 'Search Testimonials', 'testimonial' ), 'parent_item_colon' => __( 'Parent Testimonials:', 'testimonial' ), 'not_found' => __( 'No Testimonials found.', 'testimonial' ), 'not_found_in_trash' => __( 'No Testimonials found in Trash.', 'testimonial' ) ), 'query_var' => 'testimonials', 'rewrite' => array( 'slug' => 'testimonials', 'with_front' => false ), 'public' => true, 'menu_position' => 5, 'menu_icon' => plugin_dir_url(__FILE__) . '../images/testimonial-icon.png', 'supports' => array( 'title', 'editor', 'thumbnail' ), ); flush_rewrite_rules(); register_post_type('testimonials', $args); }
In this example by using init action hook we registered a new custom post type to our WordPress install. As you can see here init is not working for any particular area or event, it is adding a new functionality to the site.
Filter Hooks
Filter hooks are another type of WP hooks which works with content and output manipulation. Filter hooks basically filter the data before presenting to the users. While developing themes, developers use it to manipulate the content while presenting it to visitors.
Suppose our post title looks like this
This is the post title
And we want it to be like this
This Is The Post Title
This can be achieved by the filter hooks, and by using this we are not changing the actual data, we are just presenting it in our way.
add_filter( ‘the_title’, ‘my_custom_filter_function’ ); function my_custom_filter_function( $title ) { return ucwords( $title ); }
Here the_title filter hook passing the title of the post content argument to the function and within the function we use ucwords PHP function to modify the title.
As we can modify the title of a post, we can also make changes in post content by using the filter hook.
add_filter( ‘the_content’, ‘my_custom_content_filter_function’ ); function my_custom_content_filter_function( $content ) { return $content . ‘<a href=“#”>Social Share Button</a>’; }
Here we have added a social share button at the end of the content of the post by filter hook.
So by these examples and discussions I believe you get a basic overview of action hooks and filter hooks. Let’s try these codes by yourself now.