0

I've searched and have had trouble finding an answer.

tl;dr I have a wordpress theme and no longer have support from the creator, and I'm trying to get rid of warning text popping up on the top of some pages. I'm not a programmer so I'm not sure what to do but I do understand code enough to patch in a fix from somewhere else. I need help.

Here's what I get:

Warning: Illegal string offset 'group_no' in file1.php on line 196
Warning: Illegal string offset 'order_no' in file1.php on line 196
Warning: Illegal string offset 'group_no' in file1.php on line 200

Here's the relevant code for file1.php:

function get_location( $location, $post_id )
{
    // loaded by PHP already?
    if( !empty($location) )
    {
        return $location;
    }


    // vars
    $groups = array();
    $group_no = 0;


    // get all rules
    $rules = get_post_meta($post_id, 'rule', false);


    if( is_array($rules) )
    {
        foreach( $rules as $rule )
        {
            // if field group was duplicated, it may now be a serialized string!
            $rule = maybe_unserialize($rule);


            // does this rule have a group?
            // + groups were added in 4.0.4
            if( !isset($rule['group_no']) )
            {
                $rule['group_no'] = $group_no;

                // sperate groups?
                if( get_post_meta($post_id, 'allorany', true) == 'any' )
                {
                    $group_no++;
                }
            }


            // add to group
            $groups[ $rule['group_no'] ][ $rule['order_no'] ] = $rule; // this is line 196


            // sort rules
            ksort( $groups[ $rule['group_no'] ] );

        }

        // sort groups
        ksort( $groups ); //this is line 200
    }


    // return fields
    return $groups;
}

I understand that this is an array assignment error of some kind but I'm not familiar enough with php to understand what the error is and how to fix it. How would I rebuild this code to make the error go away while preserving function?

2 Answers2

0

I can't guarantee that this will fix your code's intended purpose, but it should get rid of the Illegal String Offset error:

<?php
// Lines 0..194
if (true === is_array($groups)
    && true === key_exists('group_no', $rule)
    && true === key_exists('order_no', $rule)
) {
    // original line 196
    $groups[ $rule['group_no'] ][ $rule['order_no'] ] = $rule; 
}

Here's an example of how it behaves when $groups is an array: https://eval.in/975271

Here's an example of how it behaves when $groups is a string: https://eval.in/975273

No error output in either!

Cameron Hurd
  • 4,836
  • 1
  • 22
  • 31
0

I don't know what make code exactly, but thinking outside box, I think that you could disable any/all WordPress and PHP alerts, because the messages what you get are simple Warnings, code can continue the execution although don't with behavior expected.

You can try add error_reporting(0) at file start or disable in php.ini directly. More information:

PD: I know this solution really don’t resolve code problem, but your goal at this moment is eliminate the messages and maintain normal page aspect.

LuisEduardox
  • 364
  • 4
  • 9