#49255 closed defect (bug) (invalid)
It is not possible to register field for settings endpoint.
| Reported by: | vavra7 | Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | |
| Component: | REST API | Version: | 4.7 |
| Severity: | normal | Keywords: | needs-patch |
| Cc: | Focuses: | rest-api |
Description
Function register_rest_field() does not work for endpoint settings. Here is example code which is supposed to work but it doesn't. No field is added.
Used WordPress v5.3.2. No plugins.
<?php function add_field_show_on_front() { $args = [ 'get_callback' => function() { return get_option('show_on_front'); } ]; register_rest_field('setting', 'show_on_front', $args); } add_filter('rest_api_init', 'add_field_show_on_front');
Change History (2)
#1
@
7 years ago
- Milestone Awaiting Review
- Resolution → invalid
- Status new → closed
- Version 5.3.2 → 4.7
#2
@
7 years ago
Aha, I see now!
So this is what I need:
<?php function register_custom_settings() { $args = array( 'type' => 'string', 'default' => get_option('show_on_front'), 'show_in_rest' => true, ); register_setting('custom_settings', 'show_on_front', $args); } add_action('rest_api_init', 'register_custom_settings');
Thank you @TimothyBlynJacobs
Note:
See TracTickets
for help on using tickets.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)
Hi @vavra7,
Thanks for the ticket! This is intended behavior, if you want to add a setting to the settings controller, you should register the option using
register_settingand settingshow_in_resttotrue.For instance: