WordPress.org

Make WordPress Core

Opened 4 weeks ago

Last modified 2 weeks ago

#51390 accepted defect (bug)

PHP error: Trying to access array offset on value of type bool in /wp-admin/includes/meta-boxes.php on line 482

Reported by: sproutchris Owned by: SergeyBiryukov
Milestone: 5.6 Priority: normal
Severity: normal Version: 5.5.1
Component: Post Formats Keywords: has-patch has-unit-tests commit
Focuses: Cc:

Description

I got an error in my debug log (running PHP 7.4):

Trying to access array offset on value of type bool in .../wp-admin/includes/meta-boxes.php on line 482

The code in question is:

...
$post_formats = get_theme_support( 'post-formats' );
if ( is_array( $post_formats[0] ) ) :
...

$post_formats equals false in my scenario. I haven't added support for "post-formats" in my theme (because I don't want it in my theme) so is this is expected behavior or is this maybe in itself what's going wrong here?

When WordPress then next asks PHP if $post_formats[0] is an array (is_array( $post_formats[0] )) when $post_formats is a boolean (therefore having no array items/indices), obviously PHP is going to throw this error. If get_theme_support() returning a boolean instead of an array is expected behavior, then I guess the easy fix would be:

if ( isset( $post_formats[0] ) && is_array( $post_formats[0] ) ) :

Attachments (4)

51390.1.patch (525 bytes) - added by Mista-Flo 3 weeks ago.
Patch without unit tests
51390.2.patch (1.1 KB) - added by Mista-Flo 3 weeks ago.
Patch with unit test
51390.3.patch (1.6 KB) - added by Mista-Flo 3 weeks ago.
Improve doc
51390.4.diff (1.5 KB) - added by garrett-eclipse 2 weeks ago.
Minor refresh to make the comment a sentence.

Download all attachments as: .zip

Change History (14)

#1 @sproutchris
4 weeks ago

  • Summary changed from Trying to access array offset on value of type bool in /var/www/realestate.columbian.com/htdocs/wp-admin/includes/meta-boxes.php on line 482 to PHP error: Trying to access array offset on value of type bool in /wp-admin/includes/meta-boxes.php on line 482

#2 @sabernhardt
4 weeks ago

  • Component changed from General to Post Formats

#3 @Mista-Flo
3 weeks ago

  • Keywords reporter-feedback added

Hi there, thanks for your report.

It's a bit bizarre because you said your theme does not support post formats, but the line just before line 142 is:

	if ( current_theme_supports( 'post-formats' ) && post_type_supports( $post->post_type, 'post-formats' ) ) :

So it should not go further in this if state if you have not enabled theme_supports for post-formats.

It seems to be an edge-case with thee theme, can you give more information about your theme or how to reproduce the issue easily?

#4 follow-up: @sproutchris
3 weeks ago

I'm assuming by line 142 that you're actually referring to line 479.

So I apologize, my theme does in fact support post formats. I missed the line ( add_theme_support('post-formats') ) when reading through my functions.php file. So this issue is occurring when the post formats are enabled for the theme.

I switched to twenty seventeen theme (that theme has post-formats theme support enabled) and the post formats meta box works fine. It is just in my custom theme that this error is occurring.

Instead of an array, get_theme_support('post-formats') on line 480 is returning true. Any idea what could cause that to happen?

#5 in reply to: ↑ 4 @SergeyBiryukov
3 weeks ago

  • Keywords needs-patch added; reporter-feedback removed
  • Milestone changed from Awaiting Review to 5.6
  • Owner set to SergeyBiryukov
  • Status changed from new to accepted

Replying to sproutchris:

Instead of an array, get_theme_support('post-formats') on line 480 is returning true. Any idea what could cause that to happen?

As noted in the HelpHub and the Theme Developer Handbook, adding post formats support is supposed to include an array of supported formats:

add_theme_support( 'post-formats', array( 'aside', 'gallery' ) );

So this usage is incorrect (previously discussed in #15443 and decided against):

add_theme_support( 'post-formats' );

What happens is that the second argument of add_theme_support() defaults to true if not specified.

We should probably add a _doing_it_wrong() notice similar to the one displayed for 'html5' support:

_doing_it_wrong( "add_theme_support( 'html5' )", __( 'You need to pass an array of types.' ), '3.6.1' );

#6 @SergeyBiryukov
3 weeks ago

  • Keywords needs-unit-tests added

@Mista-Flo
3 weeks ago

Patch without unit tests

#7 @Mista-Flo
3 weeks ago

  • Keywords has-patch added; needs-patch removed

Hi, I have uploaded a patch despite I'm unable to have working unit tests for it, any idea how to do it?

@Mista-Flo
3 weeks ago

Patch with unit test

#8 @Mista-Flo
3 weeks ago

  • Keywords has-unit-tests added; needs-unit-tests removed

Actually, I have found a way to write the unit test.

#9 @sproutchris
3 weeks ago

Thanks a lot, guys.

@Mista-Flo
3 weeks ago

Improve doc

@garrett-eclipse
2 weeks ago

Minor refresh to make the comment a sentence.

#10 @garrett-eclipse
2 weeks ago

  • Keywords commit added

Thanks @Mista-Flo this tests well and unit tests are passing, moving forward for a committer review.

In 51390.4.diff the only change was adding the missing period to the comment sentence in test_post_format_doing_it_wrong.

Note: See TracTickets for help on using tickets.