Excellent Programming Tricks

WordPress Hooks Essentials

Hooks
-- Actually function. Applied to Action or Filter.
-- Generic terms.

API Hooks
1. Filters Hooks
2. Actions Hooks
They are simply functions. To change the default behavior of WordPress, developer uses hooks for modifying the behavior of WordPress.

Theme or Plugins Action/Filter function(hook)
-- Filters/Actions are different.
-- In WordPress action functions are execute when a certain event occur.
-- Filer allows certain functions to get or modify data before it is sent to the database or the browser.
-- Both allow arguments.

Benefits
-- Change WordPress behavior without touch the PHP source file.

Action Hooks (do action)
-- Word Press tells something then action fires or run only.
-- Perform or modify when certain event occur.
♛ Types of action hook
     ✔ add_action()
     ---- Something happen in WrodPress. 
     ✔ do_action()
     ---- Creating own custom action.
-- WordPress actions like wp_head, wp_footer, init and so on.

add_action
-- add_action( string $tag, callable $function_to_add, int $priority = 10, int $accepted_args = 1 ) ;
-- add_action( $identifier, $function_name, [ $priority, $arg_count] );
---- add_action( 'save_post', 'my_save_function' );
------ Function my_save_function runs only when WordPress save a post.

Custom Hooks for Action
-- For create hook: do_action();
-- For callback function: add_action();
---- do_action( $identifier, [$arg1, $arg2, ... ]);
---- do_action( 'comment_form_before_fields' );
------ Inside a function, create a custom hook; when function runs custom hook function also executes.

Filter Hooks (do change)
-- Create hooks. New array element is added in Global Filter array with new callback function name.
-- Argument and value.
-- Apply when modify is required for certain functions behavior.
-- Place modified or new data before it is sent to the database or the browser (echo).
-- WordPress actions like the_content, the_excerpt, post_class, body_class and so on.
--- add_filter()
--- Callback function is fixed for specific hook $tag.
--- add_filter ( 'hook_name', 'your_filter', [priority], [accepted_args] );

Custom Hooks for Filter
-- For create hook: apply_filters();
-- For callback function: add_filter();
-- WordPress recommends any types of text that output on browser font-end must use apply_filters();

Frequently use hooks list
-- Action
---- add_action( 'comment_form_before_fields', 'myFunction' );
---- add_action( 'comment_form_after_fields', 'myFunction' );


--Filter
---- add_filter( 'comment_form_default_fields', 'myFunction');
---- add_filter( 'comment_form_defaults', 'myFunction', 5 );
---- add_filter( 'comment_form', 'myFunction', 1000 );

Difference between Action Vs Filter
-- (Action) Not receiving and modifying data but give a place in WordPress and give runtime execution.
-- (Filter) Receive some piece of data and then, at the end of function, return that data back.
-- (Action) Action function's sometime get some data or not.
-- (Filter ) Filter function's always get some data.
 

2 comments


Authentic аnd Excellent

Website

HTML Template

Wordpress Theme

Database applications

OR

Application services?

Excellent Programming Tricks (EPT) || Iftekhar-IT || We develops the Web applications and the WordPress templates. || Excellent Programming Tricks (EPT)

© 2020 Blogger Theme by Iftekhar IT || Excellent Programming Tricks

Execllent Programming Tricks. Powered by Blogger.