Latest Features

                        How To Use Action Hook In WordPress

                        How To Use Action Hook In Wordpress

                        In WordPress source code, you need to be a master to know how it obtains its content. However, there is another crucial feature to “communicate” with its source code you must know is Action Hook. So How To Use Action Hook In WordPress?

                         

                        1. What is Action Hook?

                        First, let’s see the definition of Action Hook: WordPress action hooks are a means of providing a way for other developers to insert their own code in specific locations within your code, in order to change or expand the functionality of your code.

                        2. How to use an Action Hook

                        To use Action Hook, you have to create a function including PHP script (callback function), then use add_action() function in WordPress to hook your callback into the Action Hook. This is example to show you how to run a hook function into hook init:

                        01
                        02
                        03
                        04
                        function beautheme_theme_setup(){
                            // PHP Script here
                        }
                        add_action( 'init', 'beautheme_theme_setup' )

                        This is not template tag, so you will insert it in fuctions.php in theme or your plugin

                        The structure of add_action() is:

                        01
                        add_action( $hook, $function_to_add, $priority, $accepted_args );
                        • $hook (string) (required) – Hook’s name you need .
                        • $function_to_add (callback) (required) – Function’s name need to be used.
                        • $priority (integer) (optional) – Priority execution of this function with other functions if it has the same hook. The default is 10, the smaller the more it done sooner.
                        • $accepted_args (integer) (optional) – The number of parameters is used in the callback function. The default is 1.

                        3. How to create an Action Hook

                        While WordPress does provide you with a LOT of action hooks that you can take advantage of, you can create your own too, giving your theme or plugin the same benefit of letting other people make changes to your code, without having to edit your code.

                        So, if you want to provide an action hook that will execute any functions that are “hooked” to it, just use this code:

                        <?php do_action('my_action_hook_name'); ?>
                        

                        Of course, change the my_action_hook_name to something unique that describes the location and/or purpose of the action hook.

                        And now, anyone can come along and hook their own function to your newly created action hook.

                        4. Examples about Action Hook in WordPress

                        Now, let’s practise  some popular Action Hook in WordPress. You can consult the Action Hook list in WordPress to know more

                        pre_get_posts

                         

                        This is the hook that you will use if you need to change the parameters of the query at a certain page by your callback function. This hook is called after the WordPress default query is created but before the query is run. This hook is usually used together with objects $query to correct the parameters of the query, or do something to manipulate the default query.

                        For example, you want to open the WordPress hosting page will query retrieves a random article, I will combine the functions of WordPress conditions and modalities $query-> set () in this hook.

                        When you use this hook, the condition functions have to be written in the form of $query. For example: $query->is_home().

                        01
                        02
                        03
                        04
                        05
                        06
                        07
                        08
                        09
                        function beautheme_modify_archive_query( $query ) { // variable declaration to use the callback
                            if( $query->is_archive() && $query->is_main_query() ) : // is_main_query is used to avoid having to intervene in the query is not the default
                                $query->set('orderby', 'rand'); // orderby=rand => arrange random post
                                $query->set('posts_per_page', '1'); // posts_per_page=1 => display 1 post per page
                            endif;
                            
                        }
                        add_action( 'pre_get_posts', 'beautheme_modify_archive_query' );

                        So, you were successful with customizing query in every page without touching the source code, weren’t you?

                         

                        This hook is used if you want to insert something into element <head> of theme without editing theme’s template

                        01
                        02
                        03
                        04
                        function beautheme_author_tag(){
                            echo "<link rel=\"author\" href=\"http://thachpham.com\" />\n";
                        }
                        add_action( 'wp_head', 'beautheme_author_tag' );

                        if you need to insert into footer, hook is wp_footer

                        Now, you know full about Action Hook. And if you any any question, please let me know, I will try my best to answer 🙂