wordpress - catching save event with a plugin - wordpress

i'm writing my first plugin for wordpress and i have some doubts regarding the hooks.
So i want an action to be executed when admin saves a post, and i'm using (inside a class):
add_action( 'save_post', array($this, 'save_post'));
function save_post(){
global $wp_query;
var_dump($wp_query);
}
the problem is that it prints the global variable when admin opens the "create a new post" and it doesn't when the user saves a post.
I want it to happen the other way around, but i can't find anything in the docs, and i'm completely alone here.
Any help? thanks

I think you're looking for the 'publish_post' action. There's also a 'draft_post' if you need it.

Related

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.

Trigger a Link once a new post is Published

Is there Anyway to trigger a link once new post is Publish ?. My goal is to use it to ping sitemap
http://google.com/ping?sitemap=http://www.example.com/my_sitemap.xml
I simply don't want to set cron job for this. Please Gurus in the house is there anyway I can use theme function to achieve this ? I am new to wp coding Please.
From the Wordpress Plugin API:
publish_post is an action triggered whenever a post is updated and its new status is "publish"
So in your theme's functions.php, write your function to call the google url, then use the add_action function, with publish_post as the hook, and your function's name as the callback.
function test()
{
write code here.....
}
add_action( 'publish_post', 'test' );

wordpress add_action('save_post', 'my_function) not working

I am trying to trigger an even upon saving / updating a post in wordpress... see here:
add_action('save_post', 'generate_location');
function generate_location($post_id) {
echo "hey";
}
the problem is that its not working...
any ideas why? Syntax?
WordPress implements the Post/Redirect/Get pattern to avoid duplicate form submissions, so you're not going to see anything echo'd from a save_post callback.
Instead, you can do a wp_die( 'hey' ) instead, or log something to the database or file system.
I don't know whether you got this working, but I was having the same issue and discovered how to fix it!
in wp-includes/post.php on line 2940 (at the time of writing), this if/else is run whilst saving a post:
if ( !empty($page_template) && 'page' == $data['post_type'] ) {
You will notice that, if there is an error with the template the function stops there and save_post is never called.
In my case, the posts I was trying to save were imported from a pre-existing site. The new site had no page templates at all, so WP was still trying to save the page with the template from before, failing, and thus; save_post was never called.
I added
/* Template Name: Default Template */
to page.php, bulk edit, selected the template and saved. Remove the template name from page.php (as it shows up twice(, and now save_post is triggered every time.
This was the solution in my case anyway. i'm sure it'll affect someone else, somewhere down the line.

delete_post hook in Wordpress not working

add_action( 'delete_post', 'test_function' );
function test_function(){
echo "Hello!";
}
The "Hello!" isn't showing up when I delete a post (It is a custom post-type, but that shouldn't matter right?). How do I debug this?
EDIT: I can't put that code in any front-end files like header.php or index.php because I won't be able to view the output when I delete a post from the back-end. What's the best way to tackle this?
Thanks
Try doing the following to see if you are reaching your filter. I tested this here with 3.2.1 and it works fine for me.
function test_function(){
die('deleted post');
}
this action will not run until you delete the post from the trash.
If you want it to run when you move it to the trash the action is 'trash_post'.

finding do_action function

I've got this.
do_action( 'bp_before_directory_groups_list' );
and I cant find the corresponding add action to trace the function that is called. Anyone know what im doing wrong?
Mark
Maybe there isn't one, as it may be just there so as to enable other plugins to hook into it But to check, run this code after loading all the plugins, like in a theme file:
print_r($wp_filter['bp_before_directory_groups_list']);
$wp_filter is a global variable that stores all the action hooks and filters added by plugins.

Resources