Custom Fields
Re-Envisioned
Support General hook action after save

  • Creator
    Topic
  • #18939
    Resolved nolab
    Participant

    Hi!

    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 ๐Ÿ™‚

Viewing 8 replies - 1 through 8 (of 8 total)
  • Author
    Replies
  • #18941
    nolab
    Participant

    Actually 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!

    #18942
    Long Nguyen
    Moderator

    Hi,

    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();
    }
    #18943
    nolab
    Participant

    Hi 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

    #18946
    nolab
    Participant

    I 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

    #18954
    Long Nguyen
    Moderator

    Hi,

    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 code

    add_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.

    #18961
    nolab
    Participant

    Thanks 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

    #18964
    Long Nguyen
    Moderator

    Hi,

    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";
            }
        }
    }
    #18994
    nolab
    Participant

    Hi!

    good idea.

    Thanks!!

Viewing 8 replies - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.