[wp-trac] [WordPress Trac] #28672: add_settings_field & co don't return values

WordPress Trac noreply at wordpress.org
Sat Jan 17 17:03:10 UTC 2015


#28672: add_settings_field & co don't return values
---------------------------------------------+-----------------------------
 Reporter:  TJNowell                         |       Owner:
     Type:  enhancement                      |      Status:  new
 Priority:  normal                           |   Milestone:  Awaiting
Component:  Options, Meta APIs               |  Review
 Severity:  normal                           |     Version:  2.7
 Keywords:  needs-unit-tests good-first-bug  |  Resolution:
                                             |     Focuses:  administration
---------------------------------------------+-----------------------------

Comment (by GunGeekATX):

 Been digging into this more and it's going to be a little more complex
 than just checking $_registered_pages.

 Settings sections and fields can be added to existing pages (general,
 media, reading, etc) and those don't show up in $_registered_pages.  New
 pages can be added via add_menu_page() and add_submenu_page(), and those
 do show up in $_registered_pages, but the format is not as simple as the
 page name that gets passed to do_settings_sections().

 Some examples from $_registered_pages while testing locally; adding a top-
 level page, adding a sub-menu page to the top-level page, and adding a
 sub-menu page to the Dashboard menu:

 {{{
 array(6) {
   ["toplevel_page_new-menu-page"]=>
   bool(true)
   ["new-menu-page_page_new-submenu-page"]=>
   bool(true)
   ["dashboard_page_new-dashboard-page"]=>
   bool(true)
   ["appearance_page_custom-header"]=>
   bool(true)
   ["appearance_page_custom-background"]=>
   bool(true)
   ["appearance_page_theme-editor"]=>
   bool(true)
 }
 }}}

 However, it may be as simple as checking to see if the page is one of the
 built-in pages (general, media, etc), or if any of the keys in
 $_registered_pages end with the page.  So far, the following addition to
 add_settings_section() seems to work:


 {{{
 // check to see if the page is one of the built-in pages
 $settings_page_check = in_array( $page, array( 'discussion', 'general',
 'media', 'permalink', 'reading', 'writing' ) );

 // if it's not a built-in page, check to see if the page matches the end
 of any of the keys for the registered pages
 if ( ! $settings_page_check ) {
         foreach ( array_keys( $_registered_pages ) as $registerd_page_name
 ) {
                 if ( substr_compare( $registerd_page_name, $page, strlen(
 $registerd_page_name ) - strlen( $page ), strlen( $page ) ) === 0 ) {
                         $settings_page_check = true;
                         break;
                 }
         }
 }

 $wp_settings_sections[$page][$id] = array('id' => $id, 'title' => $title,
 'callback' => $callback);

 if ( ! $settings_page_check )
         return new WP_Error( 'invalid_settings_page', sprintf(
 __('Settings page %s does not exist.'), $page ) );
 else
         return $settings_page_check;
 }}}

 Is this something we'd want to make new functions for,
 settings_page_exists() (and settings_section_exists())?

--
Ticket URL: <https://core.trac.wordpress.org/ticket/28672#comment:12>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list