0

I am building my first wordpress plugin and the error log is throwing up the following errors.

[06-Jul-2014 20:07:21 UTC] PHP Warning:  array_merge(): Argument #2 is not an array in \wp-content\plugins\test-plugin\screen.php on line 49

[06-Jul-2014 20:07:21 UTC] PHP Warning:  array_merge(): Argument #2 is not an array in \wp-content\plugins\test-plugin\screen.php on line 50

[06-Jul-2014 20:07:21 UTC] PHP Warning:  end() expects parameter 1 to be array, null given in \wp-content\plugins\test-plugin\screen.php on line 323

[06-Jul-2014 20:07:21 UTC] PHP Warning:  Invalid argument supplied for foreach() in \wp-content\plugins\test-plugin\screen.php on line 324

For the errors on line 49 & 50 the code is:

    function register_settings() {
    global $menu;
    global $submenu;
    $this->menus = array_merge(array(), $menu);
    $this->submenus = array_merge(array(), $submenu);
    $this->settings = get_option( $this->settings_name );
    register_setting( 'admin-theme', $this->settings_name );
}

For the errors on lines 323 & 324 the code is:

function admin_menu() {
    global $menu;
    global $submenu;
    // update menu
    end( $menu );
    foreach ($menu as $k=>&$v){
        $id = explode(' <span', $v[0]);
        $slug = 'menu_'.strtolower( str_replace( ' ','_',$id[0] ) );
        $slug_hide = $slug.'_hide';
        if($id[0] != NULL && $this->get_setting($slug) !== NULL){
            $v[0] = $this->get_setting($slug). ( isset($id[1]) ? ' <span '.$id[1] : '' );
        }
        if( $this->get_setting($slug_hide) ){
            unset($menu[$k]);
        }
        // update the submenu
        if( isset($submenu[$v[2]]) ){
            foreach ($submenu[$v[2]] as $key=>&$val){               
                $id = explode(' <span', $val[0]);
                $slug_sub = $slug.'_'.strtolower( str_replace( ' ','_',$id[0] ) );
                $slug_sub_hide = $slug_sub.'_hide';
                if($id[0] != NULL && $this->get_setting($slug_sub) !== NULL){
                    $val[0] = $this->get_setting($slug_sub). ( isset($id[1]) ? ' <span '.$id[1] : '' );
                }
                if( $this->get_setting($slug_sub_hide) ){                       
                    unset( $submenu[$v[2]][$key] );
                }
            }
        }
    }
}

I can't seem to figure it out so if anyonecanhelp or point me in the right direction it would be greatly appreciated.

1
  • 2
    Argument #2 is not an array Commented Jul 7, 2014 at 9:08

1 Answer 1

4

first you need to check your both variables is array

with is_array() like if(is_array($menu))

if not use type cast to convert variable in array

 if(is_array($menu))
      $this->menus = array_merge(array(), $menu);
    else 
      $this->menus = array_merge(array(), (array)$menu);
    if(is_array($submenu))
      $this->submenus = array_merge(array(), $submenu);
    else
      $this->submenus = array_merge(array(), (array)$submenu);
Sign up to request clarification or add additional context in comments.

4 Comments

I used type cast to convert it and it is only throwing up one error now which is on line 347. The error log states:[07-Jul-2014 09:25:48 UTC] PHP Warning: Invalid argument supplied for foreach() in wp-content\plugins\test-plugin\screen.php on line 347. Here is the code foreach ($menu as $key=>&$val){ $id = explode(' <span', $val[0]); $slug = 'menu_'.strtolower( str_replace( ' ','_',$id[0] ) ); $slug_hide = $slug.'_hide'; if($id[0] != NULL && $this->get_setting($slug) !== NULL){ $val[0] = $this->get_setting($slug). ( isset($id[1]) ? ' <span '.$id[1] : '' ); }
cause you are using again $menu and it's not an array i think you need $this->menus instaed $menu
Thanks Rakesh! That was really wrecking my brain. Much appreciated!
@Bebop if this answer did indeed helped you, kindly accept this answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.