Write custom hooks in WordPress


WordPress allow us to create custom hooks. You can write your custom hooks in WordPress as par requirement. In following example you will learn how to write custom hooks in WordPress. Two function are useful to create a new hook , first is add_action() and second is do_action().

Open functions.php and define hook and tie with function

function my_custom_hook(){
do_action('my_custom_hook');
}
function wp_function1(){
echo '<h1>Example Function1</h1>';
echo wp_login_form();
}
add_action('my_custom_hook','wp_function1',1);

function wp_function2(){
echo '<h1>Example Function 2</h1>';
echo get_calendar();
}
add_action('my_custom_hook','wp_function2',2);

Open theme file and write following code to execute the new hook. you can add this anywhere in theme where you want to call the custom hook event.

<?php my_custom_hook();?>
Advertisements

1 thought on “Write custom hooks in WordPress”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s