Support

Account

Home Forums General Issues Repeater field in template breaks in PHP 8

Solving

Repeater field in template breaks in PHP 8

  • We’re developing a new site using PHP 7.4 and ACF 5.12.2. We’re using a Repeater “image” field to output images for a slideshow. We’re using the “Display a Slider” code example found here

    https://www.advancedcustomfields.com/resources/repeater/

    When we moved the site to PHP 8, the get_sub_field(‘image’) function returns nothing. Going back to PHP 7.4 and it returns the image array. Tried changing the return value to “image id” or “image url” – still nothing.

  • It is creating any errors in your error logs or on the site?

  • I have a similar problem it seems. I am using roughly:

    if( have_rows('related_items_list') ):
    			
     	// loop through the rows of data
        while ( have_rows('related_items_list') ) : the_row();
    		echo '<div class="related-item">';	
            // display a sub field value
            $postobject = get_sub_field('related_pages');
    
            echo '<div class="related-item-img" style="background-image: url('. $thumbnail  .')"><a href="'.get_permalink($postobject->ID).'" aria-hidden="true"></a></div>';
    		echo '<div class="related-details"><h3><a href="'.get_permalink($postobject->ID).'">'.$postobject->post_title.'</a></h3>';
    		echo '<div class="related">'.$postobject->post_excerpt.'</div>';
    		echo '</div></div>';	
    		
        endwhile;
    	wp_reset_postdata();
    endif;
    

    and the final error it throws is roughly:

    PHP Fatal error: Uncaught TypeError: strpos(): Argument #1 ($haystack) must be of type string, WP_Post given in .... /wp-includes/shortcodes.php:209

    it does this every time i try to use get_sub_field in a repeater.

    This all worked fine in PHP 7 and is failing in PHP 8

  • @margotseedsandstones-org there is nothing in your code that should be causing the error you are seeing. get_sub_field() does not cause the do_shortcode() function to be called nor is there anything in getting or formatting the value of a relationship field that would cause do_shortcodes() to be called.

    However, the relationship field could be returning an array rather than a single post and calling one of the sequestration WP functions with the wrong value might be causing the error. You should check what is returned here:

    
    $postobject = get_sub_field('related_pages');
    echo '<pre>'; print_r($postobject); echo '</pre>';
    
  • Hi John – good call.

    PHP Fatal error: Uncaught TypeError: Cannot access offset of type string on string in /mydirectory/wp-content/plugins/advanced-custom-fields-pro/pro/fields/class-acf-field-repeater.php:608

  • Thanks for the quick response. I tried that previously and got the same error, it never executes the print_r because the error is cause when i run get_sub_field.

    Definitely get_sub_field is returning an object. I can go into shortcodes.php and add a print_r and see the object, but only if i run it before lines 212 and and 221 (WP v5.9.3) where $content is evaluated as a string at various points and it fails.

    Like you say it seems unlikely that get_sub_field, which clearly returns an WP Object, should be getting do_shortcode run on it at all. but I can confirm that with no additional code, just me running $postobject = get_sub_field('related_pages'); i get the same error and that i can print_r from the do_shortcode function and see my object returned.

    I’m gonna dig into the ACF configuration and come back here to try describe how these fields relate, maybe i’ve stacked ACF features incorrectly. Also i’m gonna isolate this chunk of code from other wordpress loops etc and see if it’s somehow getting caught up in some other way so it gets run by do_shortcode but any insight deeply appreciated.

  • @mww I honestly don’t have a clue. There is too much going on with that line for me to figure out what part of it is causing the error or why it would cause an error in PHP 8. (I’m not an ACF dev).

    You might want to contact the devs on it.

    If it was me I would open that file and make changes and see if I could figure it out because it is the type of thing I would do. But I don’t have any sites running php PHP 8 yet.

    Since I now that it is causing an error I would use the error log

    this is the line of code causing the error

    
    $value[ $i ][ $sub_field['_name'] ] = $sub_value;
    

    and this is what I would do to start

    
    ob_start();
    echo '$i = ';
    var_dump($i);
    echo "\r\n".'$sub_field[_name] = ';
    var_dump($sub_field['_name']);
    echo "\r\n".'$sub_value = ';
    var_dump($sub_value);
    error_log(ob_get_clean());
    $value[ $i ][ $sub_field['_name'] ] = $sub_value;
    

    You now the last line is going to cause a fatal error, so you just need to find it in the log and then see what is in the log before that point. Figure out what part is causing the error and then go from there.

  • @margotseedsandstones-org you should probably contact ACF, I don’t see any reason that get_sub_field() should be causing the error you are getting.

  • Thanks John. I reported this as a bug using the contact form.

  • To put a conclusion on this for others who might come after me — embarrassingly it was me — seems i had historically enabled support for shortcodes in my custom fields using:

    // enables looking for shortcodes in all types of custom fields
    add_filter('acf/format_value', 'do_shortcode');

    There it was sitting in my functions.php but apparently this caused no trouble under php 7 and all the trouble under php 8.

    So i have scoped this filter to the few fields where I know i need it eg:

    // enables looking for shortcodes in all types of custom fields
    add_filter('acf/format_value/name=fancy_footer_content', 'do_shortcode');

    as clearly documented in https://www.advancedcustomfields.com/resources/acf-format_value/

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

You must be logged in to reply to this topic.