Support Forum
Support › MB Settings Page › Invalid argument supplied for foreach: Warning
Hi,
We are working on a new Woocommerce theme.
I have created a settings page (which contains a simple text field with some pre-defined options) to collect extra details for the WooCommerce products. Also, used duplicate functions to the field to let users create their own fields. It is working well.
But the problem is when there are no fields added to the database, it throws an invalid argument supplied for the foreach statement from the /inc/meta-box.php.
Warning: Invalid argument supplied for foreach() in /Applications/MAMP/htdocs/wptest/wp-content/plugins/meta-box/inc/meta-box.php on line 382
Warning: Invalid argument supplied for foreach() in /Applications/MAMP/htdocs/wptest/wp-content/plugins/meta-box/inc/meta-box.php on line 112
Warning: Invalid argument supplied for foreach() in /Applications/MAMP/htdocs/wptest/wp-content/plugins/meta-box/inc/meta-box.php on line 84
As it is coming from the main core plugin, I can't add any validation. I Hope, if you add the below conditions before fields creation, it will suppress the error.
/inc/meta-box.php, line number 84
/**
* Add fields to field registry.
*/
public function register_fields() {
$field_registry = rwmb_get_registry( 'field' );
foreach ( $this->post_types as $post_type ) {
if( ! empty ( $this->fields ) ) {
foreach ( $this->fields as $field ) {
$field_registry->add( $field, $post_type );
}
}
}
}
/inc/meta-box.php, line number 112
if( ! empty ( $this->fields ) ) {
// Add additional actions for fields.
foreach ( $this->fields as $field ) {
RWMB_Field::call( $field, 'add_actions' );
}
}
/inc/meta-box.php, line number 382
public static function normalize_fields( $fields, $storage = null ) {
if( ! empty ( $fields ) ) {
foreach ( $fields as $k => $field ) {
$field = RWMB_Field::call( 'normalize', $field );
// Allow to add default values for fields.
$field = apply_filters( 'rwmb_normalize_field', $field );
$field = apply_filters( "rwmb_normalize_{$field['type']}_field", $field );
$field = apply_filters( "rwmb_normalize_{$field['id']}_field", $field );
$field['storage'] = $storage;
$fields[ $k ] = $field;
}
return $fields;
}
}
Please let me know if I am wrong with this.
Regards,
Aishwarya
Hi,
Can you please share the code that creates the settings page and custom fields?
Hi,
Sure
//Add Metabox settings page under Products menu
add_filter( 'mb_settings_pages', function ( $settings_pages ) {
$settings_pages[] = array(
'id' => 'shopily-extra-fields',
'option_name' => 'shopily_product_fields',
'menu_title' => esc_html__( 'Extra details', 'shopily' ),
'icon_url' => 'dashicons-edit',
'style' => 'no-boxes',
'columns' => 1,
'parent' => 'edit.php?post_type=product',
);
return $settings_pages;
} );
// Register meta boxes and fields for settings page
add_filter( 'rwmb_meta_boxes', function ( $meta_boxes ) {
$meta_boxes[] = array(
'id' => 'shopily-custom-fields',
'title' => esc_html__( 'Product specification details', 'shopily' ),
'settings_pages' => 'shopily-extra-fields',
'customizer' => false,
'fields' => array(
array (
'id' => 'shopily_custom_fields',
'type' => 'text',
'name' => esc_html__( 'Add field', 'shopily' ),
'label_description' => 'Label description',
'desc' => esc_html__( 'To add your own specification, just type in the label by not selecting the select option.', 'shopily' ),
'clone' => true,
'placeholder' => esc_html__( 'Enter field label', 'shopily' ),
'size' => 30,
'datalist' => array(
'id' => 'shopily_default_fields',
// List of predefined options
'options' => array(
esc_html__( 'Material', 'shopily' ),
esc_html__( 'Type', 'shopily' ),
esc_html__( 'Added date', 'shopily' ),
),
),
),
),
);
return $meta_boxes;
} );
This is the code. When I add a field from the settings page, the error goes away because the foreach has some value to iterate.
Hi,
I do not see that error message on my local site when not adding a field to the setting page. Screen record https://www.loom.com/share/a436cbca133e4fc998d1f6c78e3321ac
Can you please share a screen record on your site? And let me know which version of Meta Box and MB Settings Page on your site is.
Hi,
Thanks for checking it out. But you have commented on the fields array shopily_custom_fields
and prevented it from displaying. Check it by enabling the field in the code but without adding data from the browser by clicking "Add field".
Hope you understand!
Here you go https://www.loom.com/share/4e3e277783ee4ebfb401f5857bb554f1
No error shows up.
Here is the screencast record. I am using Metabox 5.5.1 and Settigs page 2.1.4
https://www.dropbox.com/s/20f2rb4bei9q42h/Screencast2021-12-16%2014.17.59.MP4?dl=0
Hi,
Please try to deactivate all plugins except Meta Box, MB Settings Page, switch to the standard theme of WordPress, add just your code above to create the settings page and text field. Then re-check the issue.
Let me know how it goes.