0

Okay I have installed a theme in wordpress that returns a few errors.

Warning: Invalid argument supplied for foreach() in /home/mvprop/public_html/wp-content/themes/yoo_vox_wp/warp/systems/wordpress.3.0/helpers/system.php on line 339

This problem is resolved on these forums

but I can't understand what the last post is about. The guy posts a bunch of random code that solves the problem. He doesn't specify where it's from or where to put it. Just pastes code that doesn't seem to have to do with anything.

line 334 to the end of the file

function getWidgets($position = null) {

    if (empty($this->widgets)) {
        foreach (wp_get_sidebars_widgets() as $pos => $ids) {
            $this->widgets[$pos] = array();
            foreach ($ids as $id) {
                $this->widgets[$pos][$id] = $this->getWidget($id);
            }
        }
    }

    if (!is_null($position)) {
        return isset($this->widgets[$position]) ? $this->widgets[$position] : array();
    }

    return $this->widgets;
}

/*
    Function: displayWidget
        Checks if a widget should be displayed

    Returns:
        Boolean
*/
function displayWidget($widget) {
    if (!isset($widget->options['display']) || in_array('*', $widget->options['display'])) return true;

    foreach ($this->getQuery() as $q) {
        if (in_array($q, $widget->options['display'])) {
            return true;
        }
    }

    return false;
}

/*
    Function: overrideConfig
        Overrides default config based on page

    Returns:
        Void
*/
function overrideConfig() {
    if (!count($this->config_overrides)) return;

    foreach ($this->getQuery() as $q) {
        if (isset($this->config_overrides[$q])) {
            $this->warp->config->parseString($this->config_overrides[$q]);
        }
    }
}

/*
    Function: isBlog

    Returns:
        Boolean
*/
function isBlog() {
    return true;
}

/*
    Function: isPreview
        Checks for default widgets in theme preview 

    Returns:
        Boolean
*/
function isPreview($position) {

    // preview postions
    $positions = array('logo', 'right');

    return is_preview() && in_array($position, $positions);
}

/*
    Function: ajaxSearch
        Ajax search callback

    Returns:
        String
*/
function ajaxSearch(){
    global $wp_query;

    $result = array('results' => array());
    $query  = isset($_REQUEST['s']) ? $_REQUEST['s']:"";

    if (strlen($query)>=3) {

        $wp_query->query_vars['s'] = $query;
        $wp_query->is_search = true;

        foreach ($wp_query->get_posts() as $post) {

            $content = !empty($post->post_excerpt) ? strip_tags(do_shortcode($post->post_excerpt)) : strip_tags(do_shortcode($post->post_content));

            if (strlen($content) > 255) {
                $content = substr($content, 0, 254).'...';
            }

            $result['results'][] = array(
                'title' => $post->post_title,
                'text'  => $content,
                'url'   => get_permalink($post->ID)
            );
        }
    }

    die(json_encode($result));
}

/*
    Function: _adminInit
        Admin init actions

    Returns:
        Void
*/
function _adminInit() {

    if ((defined('DOING_AJAX') && DOING_AJAX) && isset($_POST['warp-ajax-save'])) {

        // update option values
        foreach ($_POST as $option => $value) {
            if (preg_match('/^(warp_|'.preg_quote($this->prefix, '/').')/', $option)) {
                update_option($option, $value);
            }
        }

        die();
    }

    wp_enqueue_script('warp-admin', rtrim(get_bloginfo('template_url'),'/').'/warp/systems/wordpress.3.0/js/wp-admin.js', false, '1.0');
    add_action('wp_ajax_save_nav_settings', array($this,'_save_nav_settings'));
    add_action('wp_ajax_get_nav_settings', array($this,'_get_nav_settings'));
}

/*
    Function: _adminHead
        Admin head actions

    Returns:
        Void
*/
function _adminHead() {

    // init vars
    $path =& $this->getHelper('path'); 

    $head[] = '<link rel="stylesheet" href="'.$path->url('warp:systems/wordpress.3.0/css/admin.css').'" type="text/css" />';
    $head[] = '<script type="text/javascript" src="'.$path->url('warp:systems/wordpress.3.0/js/admin.js').'"></script>';

    echo implode("\n", $head);
}

/*
    Function: _adminMenu
        Admin menu actions

    Returns:
        Void
*/
function _adminMenu() {

    // init vars
    $path =& $this->getHelper('path');
    $name = $this->xml->document->getElement('name');
    $icon = $path->url('warp:systems/wordpress.3.0/images/yoo_icon_16.png');

    if (function_exists('add_object_page')) {
        add_object_page('', $name->data(), 8, 'warp', false, $icon);
    } else {
        add_menu_page('', $name->data(), 8, 'warp', false, $icon); 
    }

    add_submenu_page('warp', 'Theme Options', 'Theme Options', 8, 'warp', array($this, '_adminThemeOptions'));
    add_submenu_page('warp', 'Widget Options', 'Widget Options', 8, 'warp_widget', array($this, '_adminWidgetOptions'));
}

