user_register hook doesn't fire - wordpress

I have designed a plugin and in a part of its functionalities I'm trying to save some data to a custom table in MySQL based on the ID of new registered user in Wordpress registration form. I used user_register hook as follow:
function my_func($user_id){
// some DB Query code based on $user_id
}
add_action('user_register', 'my_func', 10, 1);
I already tried solution in Wordpress user_register hook not executing?
and used profile_update hook as described there
but it couldn't help. I also tried changing priority value and removed it but the problem still exists.
The only thing I want is to have access to ID of the new registered user in registration form.
I appreciate if someone could help me.

I finally could find the solution. Actually in the body of myfunc function in the question I was trying to output the $user_id using javascript alert and for some reason that script doesn't run at that point but my query runs successfully and the way that I used the user_register hook is correct.
I just want to share it in case anybody encountered such a problem.
Thanks.

Related

How to POST to admin-post.php from Avada formbuilder form

Using the Avada theme's form builder functionality, I’m attempting to use the “Send to URL” option to POST the form and then run an api call in a plugin I’ve written.
I was thinking I could set the “Send to URL” value to /wp-admin/admin-post.php and add a hidden field to the form named “action” with a value of “make_api_call” to get it to run the code in my plugin set up like so:
add_action('admin_post_make_api_call', 'make_api_call');
function make_api_call()
{
//todo: make the api call
wp_redirect(‘another-page’);
}
However, this does not work and returns this from the server: {"status":"error","info":"url_failed"}
What I want to do is to POST the form to admin-post.php, have my plugin run code when it POSTs, and then redirect to another page.
I've checked the documentation for the AVADA theme and it only says that you can specify a url to post to but doesn't give any additional details.
So I eventually got something to work, and I'm not knowledgeable or experienced enough with WordPress development to know if it is the "right" way, but it works well.
I realized that using the "Send to Url" option on the Avada form, it was POSTing the form to the admin-ajax.php file. There's plenty of documentation on that and I was able to partially make that work but I was not able to make it fit my use case b/c even though there is a way to configure the Avada form to redirect to a different URL on success I couldn't append parameters to that URL based on the return value from admin-ajax.php.
For future reference, here's what I was able to make work but not fit my use case by having the Avada form submission set to Send to Url. (I'm recreating this and some of it's from memory since I went with a different solution, so it may not be 100% runnable.)
The way admin-ajax works is all requests to admin-ajax.php are eventually handled by a WordPress action (filter?) like so:
add_action( 'wp_ajax_my_action', 'my_function' );
In the above, my_action is what you've set as the form's action by creating a hidden input element on your html form named action and setting it's value to "my_action". The my_function argument is the name of the function you want to run when the action happens.
add_action( 'wp_ajax_my_action', 'my_function' );
function my_function(){
//do stuff
}
Watching the request in Chrome's dev tools, I could see the action the form was setting was fusion_form_submit_form_to_url.
So ended up with this:
add_action( 'wp_ajax_fusion_form_submit_form_to_url', 'my_function' );
function my_function(){
//do stuff
}
You can see that the url you enter in the Form Submission URL field gets passed to admin-ajax as fusionAction. Whether the Avada theme does something additional with that - I don't know but you could use it to control the logic that gets executed in my_function. I suspect there's an action in the Avada form that works similar to the wp_ajax_ WordPress action but by the time I got this far I realized this wasn't going to work so I pivoted to the actual solution, below.
All of that worked okay but you can't redirect out of a call to admin-ajax.php unless you do it on the client side and I didn't want to dive into that.
What I was able to make work was configuring the Avada form to do a traditional HTTP POST. I added a hidden input element on the Avada form with a name of formName and the value set to the name of the form I wanted to handle.
In my plugin code, I hooked into the WordPress init action as in the code sample below, and then customized the logic to be executed based on which formName was sent in.
add_action('init', 'callback_function');
function callback_function()
{
if (isset($_POST['formName'])) {
$form = $_POST['formName']; //from the hidden input element "formName"
//there is a call to wp_redirect in each case
switch ($form) {
case 'form1':
process_form_1();
break;
case 'form2':
process_form_1();
break;
default:
# code...
break;
}
//without this "exit" you will get errors similar to "headers already sent"
exit;
}
}
This allowed me to run the code I needed to run based on what form was submitted, and redirect to the correct place afterward.

Wordpress Woocommerce plugin mail triggering

