My custom metabox stops working on WP 4.9.8

Support General My custom metabox stops working on WP 4.9.8Resolved

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #11042
    FEDFED
    Participant

    I'm doing something a little out of the ordinary. I'm using the MB Term Meta to create a list of items that I want to use later on in the admin. Basically, if a WP category is selected, I want to show some additional meta associated with the category and use that meta to change the template on the front end. To do this, I made a custom metabox which grabs the data out of the category I'm using.

    Here's the code for the custom metabox, it's pretty straight-forward. The problem is, I don't think it's even firing on the latest version of WP. I can't even get it to echo anything out. There were some changes to meta fields in the newest version and I haven't had a chance to dig in. For now, I'm rolling back my install, but any help would be very much appreciated!

    
    if ( class_exists( 'RWMB_Field' ) ) {
    
    class RWMB_Unit_Field extends RWMB_Checkbox_List_Field {
    
    public static function filter_options( $field, $options ) {
      $current_categories = get_the_category();
    
      foreach( $current_categories as $cat ){
    
        $units = rwmb_meta( 'unit', array( 'object_type' => 'term' ), $cat->term_id );
      }
    
      $options = [];
    
      foreach ( $units as $unit ){
        $options[] = (object) array( 'value' => sanitize_title_with_dashes($unit['name']), 'label' => $unit['name'] );
      }
    
        return $options;
     }
    
    }
    }
    
    #11043
    FEDFED
    Participant

    Scratch that! I updated wordpress and plugins at the same time. It looks to be a problem in metabox 4.15.0 and up. I was testing on 4.15.2 and it was still not working there. Rolling back to 4.14.11 fixes the problem.

    #11060
    Anh TranAnh Tran
    Keymaster

    Hi FED,

    Since 4.15, I've optimized the code for all "choice" field. I removed the filter_options function that is not needed. Can you check the source code of the choice field and try the latest version on Github?

    #11074
    FEDFED
    Participant

    Ah, I was building off the old version of the plugin, so no wonder this stopped working when I updated. Thanks for your prompt reply!

    #11329
    FEDFED
    Participant

    Ok, I'm having some trouble updating my custom field to override the format_single_value method in the RWMB_Choice_Field. I'm extending the RWMB_Input_List_Field class because I want a list of checkboxes. Basically, I want to be able to pre-fill the checkbox options after a user as applied a Category to a CPT.

    However, I'm getting Undefined index: multiple when I try to do that. I guess that makes sense because I'm using multiple values, but trying to override the format_single_value method. Is there another method in RWMB_Input_List_Field that I should be using instead?

    Thanks in advance.

    P.S. I can make a new post if that is preferred.

    #11337
    Anh TranAnh Tran
    Keymaster

    Hi,

    Can you show your code? The function format_single_value doesn't have any relation to multiple. Maybe it comes from the format_clone_value from inc/field.php file, where it checks if the field has multiple values.

    #11764
    FEDFED
    Participant

    Thanks for troubleshooting this with me. I'm at the point now where I can see the fields I want in the admin... but when I try and save the post, I get a bunch of notices and the post won't save (Notice: Undefined index: clone in /phila.gov/wp/wp-content/plugins/meta-box/inc/meta-box.php on line 301) . Here's what I'm working with on the latest version of metabox:

    if ( class_exists( 'RWMB_Field' ) ) {
    
      class RWMB_Unit_Field extends RWMB_Checkbox_List_Field {
    
        public static function normalize( $field ) {
    
          if( isset($_GET['post']) && get_post($_GET['post']) && isset($_GET['action']) && 'edit' == $_GET['action'] )
    
          $post = get_post($_GET['post']);
    
             if( isset($post)
                 && !empty($post->post_type)
                 && is_object_in_taxonomy($post->post_type, 'category')
             ){
                foreach ( (array) get_the_category($post->ID) as $cat ) {
                  if ( empty($cat->slug ) )
                    continue;
    
                  //get data out of term meta field for use later
                  $units[] = rwmb_meta( 'department_units', array( 'object_type' => 'term' ), $cat->term_id);
                }
    
              }
    
          if ( !isset( $units ) )
            return;
    
          foreach ($units as $key => $value){
            foreach($value as $name) {
              $options[] = array(
                'value' => urlencode($name['name']),
                'label' => $cat->name . ' - ' . $name['name']
              );
            }
          }
    
          $field = parent::normalize( $field );
    
          return $field;
    
        }
    
        /*Old, working version 
        public static function filter_options( $field, $options ) {
          $current_cats = get_the_category();
        
          foreach($current_cats as $cat){
            $units[$cat->name] = rwmb_meta( 'department_units', array( 'object_type' => 'term' ), $cat->term_id );
          }
        
          if ( !isset( $units ) )
            return;
        
          $options = [];
        
          foreach ($units as $key => $value){
            foreach($value as $name) {
              $options[] = (object) array( 'value' => urlencode($name['name']), 'label' => $key . ' - ' . $name['name']  );
            }
          }
        
            return $options;
         }
         */
      }
    }
    #11773
    Anh TranAnh Tran
    Keymaster

    Hi,

    I see some PHP errors in your code. So I rewrote it as follows: https://ghostbin.com/paste/b9er9. Please try it.

    #11877
    FEDFED
    Participant

    Thank you! I had to make a couple of modifications to what you posted, but your changes definitely helped! Posting the final(ish) version here in case someone else ever runs into this.

    https://ghostbin.com/paste/ytadu

Viewing 9 posts - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.