This is a very very bad idea.
You can achieve this by doing something like this:
add_action( 'init', function(){
do_action()//rinse recycle repeat for every hook
});
I highly highly HIGHLY recommend you do not take this route. It can cause a lot of problems of hooks firing when not needed etc.
The idea of a hook is to extend the functionality of the process being processed at the given time, they are supplied data that is only available during certain actions like adding comments.
If you do_action for every single hook, it could be catastrophic. Not to mention you'd not have data for each hook.
If you are looking to fire a function literally every time a page is loaded, then use the init hook, for admin panel only use admin_init
But don't fire every single WordPress action in one hook. It's just not a good idea
Just to cover all grounds
Do:
add_action( 'init', function(){
add_action( 'hook', function(){
// do code here
})
})
This will fire the function on every hook but don't fire every hook on one hook.
Of course you would do a separate add_action for every hook you wanted to fire the function on, but there is no real way to fire hooks.
A little better route to reuse code could be:
add_action( 'init', function(){
$hooks = array(
'hook1',
'hook2',
'hook3'//and so on.
);
foreach( $hooks as $hook ){
add_action( $hook, function(){
// Do code
});
}
// Do the same but in add_filter
});
Again to cover all grounds, this method directly above would add the action / filter but the action / filter would not fire until it is fired with apply_filter or do_action, this simply just adds the function to the action or filter.
Then you'd have to work on accepting the args of the function, without supplying the 4th argument on add_action you are guaranteed only 1 argument so you would have to filter in your code what argument you are work with, with some if or elses.
In response to a comment to elaborate on the arg you could do something similar to:
add_action( 'init', function(){
$hooks = array(
'hook1' => array( 'argname1', 'argname2', 'argname3' ),
'hook2' => array( 'argname1', 'argname2', 'argname3' ),
'hook3' => array( 'argname1', 'argname2', 'argname3' )//and so on.
);
foreach( $hooks as $hook=>$args ){
add_action( $hook, function(){
$numargs = func_num_args();
for( $i=0; $i<$numargs; $i++ ){
$$args[$i] = func_get_arg($i);
}
// Do code
}, 10, count( $args ) );
}
// Do the same but in add_filter
});
Then with the for you'd obviously judge the number of args you need to avoid errors.
To better avoid errors, check the $args[$i] exists or is not empty, before setting a func_get_arg to it.
Response to comment: here's a real time example of how you would use this with save_post action
add_action( 'init', function(){
$hooks = array(
'save_post' => array( 'post_id', 'post', 'update' )
);
foreach( $hooks as $hook=>$arg ){
add_action( $hook, function(){ // Note we dont do any args here
$num_args = func_num_args(); // would return 3 because we supplied 3 args
for( $i=0; $i<$num_args; $i++ ){
if( !empty( $arg[$i] ) ){
$$arg[$i] = func_get_arg( $i );
// We use !empty as it will check if isset AND not empty
// This would set all three array values to the respective variable
// $post_id, $post, $update
// Make sure you put the args in the right order in the hooks array above to avoid mixing up var names.
}
}
if( isset( $post ) ){
update_post_meta( $post->ID, 'meta_key_whatever_you_want', 'meta_value_whateveryouwant' );
// I used $post to show you that the second argumnet supplied by do_action is $post and we set $$arg[1] as $post and retrieved the arg with same key 1 Normally you would just use post_id
}
// Can do with any of the args just be carefull.
}, 10, count( $args ) );
// We count the hook arg array which is the array with simple strings for our variable names and supply it to add_action to let wordpress know we want 3 arguments. If you supply more than 3 arguments you may get errors for this hook, so setting array with 50 args for a hook that only supplies 3 is not good.
// 10 is simply priority in which this hook fires.
}
});
This is just one way you can do this, then you would just do a array key for hook and array for arguments for each hook and check if the variable is set. You may want to do some additional verification on variables to make sure you have the right data as some hooks share variable names, though you can customize, so $post_id can be set to $$arg[0] and in the arg array set as 'post_id_custom' which would be set to $post_id_custom with the hooks $post_id supplied data.
One more thing, to really completely simplify things you can do this too:
function someFunc(){
// do code here
}
add_action('hook1', 'someFunc' );
add_action('hook2', 'someFunc' );
add_action('hook3', 'someFunc' );
add_action('hook4', 'someFunc' );
this will fire the same fun with all of the action hooks supplied.
add_action( 'all', function() {
static $hooks = [];
global $allhooks;
$filter = current_filter();
if ( isset ( $hooks[ $filter ] ) )
return;
$types = array_map( function ( $arg ) {
return $arg;
}, func_get_args() );
$hooks[$filter] = $types;
if( 'shutdown' !== $filter )
return;
foreach( $hooks as $hook=>$args ){
$allhooks[$hook] = $args;
}
});
add_action( 'shutdown', function(){
global $allhooks;
var_dump( $allhooks );
});
This will print all hooks and all args that come with the hooks fyi. Simple modification of toscho's code, it var_dumps something like this for every hook.
array (size=809)
'after_setup_theme' =>
array (size=1)
0 => string 'after_setup_theme' (length=17)
'ot_theme_mode' =>
array (size=2)
0 => string 'ot_theme_mode' (length=13)
1 => boolean false
'ot_child_theme_mode' =>
array (size=2)
0 => string 'ot_child_theme_mode' (length=19)
1 => boolean false
'ot_show_pages' =>
array (size=2)
0 => string 'ot_show_pages' (length=13)
1 => boolean true
This just dumps it to the browser, simply reiterating and helping explain to you what toscho is saying that argument names are not supplied only values.
After review, it's missing hooks like save_post. So even the all hook doesn't supply every action and filter.
There really is no way to accomplish this, and it is really a bad idea.
Again I'm not a WordPress expert.