wordpress: how do actions get executed? - wordpress

Do custom hooks that hook onto wordpress core get executed automatically by wordpress or do we have to call the do_action function on the custom made hook ourselves?
Example:
add_action("action_one","some_process",10);
add_action("action_one","some_different_process",11);
function some_process(){ //... }
function some_different_process(){ //... }
do_action("action_one");
Does some_process get executed first and then some_different_process gets executed after when do_action("action_one") is called?

If you create custom action hooks that exist within your custom theme or plugin, then yes, you have to call them via do_action in the appropriate location where you would want them to be triggered.
WordPress does not automatically find action hooks and trigger them. It has a number of built-in hooks which you can latch onto in order to run custom code, but if you create your own hooks, then you need to also set up the appropriate do_action call in order to run them.
To answer your other question regarding the execution order of some_process and some_different_process, some_process will be run before some_different_process because it had a lower priority number.
If you want an action to run early, give it a low number like 1 or 5, if you don't care where it runs or want it to run last, I usually use a much higher number like 100 or 1000. If two hooks are defined with the same priority, usually the one that got registered first will run before the other one with the same priority.

All functions that are hooked onto an action are automatically executed IF that action is called, but they are not called if the action is not triggered.
For example if you have:
add_action("action_one","some_function",10);
Then some_function will be called if action_one is triggered. If action_one is never triggered, some_function is not called.
do_action is a mechanism to manually trigger the action, though keep in mind it will trigger ANY hooks into that action, not just yours (unless you setup some filters).
Another example: let's say you setup a custom function to run on the action save_post:
add_action( 'save_post', 'top_secret_function' );
Then every time you a save a post your top_secret_function will run.
If you want to trigger save_post manually (without actually saving a post) you can do so with:
do_action( 'save_post' );
and it will trigger your top_secret_function, but this would generally not be recommended because it will also trigger any other function that is hooked into save_post (which many plugins and core files do).
You can setup custom actions using a combination of add_action and do_action.

Related

The difference between do_action() and add_action() in Wordpress

After reading multiple online posts about do_action()/add_action(), I would like to brief my understanding about them.
Here I would only talk about action hooks, not about the filters.
do_action() : Registers an action hook
add_action() : adds a callback function to the registered hook.
Now the questions are :
1> About add_action()/do_action() for existing hook :
add_action( 'admin_enqueue_scripts', 'my_func');
In the above example the callback function my_func() is registered with the hook 'admin_enqueue_scripts'. Now whenever do_action('admin_enqueue_scripts') is called somewhere inside the code in future, the callback function my_func() would be executed. So in brief, the callback function won't be executed unless do_action() is called. Is my understanding correct ?
2> About add_action()/do_action() for custom hook:
Now consider a situation I am writing a new plugin and I want to provide an action hook inside it. In this case how these two functions would be used ?
// inside my plugin code
do_action('my_custom_hook');
// inside the user code who wants to use this hook
function user_func()
{
// code ...
}
add_action('my_custom_hook', 'user_func');
Is my understanding correct ?

worpdress: how to call a function when a scheduled post is published?

I am using wordpress.
I need to run a function (send a email) when a scheduled post is automatically posted.
What hook/function should I use ?
Use post status transition hooks
function scheduled_post_published($object)
{
// whatever it is you need to do
}
add_action('future_to_publish', 'scheduled_post_published');
I think you need to use the hook transition_post_status, from the documentation:
transition_post_status is a generic action that is called every time a post changes status.
And on https://wordpress.stackexchange.com/a/100657 I found piece of code that can you use as inspiration.

WordPress delete_post hook calls function twice

Here is how I've implemented it:
add_action( 'delete_post', array( 'MyClassName', 'delete' ) );
In the delete function I was using a $wpdb->insert command to see whether the function gets called. But I see that the $wpdb->insert command is called twice (inserts two rows in my table). Any ideas as to why something like this might happen?
I've also attempted to use the before_delete_post hook since it doesn't really make a difference to me but I get the same outcome.
EDIT:
It seems as though the delete function is called for each entry in the wp_posts table for some reason. So if the post has 3 revisions, the delete function will be called 4 times (the original post + 3 revisions). This is really weird behavior.
I found my solution.
The function you hook into delete_post (or probably any other similar hook) executes as many times as needed. Considering delete_post needs to delete the post and all of its revisions, it will always run more than once. In order to avoid having your function execute each time WordPress deletes a record from the database you can use did_action( $hook ). This function returns the number of times the hook executed. With this in consideration, we can fix our multiple-executions problem by placing this condition into our function:
if (did_action('delete_post') === 1)
{
// execute code here
}

add_action,add_filter,user_can ect in wordpress

I have developed wordpress for days,but I have found no way to these like:add_action,add_filter,user_can, I don't know what are the functions they refere to.
worse still,I don't know the parameter,today I want add a column to user list table admin panel,I fortunatelly found a tutorial, here is the code
add_filter( 'manage_users_columns', 'add_user_column');
function add_user_column( $columns){
$columns['available_stickers'] = __('Stickers Available', 'available_stickers');
return $columns;
}
add_filter('manage_users_custom_column', 'add_user_column_value', 10, 3);
function add_user_column_value( $value, $column_name, $user_id ){
if ( 'available_sticker' == $column_name)
$value = get_user_meta($user_id,"available_stickers",true);
return $value;
}
Even thought I made it, but I don't know where the parameter manage_users_columns comes or why I should use manage_users_columns but not other code? Puzzled
and also they have matched code like apply_filter etc.
some one can help me out of the maze,oops!
WordPress is beautifully designed because most of the actions it does are not executed directly, but through what are called actions and filters. This gives you, the developer, a possibility to hook onto any of these operations. Hooking means you can squeeze your own logic right in the middle of WP's logic, in a very clean way, only by declaring that you want things to be done where the corresponding hooks are used. More precisely:
Actions
So, for example, when a post is saved, WordPress does not just save the post, it does it by executing this line:
do_action( 'save_post', $post_ID, $post );
This is a way to give a name to this action (save_post). That means two things :
1) you can execute the exact same action by using the same line of code somewhere else.
2) you can add your own logic to be executed during this action. How? just by "adding" your custom functions to the action, like this :
add_action('save_post', 'name_of_the_function_you_want_to_execute_when_post_is_saved');
So 'save_post' is the name of the hook, add_action gives you the possibility to add your own function to the action for this hook and do_action actually executes the action.
Filters
Filters are similar to actions, but instead of being used when executing a command, they are used to treat a value, an object, a string or some text (when "filtering" it). Again, instead of just manipulating objects and strings in such a way that you would have no other possibility than dive into the core code to access it, WordPress cleverly does many of its "filtering" operations with a special action called apply_filters(). This gives you the possibility, like for actions, to actually add your own filters to the filters already applied. So when showing the content of a post, WP would execute :
apply_filters('the_content', $post->post_content);
The name of the hook is the_content. If you use :
add_filter('the_content', 'your_function_to_execute_to_further_filter_content');
you can have a function named your_function_to_execute_to_further_filter_content()that can take the content as a parameter and return the filtered version of it after whatever treatment you wish to apply to it. Then this treatment will get applied anywhere in WP where the hook the_content is used to execute these filters (including WP default ones, plug-ins filters that were added to the same hook, and your own).
So here, you are using these hooks so you can interfere with WP operations without messing WP core code. You declare the extra operations you wish to execute and you let WP aware of this. WP will then have the courtesy to execute your functions everytime the corresponding hook action or filter is being executed.

