save_post action done twice

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

save_post action done twice

Scott Bressler
It seems that in hooking into the save_post action for saving the data in a
custom meta-box, my callback is being called twice when either saving a
draft post or updating a published post. If I instead hook into edit_post,
my callback is only called once.

Is this "double-calling" behavior with save_post expected? Should I be
hooking into edit_post instead?

Thanks,
Scott
_______________________________________________
wp-hackers mailing list
[hidden email]
http://lists.automattic.com/mailman/listinfo/wp-hackers
Reply | Threaded
Open this post in threaded view
|

Re: save_post action done twice

scribu-2
On Fri, Oct 22, 2010 at 1:28 PM, Scott Bressler <[hidden email]> wrote:

> It seems that in hooking into the save_post action for saving the data in a
> custom meta-box, my callback is being called twice when either saving a
> draft post or updating a published post. If I instead hook into edit_post,
> my callback is only called once.
>
> Is this "double-calling" behavior with save_post expected? Should I be
> hooking into edit_post instead?
>

Yes, it's expected. You should be checking the post_type at the beginning of
your callback.
_______________________________________________
wp-hackers mailing list
[hidden email]
http://lists.automattic.com/mailman/listinfo/wp-hackers
Reply | Threaded
Open this post in threaded view
|

Re: save_post action done twice

Scott Bressler
Ah, I see, one is a of type 'revision' and the other of type 'post'. Thanks!

Is save_post better than edit_post? What cases would edit_post miss?

On Fri, Oct 22, 2010 at 3:50 AM, scribu <[hidden email]> wrote:

> On Fri, Oct 22, 2010 at 1:28 PM, Scott Bressler <[hidden email]>
> wrote:
>
> > It seems that in hooking into the save_post action for saving the data in
> a
> > custom meta-box, my callback is being called twice when either saving a
> > draft post or updating a published post. If I instead hook into
> edit_post,
> > my callback is only called once.
> >
> > Is this "double-calling" behavior with save_post expected? Should I be
> > hooking into edit_post instead?
> >
>
> Yes, it's expected. You should be checking the post_type at the beginning
> of
> your callback.
> _______________________________________________
> wp-hackers mailing list
> [hidden email]
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>
_______________________________________________
wp-hackers mailing list
[hidden email]
http://lists.automattic.com/mailman/listinfo/wp-hackers
Reply | Threaded
Open this post in threaded view
|

Re: save_post action done twice

scribu-2
On Fri, Oct 22, 2010 at 1:52 PM, Scott Bressler <[hidden email]> wrote:

> Is save_post better than edit_post? What cases would edit_post miss?
>

From the docblock of wp_insert_post():

 * @uses do_action() Calls 'edit_post' action on post ID and post data if
this is an update.
 * @uses do_action() Calls 'save_post' and 'wp_insert_post' on post id and
post data just before returning.
_______________________________________________
wp-hackers mailing list
[hidden email]
http://lists.automattic.com/mailman/listinfo/wp-hackers
Reply | Threaded
Open this post in threaded view
|

Re: save_post action done twice

John Blackbourn
Two handy functions for this scenario:

wp_is_post_revision( $post_id ) and wp_is_post_autosave( $post_id ).

John

On Fri, Oct 22, 2010 at 1:16 PM, scribu <[hidden email]> wrote:

> On Fri, Oct 22, 2010 at 1:52 PM, Scott Bressler <[hidden email]> wrote:
>
>> Is save_post better than edit_post? What cases would edit_post miss?
>>
>
> From the docblock of wp_insert_post():
>
>  * @uses do_action() Calls 'edit_post' action on post ID and post data if
> this is an update.
>  * @uses do_action() Calls 'save_post' and 'wp_insert_post' on post id and
> post data just before returning.
> _______________________________________________
> wp-hackers mailing list
> [hidden email]
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>
_______________________________________________
wp-hackers mailing list
[hidden email]
http://lists.automattic.com/mailman/listinfo/wp-hackers
Reply | Threaded
Open this post in threaded view
|

Re: save_post action done twice

Andrew Nacin-2
On Fri, Oct 22, 2010 at 11:41 PM, John Blackbourn
<[hidden email]<johnbillion%[hidden email]>
> wrote:

> Two handy functions for this scenario:
>
> wp_is_post_revision( $post_id ) and wp_is_post_autosave( $post_id ).


I think save_post also gets triggered when we create an auto-draft while
loading up post-new.php.
_______________________________________________
wp-hackers mailing list
[hidden email]
http://lists.automattic.com/mailman/listinfo/wp-hackers
Reply | Threaded
Open this post in threaded view
|

Re: save_post action done twice

Kenneth Newman
Seems fairly complicated, and a quite common issue among plugin developers.
Perhaps a thorough tutorial should be included in the Plugin Dev Handbook
;-)

On Sat, Oct 23, 2010 at 10:51 AM, Andrew Nacin <[hidden email]> wrote:

> On Fri, Oct 22, 2010 at 11:41 PM, John Blackbourn
> <[hidden email] <johnbillion%[hidden email]><
> johnbillion%[hidden email] <johnbillion%[hidden email]>>
> > wrote:
>
> > Two handy functions for this scenario:
> >
> > wp_is_post_revision( $post_id ) and wp_is_post_autosave( $post_id ).
>
>
> I think save_post also gets triggered when we create an auto-draft while
> loading up post-new.php.
> _______________________________________________
> wp-hackers mailing list
> [hidden email]
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>
_______________________________________________
wp-hackers mailing list
[hidden email]
http://lists.automattic.com/mailman/listinfo/wp-hackers