• Resolved timbr

    (@brugman)


    Warning: Invalid argument supplied for foreach() in /public_html/wp-content/plugins/meta-box/inc/init.php on line 17

    For anyone else who wants to fix this themselves:

    Current code

    $meta_boxes = apply_filters( 'rwmb_meta_boxes', array() );
    foreach ( $meta_boxes as $meta_box )
    {
    	new RW_Meta_Box( $meta_box );
    }

    Fixed code

    $meta_boxes = apply_filters( 'rwmb_meta_boxes', array() );
    if ( isset( $meta_boxes ) ) foreach ( $meta_boxes as $meta_box ) new RW_Meta_Box( $meta_box );

    https://www.ads-software.com/plugins/meta-box/

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Anh Tran

    (@rilwis)

    That’s strange because isset always returns true.

    I guess the returned value from rwmb_meta_boxes is not array, which is invalid. You should check the code for that.

    Thread Starter timbr

    (@brugman)

    IIRC $meta_boxes was empty, that’s why the foreach threw the warning. I see the problem still exists today….

    Instead of isset, is_array or !empty would’ve been better indeed.

    Plugin Author Anh Tran

    (@rilwis)

    I think even if $meta_boxes is empty, the foreach loop will stop immediately without throwing any error. It only throws error if the variable is not an array.

    I made a small test with foreach like this and it works perfectly:

    <?php
    $tests = array(
    	'array'        => array(),
    	'null'         => null,
    	'zero'         => 0,
    	'empty string' => '',
    	'false'        => false,
    );
    
    foreach ( $tests as $k => $v )
    {
    	echo '<h1>Test for "' . $k . '"</h1>';
    
    	// If error, we will see it throws here
    	foreach ( $v as $value )
    	{
    		echo $value . '<br>';
    	}
    
    	echo '<br><hr>';
    }

    https://prntscr.com/6g55ma

    We can see that only if the value is not an array then error appears.

    Anyway, to prevent future wrong input from users, I’ll add more check to the plugin ??

    Plugin Author Anh Tran

    (@rilwis)

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Your plugin throws a warning (Invalid argument)’ is closed to new replies.