alexanderdejong.com

Wordpress Developer

  • Home
  • Blog
  • Projects
  • Contact
You are here: Home / Archives for unserialize

CMB2 Metabox Inline Multicheck Checkbox Tutorial

Posted on July 20, 2016 7 Comments

cmb2-webdev-studios

One of my latest repeated challenges was to add checkboxes again to the backend of WordPress. Having done it before, I knew what was expected but since it’s been awhile since I last coded metaboxes I had to look up some notes, blog posts and new software to use. I used to use Advanced Custom Fields, which is an amazing plugin. But, I think it’s an overkill for the functionality we need on this particular site. I needed something that would be able to load fast, has a small library and not too much UI. Also, I wanted to be able to inject it into WordPress as a dependency and not a plugin.

I found CMB2 for that, it had it all: calendar, text, email, checkbox, dropdown and more fields. So, I got to work. First by including it using composer and a “composer.json” file. I will discuss how that is done at a later period.

Generating a Multicheck Checkbox in the Admin of a Post Type

I started by using the following code in a dedicated php file to create my metaboxes. This one is for a hotelier to include the services they offer at each hotel. But it could really be anything (as always). You can just try it out and change the Metabox Title, post_type and ID to what you want. Here are some examples on GitHub to get your started: example-functions.php Or check out this Snippet Lirary.

add_action( 'cmb2_init', 'cmb2_add_metabox' );
function cmb2_add_metabox() {

	$prefix = '_cmb_';

	$cmb = new_cmb2_box( array(
		'id'           => $prefix . 'metabox',
		'title'        => __( 'Metabox Title', 'cmb2' ),
		'object_types' => array( 'post_type' ),
		'context'      => 'normal',
		'priority'     => 'high',
	) );

	$cmb->add_field( array(
		'id' => $prefix . 'hotel_amenities',
		'type' => 'multicheck_inline',
		'default' => '0',
		'options' => array(
			'Dry cleaning/laundry service ' => __( 'dry cleaning/laundry service ', 'cmb2' ),
			'24-hour front desk ' => __( '24-hour front desk ', 'cmb2' ),
			'Facilities for disabled guests' => __( 'facilities for disabled', 'cmb2' ),
			'airport_transportation' => __( 'airport transportation', 'cmb2' ),
		),
	) );

}

Generate the Checbox

To create the checkbox we just use $cmb->add_field and type out the type multicheck_inline with some options. That was really straightforward and easy. I could immediately understand why people like CMB2.

Here is an image with the result on the backend.
CMB2-Multicheck-Checbox-Tutorial-WordPress

Unserialize the Checkbox data

This is something I’m still experimenting with. If anyone can help me explain how unserialze works and how I can use it that would be awesome.

$custom_checkbox_group = unserialize($post_meta_data['_cmb_hotel_amenities'][0]);

echo $custom_checkbox_group['_cmb_hotel_amenities'][0];

Just a quick check on how Post Meta is being stored

Paste this code to test and seee on the front-end how the data is being stored into an array. I think unserialize has something to do with how our _cmb_hotel_amenities data is stored.

$test = get_post_meta( $post->ID, $custom_checkbox_group, true ) . '<br>';
echo '<pre>';
print_r($test);
echo '</pre>';

You can show it on the front-end like this and start playing around with the values

A foreach loop will go through each value and basically show it on the front-end.

<?php if ( $amenities = get_post_meta( get_the_ID(), '_cmb_hotel_amenities', true ) ) { 


?>
		<?php foreach ( $amenities as $amenity ) { ?>
			<?php echo $amenity; ?>
		<?php } ?>
<?php } ?>

You can add the following code (below) to add html to the data.

		
//remove spaces just in case
$meta = str_replace( '|', '<li>', $meta );
 
//split strings into array on commas
$meta = implode( '</li>', $meta );

echo $meta;
?>

Basically this goes through each item in the loop and shows each as a list item. Quite useful.

Another way to process the data on the front-end

In the end I went with the following way to show the data.

<h6 class="widget-title">Amenities</h6>
<ul>
<?php 
//get meta value as string
$meta = get_post_meta( get_the_ID(), '_cmb_hotel_amenities', true );
 
foreach ( $metas as $metas ) { 
	?>
	<li>
		<?php
			echo $metas; 
		?>
	<li>
	<?php
}		

Filed Under: WP Tutorials Tagged With: checkbox, cmb2, custom fields, foreach, get_post_meta, get_the_ID, implode, multicheck, str_replace, unserialize

Recent Posts

  • Migrating a WordPress Website Manually with Phpmyadmin, Search and Replace, DigitalOcean and Runcloud
  • How to add Total Price Calculation to your WooCommerce Product Page with AJAX
  • How to Install Roots Bedrock on a Digital Ocean Droplet running on Serverpilot via SSH
  • Linux SSH Useful Server Commands to Remember: Grep, Permissions and more.
  • How to Install Roots Sage 8.5.1 on a Digital Ocean Droplet running on Serverpilot via SSH

Find more content

Affiliate links
  • cheap shared hosting
  • low priced virtual servers only 5$
Affiliates
  • If someone buys a product then I get a commission.
  • You pay the same price using my affiliate links.
  • I can spend more time making newsletters and tutorials.
  • Thanks for visiting!

Connect with me

Recent Comments

  • Pedro Gómez on Migrating a WordPress Website Manually with Phpmyadmin, Search and Replace, DigitalOcean and Runcloud
  • Tim Delmeire on How to add Total Price Calculation to your WooCommerce Product Page with AJAX
  • Jaehyuk J Yang on How to add Total Price Calculation to your WooCommerce Product Page with AJAX
  • Alexander de Jong on A guide on customising the membership experience with Theme my Login (TML)
  • Judit Sarkany on A guide on customising the membership experience with Theme my Login (TML)

Recent Posts

  • Migrating a WordPress Website Manually with Phpmyadmin, Search and Replace, DigitalOcean and Runcloud
  • How to add Total Price Calculation to your WooCommerce Product Page with AJAX
  • How to Install Roots Bedrock on a Digital Ocean Droplet running on Serverpilot via SSH
  • Linux SSH Useful Server Commands to Remember: Grep, Permissions and more.
  • How to Install Roots Sage 8.5.1 on a Digital Ocean Droplet running on Serverpilot via SSH

Copyright © 2014 | Alexander de Jong | About | Contact | Privacy | Sitemap