What use of Hooks in wordpress site

WordPress hooks are arguably the basis of WordPress development, forming a large part of the core functionality and used by almost every plugin and theme available to date.

What exactly *are* WordPress hooks anyways?

WordPress hooks are, essentially, triggers of sorts that allow users to, with short snippets of code, modify areas a WordPress theme or plugin, or add their own code to various parts of WordPress without modifying the original files. An example of this could be along the lines of either “when WordPress chooses which template file to load, run our custom code” or “when you generate the content for each post, add social bookmarking links to the end of the content”. These examples will be expanded upon once we’re a bit more familiar with what exactly the different types of hooks are.
Hooks can be divided into “Action” and “Filter” hooks.

  1. Action Hooks

    Action hooks are designated points in the WordPress core, theme and plugin code where it is possible for outside resources (outside of the scope of where the hook is… either in the core, theme or plugin) to insert additional code and, there by, customise the code to do additional functions they may desire. An example of this is the commonly used wp_head action hook, used by many themes and plugins to inject additional CSS stylesheets, processing code or anything else they require to sit between the <head> and </head> tags of their WordPress theme’s XHTML structure. This is the reason for including wp_head(); in all WordPress themes.
    To hook on to an action, create a function in your theme’s functions.php file (or in your plugin’s code) and hook it on using the add_action() function, as follows:

    	add_action( 'wp_head', 'wpcandy_actionhook_example' );
    	function wpcandy_actionhook_example () {
    		echo '<meta name="description" content="This is the meta description for this page." />' . "\n";
    	} // End wpcandy_actionhook_example()
    The above code adds the text “Hello WPCandy Readers!” between your theme’s <head> tags. Placing “wp_head” in the call to add_action() with “get_header” would display this text above your theme.
  2. Filter Hooks

    Filter hooks are used to manipulate output. An example of this would be to add a line or text (or a hyperlink, or a signature sign-off—whatever you’d like) to the end of the content of each of your blog posts. Filter hooks can also be used for truncating text, changing formatting of content, or just about any other programming manipulation requirement (for example, adding to or overriding an array of values).
    Custom code is added as a filter using the add_filter() function. The following code adds a sign-off to the end of each blog post, only when viewing the full blog post screen:

    	add_filter( 'the_content', 'wpcandy_filterhook_signoff' );
    	function wpcandy_filterhook_signoff ( $content ) {
    		if ( is_single() ) {
    			$content .= '<div>Th-th-th-th-th That\'s all, folks!</div>' . "\n";
    		} // End IF Statement
    		return $content;
    	} // End wpcandy_filterhook_signoff()

    The above code adds a new div tag to the end of the content of our blog post, only when on a single blog post screen.
    A filter hook is like using the str_replace() function in PHP. You give it some data, manipulate, replace or reformat the data and return the new content out at the end.For More Please visit this url ://wpcandy.com/teaches/how-to-use-wordpress-hooks

Function Reference
Filter Functions
  • has_filter()
  • add_filter()
  • apply_filters()
  • current_filter()
  • merge_filters()
  • remove_filter()
  • remove_all_filters()
Actions Functions
  • has_action()
  • add_action()
  • do_action()
  • do_action_ref_array()
  • did_action()
  • remove_action()
  • remove_all_actions()

Leave a Reply

Your email address will not be published. Required fields are marked *