Skip to content

add_meta_box breaks post preview #15404

Closed

Description

@braican

Describe the bug
When adding a meta box via WordPress' add_meta_box function to any post type, it appears to break the previewing functionality of that post. When working off of a fresh installation, I'm able to add a new post or edit an existing post and use the Preview button to preview my unsaved changes. However, when I add a meta box to a post using the add_meta_box function, clicking the Preview button does not show my unsaved changes - it just shows the last saved version of the post.

To reproduce
Steps to reproduce the behavior:

  1. Go to any post on any theme that doesn't utilize add_meta_box with no plugins enabled, make a change to the content in the editor, and click preview. You should see the unsaved changes in the preview.

  2. In your active theme's functions.php file, add a simple call to add_meta_box:

    add_action('add_meta_boxes', function() {
      add_meta_box( 'test-meta-box', 'Testing meta boxes', function() {
        echo 'this is a test';
      });
    });
    
  3. Go back to a post in WordPress and notice the new meta box below the Gutenberg editor. Now make a change to the title or content and click preview. If you're like me, you won't see those unsaved changes in the preview.

Expected behavior
I expect to see unsaved changes to my title and/or content appear when I view a preview of my post, while also being able to add any number of meta boxes to that post.

Screenshots

Gif of previewing a post with add_meta_box commented out (preview working as expected):

07b5ac8423ae67207b1b0b0db17f59ca

Gif of uncommenting add_meta_box and trying to preview changes (preview not working as expected):

4e838c84ece7330d731356ad24367c2e

(It happens fast, but you can see the Testing meta boxes meta box title at the bottom of the Gutenberg editor).

This is happening both when I try using the Twenty Nineteen theme and when using our custom theme. The above tests are on a WordPress installation with no plugins installed or enabled.

Desktop (please complete the following information):

  • OS: Mac OSX 10.14.4
  • Browser: Chrome
  • Version: 73.0.3683.103

Additional context
This is on WordPress version 5.1.1.

In looking at the database after each of these test, it seems like when there's a call to add_meta_box, it prevents the creation of a new revision. In the first screencast, when the preview button works as expected, clicking the preview button creates a new entry in the wp_posts table. In the second screencast and when a call to add_meta_box is being made, clicking the preview button does not create a new entry in the wp_posts table.

I discovered this while testing preview functionality with ACF. Basically adding a field group to any post type broke the preview in this way since ACF uses the add_meta_box function to create the field controls.

Activity

  1. ernilambar commented on May 3, 2019

    @ernilambar
    Member

    This issue looks similar to #12617

  2. added
    [Feature] Meta Boxes
    [Type] Bug
    Backwards Compatibility
    [Feature] Saving
    on May 23, 2019
  3. spencerm commented on Jun 4, 2019

    @spencerm

    It's the same as #14900 which was a fix to #12617

  4. braican commented on Jun 4, 2019

    @braican
    Author

    Updating to WordPress 5.2 fixed this specific issue, as that release included the fix in #14877. The issue regarding metaboxes not being previewable still persists, however, and is being tracked now in #14900. I'll close this in favor of that issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Backwards Compatibility[Feature] Meta Boxes[Feature] Saving[Type] Bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions