hook action after save
- This topic has 8 replies, 2 voices, and was last updated 5 years ago by
nolab.
-
AuthorPosts
-
April 9, 2020 at 4:23 PM #18939
nolab
ParticipantHi!
I'm trying to save to generate a static json with some wordpress data and metabox fields when a user creates / update a post.
If I hook 'on_save_post' my json file gets the correct info for wordpress fields, but just the previous for the metabox fields. I guess that with this action metabox fields have not been updated yet, ยฟright?
I have also tried to hook 'rwmb_after_save_post' but I think it doesn't work for me because there are several meta boxes / fields and it doesn't complete the action...
Is there any action I can use to generate this json when everything is saved?
Thanks for this great plugin ๐
April 9, 2020 at 9:13 PM #18941nolab
ParticipantActually it seems this is not firing when I save a post/page and I change same metabox field value
add_action( 'rwmb_after_save_post', 'on_save_post_rwmb', 10, 1 );
it should fire when I update a post or am I missing something?
Thanks!
April 9, 2020 at 9:21 PM #18942Long Nguyen
ModeratorHi,
You can follow this documentation to use the action
save_post
which is fired whenever a post or page is created or updated, include the meta data (field value) saved.Try to run the action with the sample code below
add_action( 'save_post', 'post_published_notification', 999, 3 ); function post_published_notification( $post_id, $post, $update ) { echo "This post has been published"; echo "<br>"; echo rwmb_meta( $field_id ); echo "<br>"; echo "Generate your JSON here"; die(); }
April 9, 2020 at 9:39 PM #18943nolab
ParticipantHi Thanks for your response.
Yes, it was my first idea, but when I do it like that I get wordpress fields correctly updated, but I get the previous value for the metabox field.
That's why I thought that action save_post fired before metabox update.
I mean if I save twice then the json get the correct values, but in the first attempt I only get the updated value for a wordpress field
April 9, 2020 at 11:02 PM #18946nolab
ParticipantI have updated the version, just in case, and I have made a minimum test and I don't get the updated value for the metabox field with the example code ๐
Is there any work around metabox action where I can get that value field updated??
Thanks a lot
April 10, 2020 at 7:50 AM #18954Long Nguyen
ModeratorHi,
After a few hours of researching, I've found a hook to fire after the metadata is saved
updated_postmeta
, please follow the documentation and the sample codeadd_action( 'updated_postmeta', 'post_published_notification', PHP_INT_MAX, 4 ); function post_published_notification( $meta_id, $post_id, $meta_key, $meta_value ) { echo "This post has been published"; echo "<br>"; echo rwmb_meta( $field_id ); echo "Generate your JSON here"; die(); }
Please try again and let me know how it goes.
April 10, 2020 at 4:41 PM #18961nolab
ParticipantThanks Long Nguyen! It works, I can get the metabox field updated in this action ๐
The only thing is that I have checked that this action runs not only when you actually save a post, but also when you just load a post entry and you don't make any change. So it is executed much many times than desired, but maybe it's the best we can get at the moment
April 10, 2020 at 8:26 PM #18964Long Nguyen
ModeratorHi,
You can check the post or other screen by using the function
get_current_screen()
https://developer.wordpress.org/reference/functions/get_current_screen/and check it before generating JSON
function post_published_notification( $meta_id, $post_id, $meta_key, $meta_value ) { $screen = get_current_screen(); if( is_admin() && is_object( $screen ) ) { if( $screen->id == 'post' ) { echo "This post has been published"; echo "<br>"; echo rwmb_meta( $field_id ); echo "Generate your JSON here"; } } }
April 13, 2020 at 12:59 AM #18994nolab
ParticipantHi!
good idea.
Thanks!!
-
AuthorPosts
- You must be logged in to reply to this topic.