Support

Account

Home Forums Add-ons Repeater Field Reapeater field problem with PHP 8

Solved

Reapeater field problem with PHP 8

  • Hello there.
    After updating WordPress’s official Docker image to PHP 8.0, I faced this problem:

    Fatal error: Uncaught Error: Cannot access offset of type string on string
    in /var/www/html/wp-content/plugins/advanced-custom-fields-pro/pro/fields/class-acf-field-repeater.php on line 412

    Here is the full call trace:

    acf_field_repeater::format_value()
    wp-includes/class-wp-hook.php:308
    
    WP_Hook::apply_filters()
    wp-includes/plugin.php:256
    
    apply_filters_ref_array()
    wp-content/plugins/advanced-custom-fields-pro/includes/acf-hook-functions.php:109
    
    _acf_apply_hook_variations()
    wp-includes/class-wp-hook.php:308
    
    WP_Hook::apply_filters()
    wp-includes/plugin.php:205
    
    apply_filters()
    wp-content/plugins/advanced-custom-fields-pro/includes/acf-value-functions.php:172
    
    acf_format_value()
    wp-content/plugins/advanced-custom-fields-pro/includes/api/api-template.php:51
    
    get_field()
    wp-content/plugins/XX/XX/XX.php:26

    I traced the stack trace and turned out the problem is get_field() method that I’m using like this (on line 26):
    $calls = get_field('call', $post->ID);

    It was working nice with PHP7.4 for almost a year. And also the problem solved when I revert the Docker image version to 7.4.

    WP Version: 6.1.1
    ACF Version: 6.0.5 Pro
    All the plugins and themes are updated.

  • I’ve looked at this but I can’t see what is causing the error, at a guess I would say it has something to do with the new repeater admin pagination. Try turning off pagination in the repeater settings and see if that corrects the problem.

    You should probably contact the developers either here or by logging into your account and submitting a ticket.

  • Pagination was off. I turned it on, no luck, back to off, still no luck.

    I searched the forum and found another unsolved topic, as same as my problem here.

  • I honestly don’t have any idea, this is an issue that needs to be submitted to the developers.

  • I would say that this error is not caused by acf.
    The problem here is that in PHP 8, this error used to throw a ‘warning’ but now it’s a fatal error.
    Do you get a valid value when you var_dump the return from get_field?

  • @kau I generally develop with error reporting on so that I can see all warnings and errors and I have never seen this line throw a warning.

    @sina_saeedi82 I’d like to test, can you give me a list of the field types you’re using in your repeater

  • @hube2

    typically its something like this:

    $array = array('label' => 'value');
    $string = "value";
    
    var_dump($array['label']);
    var_dump($string['label']);

    php >= 8.0 “Cannot access offset of type string on string”
    php < 8.0 “Warning: Illegal string offset”

    @sina_saeedi82 could you share more code?

  • @kau no luck with var_dump, same exception.

  • This reply has been marked as private.
  • The repeater field you attached is named “not_answered_call” the code in your OP is getting a field named “call”

    
    $calls = get_field('call', $post->ID);
    

    Are you getting the correct field name in your actual code?

  • The ‘call’ field has many sub fields, but ‘not_answered_call’ has not.

    I’ve tested and both has the same problem, so I sent you the little one.

  • I cannot recreate the error.

    If you can do some debugging for me.

    Open the file /advanced-custom-fields-pro/pro/fields/class-acf-field-repeater.php

    just before line 412 add

    
    echo '<!-- ';var_dump($i);var_dump($sub_field);var_dump($sub_value);echo ' -->';
    

    Load the page, look at the page source just before the error and tell me what appears in the comment added to the page.

  • This reply has been marked as private.
  • One more test

    Remove the code you added last time and near the top of the function on line 382 add

    
    echo '<!-- '; var_dump($value); echo ' -->';
    

    Same thing, let me know what is dumped just before the error.

  • array(1) {
      [0]=>
      string(5) "Array"
    }
  • Something is altering $value in a way that breaks it. This is likely being caused be an acf/load_value filter or an acf/format_value filter that is running before the built the built in acf/format value filter for the repeater is running.

    You need to find filters in your theme and see if one of them is causing this. Also, I noticed from your field group that you are using ACF Extented. The cause could be something in that plugin, I would try deactivating it and see if that clears up the issue.

  • @hube2 thank you very much!

    I found it. I have these:

    add_filter('acf/load_value', array($this, 'fixArabic'), 1000);
    add_filter('acf/pre_save_post', array($this, 'fixArabic'), 1000);

    Their job is to replace some Arabic characters to Persian characters.

    Before:

    public function fixArabic($content)
        {
            if (!is_a($content, 'DateTime')) {
                return str_replace(array('ي', 'ك', '٤', '٥', '٦', 'ة'), array('ی', 'ک', '۴', '۵', '۶', 'ه'), $content);
            }
    
            return $content;
        }

    After:

    public function fixArabic($content)
        {
            if (!is_a($content, 'DateTime') && !is_array($content)) {
                return str_replace(array('ي', 'ك', '٤', '٥', '٦', 'ة'), array('ی', 'ک', '۴', '۵', '۶', 'ه'), $content);
            }
    
            return $content;
        }

    The problem was when the $content is an array so && !is_array($content) solved the problem.

    Thank you again.

    But do you have any ideas why it was working with PHP7.4 and not with PHP8.0?

  • basically, as I understand it, PHP 7 lets you do something like this

    
    // create a value that is a string
    $value = 'ABCD';
    
    // use the above variable and replace it with an array
    // this automatically unsets the variable and creates an array
    $value[] = 'ABCD'
    

    PHP 8 does not allow this. You would first need to reinitialize the variable to an array

    
    // set a variable with a string value
    $value = 'ABCD';
    
    // set the var to an array
    $value = array();
    // set at array var
    $value[] = 'ABCD'
    
  • Thanks John for your amazing support.

Viewing 19 posts - 1 through 19 (of 19 total)

You must be logged in to reply to this topic.