meta-box icon indicating copy to clipboard operation
meta-box copied to clipboard

Field values not saved when using classic editor

Open JaakkoKarhu opened this issue 6 years ago • 2 comments

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

JaakkoKarhu avatar Sep 03 '19 16:09 JaakkoKarhu

Hi @JaakkoKarhu,

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

rilwis avatar Sep 05 '19 04:09 rilwis

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...

JaakkoKarhu avatar Sep 05 '19 10:09 JaakkoKarhu