0

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.

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' );
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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