wordpress Hooks

Hooks are used to add some additional functionality in wordpress. we can edit wordpress features by editing those hooks.Some of hooks with description and usage.

USually we used to write the hooks in function.php as function,

eg: function hookfun(){

}

add_action(‘hookname’,’ourfunction ( in this case hookfun)’);

 

Hooks:

  • wp_loaded

if you want to call a function only after wordpress and plugins loaded call this hook.

add_action(‘wp_loaded’, myfun);

function myfun(){

// codes here;

}

  • INIT HOOK:hooks used to add some functionality after wordpress loaded, but before headers sent.

Eg:-

To add custom taxonomies:

add_action( ‘init’, ‘custom_taxonomy_item’ );

To add custom post type

add_action( ‘init’, ‘create_posttype’ );

  • add_meta_boxes

Adding a meta boxes to wordpress posts, or pages or post type.

add_action( ‘add_meta_boxes’, ‘shorcodeslider’ );
function shorcodeslider() {
add_meta_box( ‘custom-srt-id’, ‘Slider shortcode’, ‘shorcodeslider2’, ‘post’, ‘normal’, ‘high’ ); // change post type here
}

Then define what need to integrate inside “shorcodeslider2” function.

  • save_post

Execute after post published, and updated. So if you need to do any action while publish/updating a post use this hook

add_action( ‘save_post’, ‘fun’ );
function fun( $post_id ) {
$title=”Title: “.get_the_title($post_id);
$title.=”Content: “.get_the_content($post_id);
$title.=”URL: “.get_permalink($post_id);
wp_mail(“sample@sample.com”,”Post updated”,$title);
}

  • publish_post

Execute after post published

add_action( ‘publish’, ‘fun’ );
function fun( $post_id ) {
$title=”Title: “.get_the_title($post_id);
$title.=”Content: “.get_the_content($post_id);
$title.=”URL: “.get_permalink($post_id);
wp_mail(“sample@sample.com”,”Post updated”,$title);
}