Make WordPress Core

Opened 7 years ago

Closed 4 years ago

#42875 closed defect (bug) (fixed)

Registering an "array" setting for the REST API causes a "Undefined index: items" warning

Reported by: perrywagle's profile perrywagle Owned by: timothyblynjacobs's profile TimothyBlynJacobs
Milestone: 5.4 Priority: normal
Severity: normal Version: 4.9
Component: REST API Keywords: has-patch has-unit-tests
Focuses: docs Cc:


Getting the error:

Undefined index: items in /var/www/html/wp-includes/rest-api/endpoints/class-wp-rest-settings-controller.php on line 328

The context of that line is that the variable is determined to be an 'array', but then gets indexed by the string 'items', which is not in the apparently associative array of associative array (?).

Apparently, some people trigger this, and some don't.

Attachments (3)

42875.diff (4.5 KB) - added by TimothyBlynJacobs 5 years ago.
42875.2.diff (4.5 KB) - added by TimothyBlynJacobs 5 years ago.
42875.3.diff (4.6 KB) - added by TimothyBlynJacobs 4 years ago.

Download all attachments as: .zip

Change History (13)

#1 @perrywagle
7 years ago

Actually, with debugging on, it doesn't work.

#2 @perrywagle
7 years ago

AH. Its leaving out the

define( 'WP_DEBUG_DISPLAY', false );

that dies it (default true).

Seems seems to be a problem, just a different one.

Version 0, edited 7 years ago by perrywagle (next)

#3 @dshanske
7 years ago

I traced the issue, in discussions with @perrywagle to a plugin using register_setting to declare a type of 'array' and noting show_in_rest.

It says that valid types are 'string', 'boolean', 'integer', and 'number'. I don't recall seeing this when the feature was first extended. Either way, the addition of a function in 4.9 created this error if the type is not set to one of the above 4.

The simple solution is, if it is an unsupported type, it should generate an error about that.

#4 @TimothyBlynJacobs
5 years ago

  • Focuses docs added
  • Keywords has-patch has-unit-tests added
  • Milestone changed from Awaiting Review to 5.4
  • Owner set to TimothyBlynJacobs
  • Status changed from new to assigned
  • Summary changed from /var/www/html/wp-includes/rest-api/endpoints/class-wp-rest-settings-controller.php on line 328 to Registering an "array" setting for the REST API causes a "Undefined index: items" warning
  • Version changed from 4.9.1 to 4.9

See also which had the same issue.

To summarize, when registering an array setting, a defined "items" schema is required. I've uploaded a patch that mirrors the fix in #43392, except that it won't prevent the registration of the setting. Since this code is already in the wild, I don't think we can prevent the registration. This means you'll still get the PHP warning as well.

This ticket was mentioned in Slack in #core-restapi by timothybjacobs. View the logs.

5 years ago

#6 @kadamwhite
5 years ago

We say "meta" when we mean setting, in one comment on the latest patch; and for clarity it might be good to have a test that explicitly asserts we DON'T get a doing_it_wrong when we DO provide the items schema, though that may be covered implicitly by the other tests.

#7 @TimothyBlynJacobs
5 years ago

I uploaded a new patch that fixes the documentation error.

Opted not to add an additional test since we do have the test_get_item_with_custom_array_setting test and it isn't clear what the assertion would be for just testing the doing it wrong.

This ticket was mentioned in Slack in #core-restapi by kadamwhite. View the logs.

4 years ago

This ticket was mentioned in PR #144 on WordPress/wordpress-develop by TimothyBJacobs.

4 years ago

#10 @TimothyBlynJacobs
4 years ago

  • Resolution set to fixed
  • Status changed from assigned to closed

In 47325:

REST API: Warn if registering array settings without an items schema.

The documentation for register_setting has also been clarified to include all of the supported types and the flexibility of the show_in_rest parameter.

Fixes #42875.
Props perrywagle, dshanske, kadamwhite.

Note: See TracTickets for help on using tickets.