Mail not sending when status changed from processing to on-hold and processing to failed.
Kindly tell me how to achieve this. Thanks in advance
Even that kind of questions (directly asking the need without any research or any part of code) are not welcomed here, I want to help you about that by giving the basic idea.
In the file "wp-content/plugins/woocommerce/includes/class-wc-emails.php", search for the "public static function init_transactional_emails()" and check "$email_actions" array there.
$email_actions = apply_filters(
'woocommerce_email_actions', array(
'woocommerce_low_stock',
'woocommerce_no_stock',
'woocommerce_product_on_backorder',
'woocommerce_order_status_pending_to_processing',
'woocommerce_order_status_pending_to_completed',
'woocommerce_order_status_processing_to_cancelled',
'woocommerce_order_status_pending_to_failed',
'woocommerce_order_status_pending_to_on-hold',
'woocommerce_order_status_failed_to_processing',
'woocommerce_order_status_failed_to_completed',
'woocommerce_order_status_failed_to_on-hold',
'woocommerce_order_status_on-hold_to_processing',
'woocommerce_order_status_on-hold_to_cancelled',
'woocommerce_order_status_on-hold_to_failed',
'woocommerce_order_status_completed',
'woocommerce_order_fully_refunded',
'woocommerce_order_partially_refunded',
'woocommerce_new_customer_note',
'woocommerce_created_customer',
)
);
Since after every update of Woocommerce plugin any changes you made on those files will be gone, you need to add your email trigger for status changes you mentioned by either using a hook or overriding the files using your child theme.
About your request, for "from processing to on-hold" you need to add:
'woocommerce_order_status_processing_to_on-hold',
About overriding a file (or function) from includes folder of Woocommerce you may check this post: Override woocommerce files from includes folder
I hope this will help you to solve it. Have a good day.

Send WooCommerce Email on Address Save using woocommerce_email_actions

I've been struggling with this for a couple days now and have been through every post/comment/discussion/etc... I could find trying to find a working solution.
I want to send an email via a custom class that extends WC_Email whenever a woocommerce user updates their address. I've found various resources explaining how to create custom wc emails (skyverge was most useful) and I have successfully done that. I have a plugin that adds a custom email in WP-Admin->WooCommerce->Settings->Emails.
If I use an action that is already part of the woocommerce_email_actions such as add_action( 'woocommerce_order_status_failed_to_processing_notification', array( $this, 'trigger' ) ); and manually change the order status in the backend everything works just fine.
Problem is I want to use add_action( 'woocommerce_customer_save_address', array( $this, 'trigger' ) ); and unfortunately it never fires.
Based on some other threads I've tried adding the following to my main plugin file
function new_woocommerce_email_actions( $actions ){
$actions[] = 'woocommerce_customer_save_address';
return $actions;
}
add_filter( 'woocommerce_email_actions', 'new_woocommerce_email_actions' );
Supposedly this should allow me to use the action in my custom class, but no luck. I've also tried adding other actions without any success. For instance using the filter to add woocommerce_order_status_cancelled won't fire when manually changing the order to cancelled. I'm struggling to figure out why this isn't working and most of the the threads I've found are 2+ years old and dead, so here I am. Any help or pointers would be greatly appreciated.
Posting as an answer in case others are trying to get this functionality working.
I found this article from Tyche Softwares after extensive digging and was able to create a plugin that will send a custom WC_Email whenever a customer updates their address information.
I'm still not 100% as to why the add_filter('woocommerce_email_actions'... wasn't working, and if anyone can tell me why I'd still be interested.
This plugin works by calling a custom action defined in my extended WC_Email class via do_action whenever the existing woocommerce_customer_save_address action happens.

call a function (actually an API call) whenever user place order

I need to call a function (actually an API call) whenever user place order(i.e after checkout).
I searched woo-commerce doc (https://docs.woothemes.com/wc-apidocs/hook-docs.html) but could not find proper hook.
Already i have wasted lot of time for this and client need this to be done.
If anyone can suggest anything then it will very helpful. I am new to woo-commerce.
Thanks for your time.
In your theme's function.php or in your custom plugin Place this:
add_action('woocommerce_payment_complete', 'my_function');
function my_function(){
// Whatever I want to do when payment is completed
// Like api call to external server etc
}
This action triggers when payment is completed. There are other actions available instead of woocommerce_payment_complete like woocommerce_order_status_pending , woocommerce_thankyou etc.
Hope that helps.

Wordpress - action when post is deleted (more then one)

I'm having problem with triggering function on "delete_post" action.
I'm building functionality where i had to add one more table in database (i cannot use wp_postmeta for this one).
Because of this I created a custom table which have that additional data related with posts.
I would like that, when admin delete post from admin panel, to delete data for that post in my custom table (i don't want redundant data to stay there when post is gone)
And i implemented something like this:
add_action('admin_init', 'codex_init');
function codex_init() {
if (current_user_can('delete_posts')) add_action('delete_post', 'delete_something');
}
function delete_something($postid) {
//here im deleting everything from that table with that post id for that post type
}
And this works perfect if someone click to delete only one post. But when I want to delete more posts at once (checking buttons on wordpress admin menu and selecting delete option - bulk deletion), this will not work?
Am i doing something wrong here, or is there different action when someone want to delete several posts at once?
EDIT:
As indicated in the comment bellow, this action works perfectly for bulk and individual delete actions as well. My issue was related with the usage of the global $post variable - to get the id of individual posts - when instead I should use parameter which is provided to the function directly.
I looked at the code in wp-admin/edit.php it does trigger the function wp_delete_post which has your action.
Try this. Does it die and hit the var_dump?
add_action('admin_init', 'codex_init');
function codex_init() {
if (current_user_can('delete_posts')) add_action('delete_post', 'delete_something');
}
function delete_something($postid) {
var_dump('trigger'.$postid);die();
//here im deleting everything from that table with that post id for that post type
}

Resources