Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preview/Autosave not saving meta fields #20755

Closed
isuke01 opened this issue Mar 10, 2020 · 5 comments
Closed

Preview/Autosave not saving meta fields #20755

isuke01 opened this issue Mar 10, 2020 · 5 comments
Labels
[Feature] Meta Boxes A draggable box shown on the post editing screen [Status] Duplicate Used to indicate that a current issue matches an existing one and can be closed

Comments

@isuke01
Copy link

isuke01 commented Mar 10, 2020

Describe the bug
I noticed that issue using ACF 5.8.8. First I did some tests, I have read I have to use 'show_in_rest' => true so I did some tests, and still looks like it does not work at all.

I have created custom meta but nothing is saved.
Also I noticed during preview save action inside hook post_save value for $_POST are empty so it might be an issue too.

I tested that without Gutenberg, same setup, everything work as expected.

To reproduce
Here is code I used for tests:

function myprefix_add_meta_box() {
    add_meta_box( 
      'myprefix_post_options_metabox', 
      'Post Options', 
      'myprefix_post_options_metabox_html', 
      'post', 
      'normal', 
      'default'
    );
  }
  add_action( 'add_meta_boxes', 'myprefix_add_meta_box' );
  function myprefix_post_options_metabox_html($post) {
    $field_value = get_post_meta($post->ID, '_myprefix_text_metafield', true);
    wp_nonce_field( 'myprefix_update_post_metabox', 'myprefix_update_post_nonce' );
    ?>
    <p>
      <label for="myprefix_text_metafield"><?php esc_html_e( 'Text Custom Field', 'textdomain' ); ?></label>
      <br />
      <input class="widefat" type="text" name="myprefix_text_metafield" id="myprefix_text_metafield" value="<?php echo esc_attr( $field_value ); ?>" />
    </p>
    <?php
  }
  function myprefix_save_post_metabox($post_id, $post) {
    $edit_cap = get_post_type_object( $post->post_type )->cap->edit_post;
    if( !current_user_can( $edit_cap, $post_id )) {
      return;
    }
    if( !isset( $_POST['myprefix_update_post_nonce']) || !wp_verify_nonce( $_POST['myprefix_update_post_nonce'], 'myprefix_update_post_metabox' )) {
      return;
    }
    if(array_key_exists('myprefix_text_metafield', $_POST)) {
      update_post_meta( 
        $post_id, 
        '_myprefix_text_metafield', 
        sanitize_text_field($_POST['myprefix_text_metafield'])
      );
    }
  }
  add_action( 'save_post', 'myprefix_save_post_metabox', 10, 2 );

  function myprefix_register_meta() {
    register_meta('post', '_myprefix_text_metafield', array(
      'show_in_rest' => true,
      'type' => 'string',
      'single' => true,
      'auth_callback' => function() { 
        return current_user_can('edit_posts');
      }
    ));
  }
  add_action('init', 'myprefix_register_meta');

For Autosave

  1. just create new post
  2. meta fields and e.g tilte
  3. Wait for autosave,
  4. Check meta for autosaved post.

For preview:

  1. just hit Preview button
  2. check revision ID
  3. check DB post meta of this review.

Expected behavior
Those custom meta fields must be saved during preview for proper display on frontend.
Auto save also must save meta fields that user entered.

Screenshots
image
image

Desktop (please complete the following information):

  • OS: iOS
  • Browser Chrome
  • WP Version: 5.3.2
  • Theme : WP Default
@Soean
Copy link
Member

Soean commented Mar 10, 2020

I think this is a feature, not a bug. Meta boxes should only save on full saves, see #7502
Also see this 8 years old ticket about preview and meta: https://core.trac.wordpress.org/ticket/20299

@Soean Soean added the [Feature] Meta Boxes A draggable box shown on the post editing screen label Mar 10, 2020
@isuke01
Copy link
Author

isuke01 commented Mar 10, 2020

So this "feature" does:
Not save user progress with meta fields during autosave/auto-draft save?
And does not show meta fields during preview?
Can i disable this "feature" #7502?

I'd really like to show my custom fields to user, they are kind of important during preview.

But without Gutenberg everything works fine.

Actually this "old trick" https://core.trac.wordpress.org/ticket/20299
does not help me ;/

@Soean
Copy link
Member

Soean commented Mar 10, 2020

It does not save on autosave, because there is no meta revision. If a user or wordpress saves a post, the old meta data is overriden and you can't undo the change.

But you are right. If you have a new post and just want to preview it, it should save the data, because there is no old data.

@isuke01
Copy link
Author

isuke01 commented Mar 10, 2020

Ok, so is there any way to force save meta for review using Gutenberg? As I mentioned before, it works correctly without Gutenberg, same setup.
This is important since I my frontend look's really depend on those meta fields.

@talldan talldan added the [Status] Duplicate Used to indicate that a current issue matches an existing one and can be closed label Mar 31, 2020
@talldan
Copy link
Contributor

talldan commented Mar 31, 2020

The preview issue is tracked here - #14900.

Unfortunately saving metaboxes during preview caused preview not to work correctly (it deleted the revision caused by the preview). It's not a trivial thing to fix, unfortunately.

@talldan talldan closed this as completed Mar 31, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Meta Boxes A draggable box shown on the post editing screen [Status] Duplicate Used to indicate that a current issue matches an existing one and can be closed
Projects
None yet
Development

No branches or pull requests

3 participants