Posted by on in iDevelop, iG:Custom Metaboxes, PHP, WP-Plugins | 0 comments

This is something that I’ve been meaning to do for a while but didn’t get around to it till recently, or rather, mid November last year. I wanted a neat, clean, Object Oriented way to create metaboxes in WordPress, which would be light & fast, and yet flexible enough to allow override on both UI render and data validation/sanitization. The other requirement was that I was looking to over-haul some old code that we’ve been using at PMC (my employer), so naturally the code has to be WordPress.com VIP compliant as pretty much all our sites are hosted there. And because I’ve the uncurable itch to roll my own thing, I just had to do it! 😉

So without further ado, let’s see how this new thing works. WordPress starts creating metaboxes on add_meta_boxes action, so calling this API and enqueuing your metaboxes anytime before add_meta_boxes is run will be good enough.

php
my-plugin.php

  1. $metabox = new iG\Metabox\Metabox( 'my-metabox-1' );
  2.  
  3. $metabox->set_title( 'My 1st Metabox' )
  4.         ->set_context( 'normal' )
  5.         ->set_priority( 'default' )
  6.         ->set_css_class( 'my-1st-metabox' )
  7.         ->set_post_type( 'post' )
  8.         ->set_post_type( 'custom-post-type' )
  9.  
  10.         // Call set_post_type() for each post type or
  11.         // pass them all as array to set_post_types()
  12.         ->set_post_types( array(
  13.             'custom-post-type-2', 'custom-post-type-3'
  14.         ) )
  15.         ->add_field(    //add a simple text input field
  16.             iG\Metabox\Text_Field::create( 'my-txt-1', 'My 1st Text Field 1' )
  17.                             ->set_description( 'Some Desc for 1st Field' )
  18.                             ->set_css_class( 'my-txt-css-1' )
  19.                             ->set_placeholder( 'Enter the first text here' )
  20.                             ->set_size( 50 )
  21.         )
  22.         ->add_field(    //add a checkbox toggle
  23.             iG\Metabox\Checkbox_Field::create( 'my-toggle-1', 'Flag Toggle' )
  24.                                 ->set_description( '1st Toggle Switch' )
  25.                                 ->set_value( 'yes' )
  26.         )
  27.         ->add_field(    //add a HTML5 number input field
  28.             iG\Metabox\Number_Field::create( 'my-num-1', 'My Number Field A' )
  29.                                 ->set_description( 'Some Desc for 1st Number Field' )
  30.  
  31.                                 // Call set_css_class() for each class or
  32.                                 // pass them all as array to
  33.                                 // set_css_classes()
  34.                                 ->set_css_classes( array(
  35.                                     'my-num-fld-1', 'x-custom-flds'
  36.                                 ) )
  37.                                 ->set_placeholder( 'Enter the a number here' )
  38.  
  39.                                 // Make numbers incrementable or
  40.                                 // reducable by 2.5
  41.                                 ->set_step( 2.5 )
  42.         )
  43.         ->add_field(    //add a radio button group
  44.             iG\Metabox\Radio_Group::create( 'my-rdg-1', 'My Radio Group A' )
  45.                                 ->set_description( 'Some Desc for 1st Radio Group' )
  46.                                 ->set_css_class( 'my-rdg-css-1' )
  47.                                 ->set_values( array(
  48.                                     'apple' => 'Apple',
  49.                                     'grape' => 'Grape',
  50.                                     'banana' => 'Banana',
  51.                                     'orange' => 'Orange',
  52.                                     'peach' => 'Peach',
  53.                                 ) )
  54.         )
  55.         ->add();

This code, when run, will create a new metabox named My 1st Metabox on the add/edit screens of post types post, custom-post-type, custom-post-type-2 & custom-post-type-3 in wp-admin containing a text input field, a checkbox to act as toggle switch for a flag, a HTML5 number field and a radio button group. Besides rendering this whole UI, the library would also save all these values in post-meta of the said post (when the post is saved) and those values would be retreivable using the normal get_post_meta() API call of WordPress.

Here’s an example:

php
my-plugin.php

  1. $txt_field = get_post_meta( $post_id, 'my-txt-1', true );
  2. $cb_toggle_field = get_post_meta( $post_id, 'my-toggle-1', true );
  3. $number_field = get_post_meta( $post_id, 'my-num-1', true );
  4. $radio_field = get_post_meta( $post_id, 'my-rdg-1', true );

Its just as simple, no fuss no muss. Each field type has its own class which inherits the iG\Metabox\Field abstract class. So the library follows the Single Responsibility principle whereby every class has only one focus & a single class does not try to do everything.

Here are some more tid-bits:

  • Every field API has set_render_callback() to which you can pass a callback which would be responsible for rendering the HTML for that field. Your callback would have to return the HTML for the field and not echo it out.
  • Every field API has set_sanitize_callback() to which you can pass a callback which would be responsible for sanitizing the data of that field. You can set different parameters to be passed to your callback by passing them in an array as second parameter of set_sanitize_callback() and all those would be passed to your callback in same order from second parameter onwards, ie., first parameter passed to your callback would always be field data that is to be sanitized and then your additional callback parameters would be passed. Your callback would have to return the sanitized data back for the field class to save it in post meta.

Looks good?

You can grab it from:

Pull requests are welcome on Github. :tup: