#51390 closed defect (bug) (fixed)
PHP error: Trying to access array offset on value of type bool in /wp-admin/includes/meta-boxes.php on line 482
Reported by: |
|
Owned by: |
|
---|---|---|---|
Milestone: | 5.6 | Priority: | normal |
Severity: | normal | Version: | 5.5.1 |
Component: | Post Formats | Keywords: | has-unit-tests needs-testing |
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 (5)
Change History (26)
#1
@
4 years 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
#4
follow-up:
↓ 5
@
4 years 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
;
follow-up:
↓ 15
@
4 years 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 returningtrue
. 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' );
#7
@
4 years 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?
#8
@
4 years ago
- Keywords has-unit-tests added; needs-unit-tests removed
Actually, I have found a way to write the unit test.
#10
@
4 years 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.
This ticket was mentioned in Slack in #core by helen. View the logs.
4 years ago
#14
follow-up:
↓ 17
@
4 years ago
- Keywords has-patch commit removed
- Resolution fixed deleted
- Status changed from closed to reopened
After [49344], there is now some unexpected output in the unit test suite, as seen in this build for example:
Notice: add_theme_support( 'post-formats' ) was called <strong>incorrectly</strong>. You need to pass an array of types. Please see <a href="https://wordpress.org/support/article/debugging-in-wordpress/">Debugging in WordPress</a> for more information. (This message was added in version 5.6.0.) in /var/www/build/wp-includes/functions.php on line 5289
Cannot reproduce locally so far. Reopening for investigation.
#15
in reply to:
↑ 5
@
4 years ago
Replying to SergeyBiryukov:
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' );
Looks like there is actually a bug with that notice not being displayed as expected, see #51657.
This ticket was mentioned in Slack in #core by helen. View the logs.
4 years ago
#17
in reply to:
↑ 14
@
4 years ago
Replying to SergeyBiryukov:
After [49344], there is now some unexpected output in the unit test suite, as seen in this build for example:
Notice: add_theme_support( 'post-formats' ) was called <strong>incorrectly</strong>. You need to pass an array of types. Please see <a href="https://wordpress.org/support/article/debugging-in-wordpress/">Debugging in WordPress</a> for more information. (This message was added in version 5.6.0.) in /var/www/build/wp-includes/functions.php on line 5289
Cannot reproduce locally so far. Reopening for investigation.
I'm currently seeing this on my local MAMP install of trunk when running the full suite via grunt test
. Mine is in the same location;
https://wordpress.slack.com/archives/C02RQBWTW/p1603919117252900
#18
follow-up:
↓ 19
@
4 years ago
- Keywords needs-testing added
After investigation @SergeyBiryukov I found it was the add_theme_support( 'post-formats', array( 'post', 'page' ) );
call in the rest-search-controller.php
wpSetUpBeforeClass
. Uploaded patch 51390.5.diff that fixed for my local by introducing post and page as an array to the function call.
#19
in reply to:
↑ 18
@
4 years ago
Replying to garrett-eclipse:
After investigation @SergeyBiryukov I found it was the
add_theme_support( 'post-formats', array( 'post', 'page' ) );
call in therest-search-controller.php
wpSetUpBeforeClass
. Uploaded patch 51390.5.diff that fixed for my local by introducing post and page as an array to the function call.
Ah, that explains why the notice was only displayed when running the full suite and not when running the theme tests individually. Thanks for the patch!
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:
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?