Drupal hook_cron execution order

What order does Drupal execute it's _cron hooks? It is important for a certain custom module I am developing and can't seem to find any documentation on it on the web. Maybe I'm searching for the wrong thing!
Drupal executes all of its hooks in the order based off of module weight. Module weight defaults to 0, and the secondary ordering is alphabetical by module name:
http://api.drupal.org/api/function/module_list/6
You can inspect and adjust the cron execution orders with the Supercron module. Some more details about this module (from its project page):
SuperCron is a complete replacement for Drupal's built-in Cron functionality. It allows you to:
See the list of all Cron hooks found in the enabled modules
Change the order in which cron hooks are called
Disable certain hooks
Run the tasks you choose in parallel, so that cron tasks will be executed all at once rather than one after the other
Identify the exceptions raised by individual hooks
Call hooks individually on demand (great for identifying problems)
Keep executing cron hooks that follow an exception, limiting the damage to only one module
Measure the time it takes for a cron hook to execute (we display the last call timings and the average timings)
Capture any output generated by the hooks
Change the way Cron behaves when the site is under load (this optional feature requires Throttle to be enabled)
Limit the IP addresses that may be allowed to call your cron scripts
For Drupal 8 you have to rearrange modules' implementation order in hook_module_implements_alter:
function YOUR_MODULE_module_implements_alter(&$implementations, $hook) {
// Move our hook_cron() implementation to the end of the list.
if ($hook == 'cron') {
$group = $implementations['YOUR_MODULE'];
unset($implementations['YOUR_MODULE']);
$implementations['YOUR_MODULE'] = $group;
}
}
If you'd like to call your hook_cron first:
function YOUR_MODULE_module_implements_alter(&$implementations, $hook) {
// Move our hook_cron() implementation to the top of the list.
if ($hook == 'cron') {
$group = $implementations['YOUR_MODULE'];
$implementations = [
'YOUR_MODULE' => $group,
] + $implementations;
}
}
Hooks execution is determinate from the weight of the module implementing them; the weightier module will be executed for last.

Resources