I am working on a system where I need to send orders to another system using the other system's API, I have a setup that works for 95% of the orders, but a few orders do not trigger my action hook and I can not figure out why a few orders to trigger the action:
add_action( 'woocommerce_order_status_processing', array( $this, 'new_order_processing' ),1 );
Is there another hook I should use to ensure that new_order_processing is triggered on all new orders and why is the hook not always triggered? The customers only have one payment option bank transfer.
(The orders that do not trigger the 'woocommerce_order_status_processing' action do have the status processing when looking inside Woocommerce.)
Related
Right now I am checking the saved post for certain conditions. If the conditions fail the post is set to draft and an admin notice is displayed to let the user know what needs to change.
The timing of the hooks require me to do the same or similar functions twice. Once for each hook. I would like to reduce overhead if possible.
When wp_insert_post_data runs, I check the content and if it doesn't pass the checks, it sets it to draft. When admin_notices runs the post status has already been set and is unchangable. Is there a different hook I should be using? Or is there a better way to do this?
There are many other checks being done in the real code. Here is a basic example of what I'm doing:
add_action( 'admin_notices', 'incorrect_content_notices' );
function notice_of_incorrect_content() {
/**
* Do a bunch of conditional checks. These also get done in another hook.
*/
}
add_filter( 'wp_insert_post_data', 'incorrect_content_set_to_draft', 99 );
function incorrect_content_set_to_draft( $post_data ) {
/**
* Do a bunch of conditional checks that were already done in another hook.
*/
}
I have a custom post type which has some metadata to check in order to be saved or updated. Now I would like to know if it's possible to abort the post creation or update if the check on metadata fails.
To give a simple context and fix the idea, let's say my custom post has a metabox with two date input, let's call them date_start and date_end. When the user try to publish or update the post in the backend I would like to check those metadata and if date_start >= date_end the process aborts without actually update or insert the post into the database. The logic I'm trying to achieve would be something like this:
add_action( 'before_updating_post', check_metadata );
function check_metadata() {
if ( condition ) {
continue post save/update
} else {
abort post save/update // The post will not be saved/updated
}
}
Where 'before_updating_post' is a fantasy name for an hook which allows me to abort the process of updating the post.
I've looked at some questions related to post update hooks but was unable to find some which allows to abort the process being most of them related to data manipulation before saving.
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.
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.
I want to be able to upgrade user's permission after the order status shows complete.
I figured out that I should use hook_order hook in order to achieve that. But how do I get to know which user has created that order and how do go about updating the permissions as well as setting up the expire time for that role automatically.
I want this hook to be called as soon as the payment is made and the order is completed.
Any pointers will be valuable.
In the hook_order, 3 parameters are passed. Third parameter depends on the first one. When the first parameter is 'update', third parameter is the status to which the order is going.
hook_order($op, &$order, $arg2){
switch($op){
case 'update':
if($arg2 === 'completed'){
// This order got marked completed
}
}
}
$order->uid will give you the user that created the order. You can do something like the following
$user = user_load(array('uid' => $order->uid));
// update the roles assigned to user
user_save($user);
For expiring the role, you will need to write a module that will keep track of the duration and will do something like above when the time expires. Or you can use role_expire module and see if that helps.