1

I have some meta boxes setup, some are my own, some are initialized in plugins.

As the title indicates, these meta boxes are not updating graphically after the post has been submitted.

Here's an example of the problem, I have a metabox with a single toggle to enable notifications. After the first notification is sent and the post has been published, I unset the checkbox and add a little warning above it. However, this doesn't update until I refresh the page entirely.

I'm fairly new to wordpress but this does not seem like the correct behaviour, as when I debug the code I can verify that the metabox callback is getting called after save_post.

I can also see by viewing the Network tab in Chrome's Dev Tools, that when I 'save_post', it downloads a load of re-generated HTML with all the metaboxes updated. However I see no evidence of these elements updating in the editor.

I suspect it might be due to a quirk in the new editor.

It appears to be happening with all meta boxes, except funnily enough the featured image - I've got it set up so if there's no _thumbnail_id set then it sets one by default, and after saving the post this image appears in the featured image box.

function cabtv_add_post_options()
{
    // Add our meta box for the "post" post type (default)
    if ( current_user_can("send_notifications") ) {
        $post_types = ["post", "go_live"];
        foreach ($post_types  as $post_type) {
            add_meta_box(
                'cabtv_notif_on_post',
                'Notifications',
                'cabtv_notif_on_post_html_view',
                $post_type,
                'side',
                'high'
            );
        }
    }
}
add_action('admin_init', 'cabtv_add_post_options');

function cabtv_notif_on_post_html_view($post)
{
    //wp_nonce_field( 'cabtv_notif_metabox', 'cabtv_notif_metabox' );
    $checked = cabtv_should_post_send_notification($post);

    if ( get_post_meta( $post->ID, 'cabtv_notifications_sent', true ) == "1" ) { ?>
        <div style='padding:10px;'><span style='color:red; font-weight:bold;'>Notifications have already been sent for this post.</span></div>
    <?php } ?>

    <input type="hidden" name="cabtv_meta_box_present" value="true"></input>
    <input type="checkbox" name="cabtv_send_notification" value="true" <?php if ($checked) echo 'checked'; ?>></input>
    <label>
    <?php echo esc_attr('Send notification on '.($post->post_status === 'publish' ? 'update' : 'publish') ); ?>
    </label>
<?php
}

Some help would be appreciated.

2
  • The same problem, The problem occurs when you need to show some data depending on some value of meta box or after a change of post status.
    – Rakesh Roy
    Nov 19, 2020 at 12:49
  • Been over a year since I posted and STILL no solution! Weird thing is I can see the metabox html callback is being called after the post is updated, reflecting the new changes, but none of these changes are applied to the actual webpage!
    – hedgehog90
    Jul 7, 2021 at 14:24

2 Answers 2

0

snippet code of notification display when check notify checkbox and display message in Update and Publish post or page in admin

 function cabtv_add_post_options()
    {
        // Add our meta box for the "post" post type (default)    
        $post_types = array("post", "page");

        foreach ($post_types  as $post_type) {

            add_meta_box('cabtv_notif_on_post','Notifications','cabtv_notif_on_post_html_view',$post_type,'side','high');
        }

    }
    add_action('add_meta_boxes', 'cabtv_add_post_options');


    function cabtv_notif_on_post_html_view($post)
    {   
        wp_nonce_field( 'cabtv_notif_metabox', 'cabtv_notif_metabox' );

        $checked = get_post_meta( $post->ID, 'cabtv_notifications_sent', true);
        $check = $checked?'checked':'';

        if ($checked) { ?>
            <div style='padding:10px;'>
                <span style='color:red; font-weight:bold;'>Notifications have already been sent for this post.</span>
            </div>
        <?php } ?>

        <input type="hidden" name="cabtv_meta_box_present" value="true"></input>
        <input type="checkbox" name="cabtv_send_notification" value="true" <?php echo $check; ?>></input>
        <label>
        <?php echo esc_attr('Send notification on '.($post->post_status === 'publish' ? 'update' : 'publish') ); ?>
        </label>

    <?php
    }




    function save_notice_meta_box_data( $post_id ) {

        // Check if our nonce is set.
        if ( ! isset( $_POST['cabtv_notif_metabox'] ) ) {
            return;
        }

        // Verify that the nonce is valid.
        if ( ! wp_verify_nonce( $_POST['cabtv_notif_metabox'], 'cabtv_notif_metabox' ) ) {
            return;
        }

        // If this is an autosave, our form has not been submitted, so we don't want to do anything.
        if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
            return;
        }

        // Check the user's permissions.
        if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) {

            if ( ! current_user_can( 'edit_page', $post_id ) ) {
                return;
            }

        }
        else {

            if ( ! current_user_can( 'edit_post', $post_id ) ) {
                return;
            }
        }

        /* OK, it's safe for us to save the data now. */

        // Make sure that it is set.
        if ( ! isset( $_POST['cabtv_send_notification'] ) ) {
            return;
        }

        // Sanitize user input.
        $my_data = sanitize_text_field( $_POST['cabtv_send_notification'] );

        // Update the meta field in the database.
        update_post_meta( $post_id, 'cabtv_notifications_sent', $my_data?$my_data:'');
    }

    add_action( 'save_post', 'save_notice_meta_box_data' );

**save_notice_meta_box_data function Code Explaination : **

// Check if our nonce is set.

if ( ! isset( $_POST['cabtv_notif_metabox'] ) ) {
    return;
}

// Check the user's permissions.

if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) {

        if ( ! current_user_can( 'edit_page', $post_id ) ) {
            return;
        }

    }
    else {

        if ( ! current_user_can( 'edit_post', $post_id ) ) {
            return;
        }
    }

// Make sure that it is set.

if ( ! isset( $_POST['cabtv_send_notification'] ) ) {
        return;
    }

// Update the meta field in the database.

 update_post_meta( $post_id, 'cabtv_notifications_sent',$my_data);

//Save post and page Hook

add_action( 'save_post', 'save_notice_meta_box_data' );
1
  • I appreciate the work you put into that but it supposes I didn't understand how post meta works, or that I wasn't doing anything with them in 'save_post', and you ignored the fundamental part of my question about reloading the meta box html after the post is saved.
    – hedgehog90
    Jul 9, 2021 at 9:37
0

I managed to cobble something together to update the meta box html via Javascript and wrapping a wp core dispatch that I found in edit-post.js: https://wordpress.stackexchange.com/a/391627/183281

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.