[wp-trac] [WordPress Trac] #18285: New Settings API and workflow

WordPress Trac noreply at wordpress.org
Thu Sep 26 04:38:00 UTC 2013


#18285: New Settings API and workflow
--------------------------------------+-------------------------
 Reporter:  nacin                     |       Owner:
     Type:  feature request           |      Status:  closed
 Priority:  normal                    |   Milestone:
Component:  Plugins                   |     Version:
 Severity:  normal                    |  Resolution:  maybelater
 Keywords:  settings-api needs-patch  |
--------------------------------------+-------------------------

Comment (by CaptainN):

 I know this ticket is old. Here is a quick example of what WP_MetaForm can
 do:

 {{{#!php
 <?php
 $post_type = 'my_cpt';
 $post_meta_prefix = '_my_cpt_';

 add_action('init', function () use ( $post_type, $post_meta_prefix ) {

         $metaform = new WP_MetaForm(
                 array(
                         'prefix'    => $post_meta_prefix,
                         'label'     => 'Discount Info',
                         'post_type' => $post_type
                 ), 'My_Form_Callable'
         );

         $post_args = array(
                 'label'       => 'My CPT',
                 'labels'      => array(
                         'name'               => _x('My CPTs', 'Terminal
 Design My CPTs'),
                         'singular_name'      => _x('My CPT', 'post type
 singular name'),
                         'add_new'            => _x('Add New', 'book'),
                         'add_new_item'       => __('Add New My CPT'),
                         'edit_item'          => __('Edit My CPT'),
                         'new_item'           => __('New My CPT'),
                         'all_items'          => __('My CPTs'),
                         'view_item'          => __('View My CPT'),
                         'search_items'       => __('Search My CPTs'),
                         'not_found'          => __('No My CPTs found'),
                         'not_found_in_trash' => __('No My CPTs found in
 Trash'),
                         'parent_item_colon'  => 'My CPT: ',
                         'menu_name'          => 'My CPTs'
                 ),
                 'description'  => '',
                 'public'       => false,
                 'show_ui'      => true,
                 'show_in_menu' => true,
                 'hierarchical' => false,
                 'supports'     => array( 'title' ),
                 'rewrite'      => array( 'slug' => 'my-cpt' ),
                 'register_meta_box_cb' => array( &$metaform, 'metabox' )
         );

         register_post_type( $post_type, $post_args );
 });

 function My_Form_Callable() { ?>
 <div method="post">
 <label>Enter a custom value
 <input type="text" name="custom_key" required></label>
 <label>Enter another custom value
 <input type="text" name="another_custom_key"></label>
 </div>
 <?php
 }

 }}}

 That's all it takes to create a custom post type with a custom form in a
 meta box. The form defines the meta keys (one per input), and defines
 which fields are required, validates all the post data according to the
 declared validation rules (as much as is supported by OnceForm) and
 sanitizes the inputs (at least that's the idea - I don't have test
 coverage on that yet), and saves everything in to the appropriate place in
 the database without having to write a ton of boilerplate. IT also
 prefixes the meta keys if you want to use a prefix.

 That's a meta box demo, but I'd guess a Settings page could be done as
 easily.

 A big part of what it seems the Settings API does is just puts together
 HTML that matches the admin - but my sense is that would be much easier to
 just grab a template, edit in the fields you want, and then throw it at
 OnceForm. No need for complicated layout code, just write the HTML - much
 easier.

 If interested:
 https://github.com/adcSTUDIO/WP-OnceForm

--
Ticket URL: <http://core.trac.wordpress.org/ticket/18285#comment:105>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list