Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is add_action( 'init [closed]

Why do we use this type of thing in wordpress? Can anyone explain it to me please? Why do we use init in wordpress functions? Or, what is init?

like image 747
Nadeem Akram Avatar asked Jan 18 '14 07:01

Nadeem Akram


People also ask

What is INIT in Add_action?

INIT HOOK: Runs after WordPress has finished loading but before any headers are sent. Useful for intercepting $_GET or $_POST triggers. For example, to act on $_POST data: add_action('init', 'process_post'); function process_post(){ if(isset($_POST['unique_hidden_field'])) { // process $_POST data here } }

What is WP init?

WordPress includes a built-in action called init that fires after WordPress has finished loading and authenticated the user, but before any headers are sent.

What is the difference between Add_action and Add_filter in WordPress?

add_action is what you use to create a trigger "hook" - when something happens, do-something-else. add_filter is used to "hook" data change/replace - where there is [some-code], change it to some-other-expanded-code. add_action allow us to bind a a function to one or many WordPress event.

Can I pass arguments to my function through Add_action?

It is possible to pass additional arguments directly to the hook that you are calling the additional function on. The $accepted_args parameter will allow you to define the number of parameters accepted from the action hook to the function being called in the add_action function.


2 Answers

Add action is used instead of hard-coding a function into WordPress. The benefit to using add_action is that you allow core wordpress functions to keep track of what has been added, and by doing so, can override previously added functions by de-registering them later on.

For example:

You download a plugin with a defined action/method named

add_action( 'init', 'crappy_method' );

You need to override the crappy function with your own:

remove_action('init', 'crappy_method' );
add_action( 'init', 'my_even_crappier_method' );

By doing this you can copy the original method and customize it without changing the original files. This is very useful with plugins so that you can update them later on without losing your changes.

like image 51
Chizzle Avatar answered Oct 03 '22 21:10

Chizzle


USAGE: add_action( $hook, $function_to_add, $priority, $accepted_args );

Parameter: $hook (string) (required) The name of the action to which $function_to_add is hooked. Can also be the name of an action inside a theme or plugin file, or the special tag "all", in which case the function will be called for all hooks) Default: None

INIT HOOK: Runs after WordPress has finished loading but before any headers are sent. Useful for intercepting $_GET or $_POST triggers.

For example, to act on $_POST data:

add_action('init', 'process_post');
function process_post(){
if(isset($_POST['unique_hidden_field'])) {
 // process $_POST data here
}
}
like image 45
DDphp Avatar answered Oct 03 '22 19:10

DDphp