Hooks, Actions and Filters
Hooks are provided by WordPress to allow your plugin to 'hook into' the rest of WordPress; that is, to call functions in your plugin at specific times, and thereby set your plugin in motion. There are two kinds of hooks:
You can see many WordPress action hooks when you are developing a plugin for WordPress, but some WordPress hooks are used more than other hooks. So for developing a plugin, you should have some knowledge of these hooks
Hooks are provided by WordPress to allow your plugin to 'hook into' the rest of WordPress; that is, to call functions in your plugin at specific times, and thereby set your plugin in motion. There are two kinds of hooks:
- Actions
- Filters
You can see many WordPress action hooks when you are developing a plugin for WordPress, but some WordPress hooks are used more than other hooks. So for developing a plugin, you should have some knowledge of these hooks
plugins_loaded
This one is the most important hook in any plugin development process.
plugins_loaded is fired after all the WordPress files are loaded on the
page but before the pluggable functions, then WordPress starts executing
everything. So now you can understand, no code snippet will run until
this hook is fired
<?php
add_action('plugins_loaded', 'display_my_msg');
function display_my_msg() {
add_action('wp_footer', 'display_message', 100);
}
function display_message() {
echo 'This is dummy Text';
}
?>
init
This hook is fired after everything is set up in WordPress. WordPress
also tries to add a plenty of things, for example, registration of
taxanomies, initialization of the default widgets and post types. You
can say that everything is ready when it will come to this hook, so your
plugin whatever you are developing will perhaps use init hook for
anything it needs to do, when entire information of WordPress is
available to use.
<?php
add_action('init', 'excerpts_to_page_type');
function excerpts_to_page_type() {
add_post_type_support('page', array('excerpt'));
}
?>
wp_head
You can see the difference with and without using wp_head in your template. Without this hook you will not see anything besides your own written code, but with this hook you will see extra files like javascript, css etc after your written code in the head section.
<?php
add_action('wp_head', 'tr_add_meta_desc');
function tr_add_meta_desc() {
//Getting the blog description
$desc = esc_attr(get_bloginfo('description'));
}
//Check meta description is set or not
if(!empty($desc)) {
echo '<meta name="description" content="' . $desc . '" />';
}
?>
template_redirect
The importance of this hook is that at this point WordPress knows which
page or post a visitor is viewing. It is just executed before the theme
is chosen for the particular page view. template_redirect doesn’t fire
in the admin area, it just fired only on the front side. The hook is
very useful if you want to load a specific piece of code only for
certain page views.
<?php
add_action('template_redirect', 'add_my_css_file');
function add_my_css_file() {
if(is_singular('post')) {
wp_enque_style('add_css', 'my_custom.css', false, 0.1, 'screen');
}
}
?>
admin_menu
admin_menu hook is just for admin pages when page loads. If your plugin
doesn’t need to execute in the front end, you can use this hook because
whenever your plugin works directly in the admin side, you would
absolutely use admin_menu to run your code.
<?php
add_action('admin_menu' , 'my_plugin_page');
function my_plugin_page() {
add_options_page('My Page', 'My Page', 'manage_options', 'my_plugin', 'my_plugin_page");
}
?>
Labels:
tutorials,
wordpress hooks
Previous Article

Responses
0 Respones to "Frequently Used WordPress Action Hooks For Plugin Development"
Post a Comment