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

Field values not saved when using classic editor #1333

Open
JaakkoKarhu opened this issue Sep 3, 2019 · 2 comments
Open

Field values not saved when using classic editor #1333

JaakkoKarhu opened this issue Sep 3, 2019 · 2 comments

Comments

@JaakkoKarhu
Copy link

I am building a plugin, which relies on Meta Box -plugin. Everything works fine, except when using classic editor, ie. with Classic Editor or WP Bakery plugins. Then the changes to the fields are not saved.

I was able to track the issue RW_Meta_Box class save_post() function does not fire for some reason when clicking the "Update" button on classic editor.

I tried to add action to proper hook for fixing the issue, but was not able to.

Is there a solid fix for this issue?

Meta Box version 5.1.2

@rilwis
Copy link
Member

rilwis commented Sep 5, 2019

Hi @JaakkoKarhu,

Do you see the problem if you deactivate the WP Bakery plugin?

@JaakkoKarhu
Copy link
Author

JaakkoKarhu commented Sep 5, 2019

No, with Gutenberg it works perfectly. And to be more specific: even if the WP Bakery is activated, but using Gutenberg, it works. However, everytime on classic edit view, saving fails.

I found a solution, very hackish one. So basically I have a custom hook which is fired on 'save_post':

`

   public function classic_editor_save_metaboxes($id) {
   global $LC_METABOXES;
   $flat_fields = array();

  /* Clumsy sniffing for the editor which is used. We don't want to
   * alter the save functions of Gutenberg and Meta Box, since they
   * are working fine.
   */
  if (!isset( $_POST['post_author_override'])&&!isset($_POST['meta'])) {
    // Editor is most likely Gutenberg.
    return;
  }
 /* I was unable to retrieve the metaboxes in this function, so I followed a _very_ bad
  * practice and stored them on global while initialising. Sorry, had to deliver.
  * Here I am just flattening them for easier use later.
  */
  foreach ($LC_METABOXES as $key => $mb) {
    $flat_fields = array_merge($flat_fields, $mb['fields']);
  };

  if ($id) {
    foreach ($flat_fields as $key => $f) {
      $fid = $f['id'];
      /* Has not been tested against al lof the Meta Box types.
       * Just sorting couple of special cases I recognised.
       */
      if (!isset($_POST[$fid])) {
        if ($f['type']==='checkbox') {
          update_post_meta($id, $fid, 0);
        }
      } else if (gettype($_POST[$fid])==='array'&&$f['type']!=='fieldset_text') {
        delete_post_meta($id, $fid);
        foreach ($_POST[$fid] as $key => $store_this) {
          add_post_meta($id, $fid, $store_this);
        }            
      } else {
        update_post_meta($id, $fid, $_POST[$fid]);
      }
    }
  }
}
add_action( 'save_post', 'classic_editor_save_metaboxes' );

`

I am not very proud of this code. I wrote it while being very tired, so please take this in account when evaluating...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants