My custom metabox stops working on WP 4.9.8
- This topic has 8 replies, 2 voices, and was last updated 6 years, 5 months ago by
FED.
-
AuthorPosts
-
August 17, 2018 at 9:10 PM #11042
FED
ParticipantI'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; } } }
August 17, 2018 at 10:11 PM #11043FED
ParticipantScratch 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.
August 20, 2018 at 1:48 PM #11060Anh Tran
KeymasterHi 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 thechoice
field and try the latest version on Github?August 21, 2018 at 1:56 AM #11074FED
ParticipantAh, I was building off the old version of the plugin, so no wonder this stopped working when I updated. Thanks for your prompt reply!
September 12, 2018 at 9:56 PM #11329FED
ParticipantOk, I'm having some trouble updating my custom field to override the
format_single_value
method in the RWMB_Choice_Field. I'm extending theRWMB_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 theformat_single_value
method. Is there another method inRWMB_Input_List_Field
that I should be using instead?Thanks in advance.
P.S. I can make a new post if that is preferred.
September 14, 2018 at 2:36 PM #11337Anh Tran
KeymasterHi,
Can you show your code? The function
format_single_value
doesn't have any relation tomultiple
. Maybe it comes from theformat_clone_value
frominc/field.php
file, where it checks if the field has multiple values.October 26, 2018 at 1:49 AM #11764FED
ParticipantThanks 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; } */ } }
October 26, 2018 at 3:14 PM #11773Anh Tran
KeymasterHi,
I see some PHP errors in your code. So I rewrote it as follows: https://ghostbin.com/paste/b9er9. Please try it.
November 7, 2018 at 9:28 PM #11877FED
ParticipantThank 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.
-
AuthorPosts
- You must be logged in to reply to this topic.