AnswerBun.com

Hook when post is set from published to draft?

Is there an action i can hook into when a post is set from published back to draft? I have tried both transition_post_status and publish_to_draft but they don’t seem to work when I set the post back to draft. I am doing something like:

 add_action('publish_to_draft', 'doStuff')
 function doStuff() {
   // do stuff here
 }

Maybe there is something I am missing, but I wanted to check that these are the correct hooks to use.

WordPress Development Asked by Anton Emery on January 2, 2021

1 Answers

One Answer

Yes, these are probably the right calls to use for what you want.

I just checked this by adding this to my functions.php:

add_action('publish_to_draft', 'doStuff');

function doStuff() {
    update_option('foo', 'bar1');
}

Then using the quick edit in the posts list to change a published post to draft, and this hook definitely ran at that point and set this option in the database.

Note that if you echo something from this hook, or expect to see something output to your browser you likely won't see it, and you might cause an error as this might be being called via an ajax type call.

Correct answer by mozboz on January 2, 2021

Add your own answers!

Related Questions

How can I noindex my archive pages?

1  Asked on November 26, 2021 by summer-pratt

     

admin-ajax error 400

0  Asked on November 26, 2021 by daniel-nebert

   

Bulk update custom post types

1  Asked on November 26, 2021 by samm-foolt

     

Upload avatar for post comment

1  Asked on November 26, 2021 by divyang

     

Non-blocking file_put_contents in function.php

2  Asked on November 26, 2021 by mustafa

   

Image problem – Multisite image upload broken

1  Asked on November 23, 2021 by prakhar-monga

   

Where is the htaccess in wordpress.com hosting?

1  Asked on November 23, 2021 by user2007920

   

How do you approach block structured content?

1  Asked on November 23, 2021 by kevin-all

     

Google Fonts not loading correctly

1  Asked on November 23, 2021 by octavialo

   

wp_redirect() function is not working

8  Asked on November 21, 2021 by sans780730

   

How to search only by post title and category?

1  Asked on November 21, 2021 by carl-jue-nier

     

Custom form validation

1  Asked on November 21, 2021 by alexander-holsgrove

   

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved.