Doing Metaboxes in WordPress the easy way

Adding Metaboxes in WordPress to your admin areas normally involves 3 steps:

  1. Action to define the metabox area
  2. Callback to define the metabox content
  3. Action to handle the metabox updates / saves

This can be done much easier using Custom Metaboxes and Fields for WordPress, which is a metabox, custom fields and forms library. The library provides a bunch of custom fields and also allows you to add your own easily. Much easier to build new Metaboxes fast!

List of field types included:

  1. title An arbitrary title field *
  2. text
  3. text_small
  4. text_medium
  5. text_email
  6. text_url
  7. text_money
  8. textarea
  9. textarea_small
  10. textarea_code
  11. text_date Date Picker
  12. text_time Time picker
  13. select_timezone Time zone dropdown
  14. text_date_timestamp Date Picker (UNIX timestamp)
  15. text_datetime_timestamp Text Date/Time Picker Combo (UNIX timestamp)
  16. text_datetime_timestamp_timezone Text Date/Time Picker/Time zone Combo (serialized DateTime object)
  17. colorpicker Color picker
  18. radio *
  19. radio_inline *
  20. taxonomy_radio *
  21. taxonomy_radio_inline *
  22. select
  23. taxonomy_select *
  24. checkbox *
  25. multicheck
  26. taxonomy_multicheck *
  27. taxonomy_multicheck_inline
  28. wysiwyg (TinyMCE) *
  29. file Image/File upload *†
  30. file_list Image/File list upload
  31. oembed Converts oembed urls (instagram, twitter, youtube, etc. oEmbed in the Codex)
  32. group Hybrid field that supports adding other fields as a repeatable group. *

Simple example from the CMB2 Github page

  1. if ( file_exists(  __DIR__ . '/cmb2/init.php' ) ) {
  2.   require_once  __DIR__ . '/cmb2/init.php';
  3. } elseif ( file_exists(  __DIR__ . '/CMB2/init.php' ) ) {
  4.   require_once  __DIR__ . '/CMB2/init.php';
  5. }
  6.  
  7. add_action( 'cmb2_init', 'cmb2_sample_metaboxes' );
  8. /**
  9.  * Define the metabox and field configurations.
  10.  */
  11. function cmb2_sample_metaboxes() {
  12.  
  13.     // Start with an underscore to hide fields from custom fields list
  14.     $prefix = '_yourprefix_';
  15.  
  16.     /**
  17.      * Initiate the metabox
  18.      */
  19.     $cmb = new_cmb2_box( array(
  20.         'id'            => 'test_metabox',
  21.         'title'         => __( 'Test Metabox', 'cmb2' ),
  22.         'object_types'  => array( 'page', ), // Post type
  23.         'context'       => 'normal',
  24.         'priority'      => 'high',
  25.         'show_names'    => true, // Show field names on the left
  26.         // 'cmb_styles' => false, // false to disable the CMB stylesheet
  27.         // 'closed'     => true, // Keep the metabox closed by default
  28.     ) );
  29.  
  30.     // Regular text field
  31.     $cmb->add_field( array(
  32.         'name'       => __( 'Test Text', 'cmb2' ),
  33.         'desc'       => __( 'field description (optional)', 'cmb2' ),
  34.         'id'         => $prefix . 'text',
  35.         'type'       => 'text',
  36.         'show_on_cb' => 'cmb2_hide_if_no_cats', // function should return a bool value
  37.         // 'sanitization_cb' => 'my_custom_sanitization', // custom sanitization callback parameter
  38.         // 'escape_cb'       => 'my_custom_escaping',  // custom escaping callback parameter
  39.         // 'on_front'        => false, // Optionally designate a field to wp-admin only
  40.         // 'repeatable'      => true,
  41.     ) );
  42.  
  43. }

 

Alex

I am a full-stack developer. I love programming,  design and know my way around server architecture as well.  I would never feel complete, with one of these missing. I have a broad range of interests, that’s why I constantly dive into new technologies and expand my knowledge where ever required. Technologies are evolving fast and I enjoy using the latest. Apart from that, I am a peace loving guy who tries to have people around him that think the same.  I truly believe in the principle: “If you help someone, someone will help you, when you need it."

Recent Posts

B&B / Hotel Booking Solutions for WordPress | 2024

BOOKING SOLUTIONS 202x This is my take on a subset of booking, appointment, PMS or… Read More

4 weeks ago

WordPress Cron + WP-CLI + Ntfy

THE GOAL Create a system cron for WordPress, that is accessible and can be easily… Read More

2 months ago

2024 is here and now :)

2024, what's cooking? Slowly getting into the 2024 spirit. 3 projects coming to a close… Read More

4 months ago

2023 ends and whats next !

Short look back at 2023 This has been a busy and interesting year. I am… Read More

4 months ago

cubicFUSION Grid Tweaker – Elementor Grid made easy.

Elementor Pro provides grid containers as an experimental feature. The options provided are limited, when… Read More

5 months ago

Archaeology Travel Booth – Travel Innovation Summit 2023

Archaeology Travel is an online travel guide for people who enjoy exploring the world’s pasts.… Read More

6 months ago