/*
    Function: _adminThemeOptions
        Render admin theme options layout

    Returns:
        Void
*/  
function _adminThemeOptions() {

    // init vars
    $path  =& $this->getHelper('path');
    $xml   =& $this->getHelper('xml');
    $http  =& $this->getHelper('http');
    $check =& $this->getHelper('checksum');

    // get warp xml
    $warp_xml = $xml->load($path->path('warp:warp.xml'), 'xml', true);

    // update check
    $update = null;
    if ($url = $warp_xml->document->getElement('updateUrl')) {

        // get template info
        $template = get_template();
        $version  = $this->xml->document->getElement('version');
        $url      = sprintf('%s?application=%s&version=%s&format=raw', $url->data(), $template, $version->data());

        // only check once a day 
        if (get_option($this->prefix.'update_check') != date('Y-m-d').' '.$version->data()) {
            if ($request = $http->get($url)) {
                update_option($this->prefix.'update_check', date('Y-m-d').' '.$version->data());
                update_option($this->prefix.'update_data', $request['body']);
            }
        }

        // decode update response
        $update = json_decode(get_option($this->prefix.'update_data'));
    }

    // verify theme files
    if (($checksums = $path->path('template:checksums')) && filesize($checksums)) {
        $check->verify($path->path('template:'), $log);
    } else {
        $log = false;
    }

    echo $this->warp->template->render('admin/theme_options', array('xml' => $this->xml, 'warp_xml' => $warp_xml, 'update' => $update, 'checklog' => $log));
}

/*
    Function: _adminWidgetOptions
        Render admin widget options layout

    Returns:
        Void
*/  
function _adminWidgetOptions() {

    // get position settings
    $position_settings = $this->warp->config->get('warp.positions');

    // get module settings
    $module_settings = array();
    $settings = $this->xml->document->getElement('modulesettings');

    foreach ($settings->children() as $setting) {
        $module_settings[$setting->attributes('name')] = $setting;
    }

    echo $this->warp->template->render('admin/widget_options', compact('position_settings', 'module_settings'));
}

/*
    Function: getMenuItemOptions
        Retrieve menu by id

    Parameters:
        $id - Menu Item ID

    Returns:
        Array
*/
function getMenuItemOptions($id) {

    $menu_settings = array(
        'columns'     => 1,
        'columnwidth' => -1,
        'image'       => ''
    );

    if (isset($this->menu_item_options[$id])) {
        $menu_settings = array_merge($menu_settings, $this->menu_item_options[$id]);
    }

    return $menu_settings;
}


/*
    Function: _save_nav_settings
        Saves menu item settings

    Returns:
        Void
*/  
function _save_nav_settings() {

    if (isset($_POST['menu-item'])) {

        $menu_item_settings = $this->menu_item_options;

        foreach ($_POST['menu-item'] as $itemId=>$settings){
            $menu_item_settings[$itemId] = $settings;
        }

        update_option($this->prefix.'menu-items', $menu_item_settings);
        $this->menu_item_options = $menu_item_settings;
    }

    die();
}

/*
    Function: _get_nav_settings
        Returns menu item settings as json

    Returns:
        Boolean
*/
function _get_nav_settings() {
    die(json_encode($this->menu_item_options));
    }

}

    /*
        Function: mb_strpos
            mb_strpos function for servers not using the multibyte string extension
    */
    if (!function_exists('mb_strpos')) {
        function mb_strpos($haystack, $needle, $offset = 0) {
            return strpos($haystack, $needle, $offset);
        }
    }
Web Master
  • 4,240
  • 6
  • 20
  • 28
  • We need to see your code before we can help with this - specifically line 399 of `/home/mvprop/public_html/wp-content/themes/yoo_vox_wp/warp/systems/wordpress.3.0/helpers/system.php` and a few lines before it. – DaveRandom Jul 25 '12 at 15:33
  • related question: http://stackoverflow.com/questions/3584700/iterable-objects-and-array-type-hinting – feeela Jul 25 '12 at 15:45
  • For me it's very obvious in the linked forums where this codes belongs to and to which file is needs to get pasted: the same the error is in. It's also said, that "this could be a 'band-aid' fix, as may be part of a larger problem". Check the given file `yoo_vox_wp/warp/systems/wordpress.3.0/helpers/system.php` and compare its contents to the posted code. If you're not able to do so, hire a programmer. – feeela Jul 25 '12 at 15:50

1 Answers1

1

Basically what this warning message tells you is that the variable you're passing into your foreach is not an array or object. Make sure your variable is valid by testing it ( is_array($var) or is_object($var) ) or placing this block of code in a try-catch.

If $var is supposed to be an array, you should also initialize it just to be certain.

$var = Array();
.
. // code that may change the data type of $var
.
if (is_array($var)) {
    foreach($var as $v) {
        //code here
    }
}

From the manual at http://php.net/manual/en/control-structures.foreach.php:

The foreach construct provides an easy way to iterate over arrays. foreach works only on arrays and objects, and will issue an error when you try to use it on a variable with a different data type or an uninitialized variable.

Matt
  • 6,993
  • 4
  • 29
  • 50