Setting Up Custom Post Types + MetaBoxes for Understraps

To set up Custom Post Types & Metaboxes for the Understrap Child template you need to do four things.

  1. Copy your cmb2 folder into vendors
  2. Add custom-metaboxes.php to inc folder in child directory
  3. Add custom-post-type.php to inc folder in child directory
  4. require new scripts in functions.php

1. Create the CMB2 folder

Go to the GitHub project for CMB2 and download the project.

Copy the following folders and files to the vendor folder in your child theme (you will need to create this if it doesn’t exist).

/css

/images

/includes

/js

/languages

bootstrap.php

index.php

init.php

2. Create custom-metaboxes.php

Copy the contents of example-functions.php and copy them into a new file: inc/custom-metaboxes.php

Edit lines 16 – 19 to redefine the path to include the vendors folder

if ( file_exists( dirname( __FILE__ ) . '/../vendors/cmb2/init.php' ) ) {
require_once dirname( __FILE__ ) . '/../vendors/cmb2/init.php';
} elseif ( file_exists( dirname( __FILE__ ) . '/../vendors/CMB2/init.php' ) ) {
require_once dirname( __FILE__ ) . '/../vendors/CMB2/init.php';

Copy and replace all instances of your_prefix with the project prefix.

3. Create the custom-post-type.php

The file needs to have the following functions and actions: A function and action to flush rewrite rules for custom post types, A function and action to register CPTs (using register_post_type() function), A function and action to define the metaboxes for each custom post type.

Use the example below as a starting point (don’t forget to find and replaceyour_prefix with the project prefix) :

<?php
/* Custom Post Type Example – stolen from bonestheme
This page walks you through creating
a custom post type and taxonomies. You
can edit this one or copy the following code
to create another one.

Developed by: Eddie Machado
URL: http://themble.com/bones/

Remember to replace ‘your_prefix_’ with theme name.

*/

// Flush rewrite rules for custom post types
add_action( ‘after_switch_theme’, ‘your_prefix_flush_rewrite_rules’ );

// Flush your rewrite rules
function your_prefix_flush_rewrite_rules() {
flush_rewrite_rules();
}

/*
Register my Custom Post Types
*/

function cpt_register() {
// SERVICES CUSTOM POST TYPE
// creating (registering) the custom type
register_post_type( ‘services’, /* (http://codex.wordpress.org/Function_Reference/register_post_type) */
array( ‘labels’ => array(
‘name’ => __( ‘Services’, ‘bc’ ), /* This is the Title of the Group */
‘singular_name’ => __( ‘Service’, ‘bc’ ), /* This is the individual type */
‘all_items’ => __( ‘All Services’, ‘bc’ ), /* the all items menu item */
‘add_new’ => __( ‘Add New’, ‘bc’ ), /* The add new menu item */
‘add_new_item’ => __( ‘Add New Service’, ‘bc’ ), /* Add New Display Title */
‘edit’ => __( ‘Edit’, ‘bc’ ), /* Edit Dialog */
‘edit_item’ => __( ‘Edit Service’, ‘bc’ ), /* Edit Display Title */
‘new_item’ => __( ‘New Service’, ‘bc’ ), /* New Display Title */
‘view_item’ => __( ‘View Service’, ‘bc’ ), /* View Display Title */
‘search_items’ => __( ‘Search Service’, ‘bc’ ), /* Search Custom Type Title */
‘not_found’ => __( ‘Nothing found in the Database.’, ‘bc’ ), /* This displays if there are no entries yet */
‘not_found_in_trash’ => __( ‘Nothing found in Trash’, ‘bc’ ), /* This displays if there is nothing in the trash */
‘parent_item_colon’ => ”
), /* end of arrays */
‘description’ => __( ‘Financial Services Offered’, ‘bc’ ), /* Custom Type Description */
‘public’ => true,
‘publicly_queryable’ => true,
‘exclude_from_search’ => false,
‘show_ui’ => true,
‘query_var’ => true,
‘menu_position’ => 15, /* this is what order you want it to appear in on the left hand side menu */
‘menu_icon’ => ‘dashicons-format-aside’, /* the icon for the custom post type menu */
‘rewrite’ => array( ‘slug’ => ‘services’, ‘with_front’ => false ), /* you can specify its url slug */
‘has_archive’ => ‘financial-services’, /* you can rename the slug here */
‘capability_type’ => ‘post’,
‘hierarchical’ => false,
/* the next one is important, it tells what’s enabled in the post editor */
‘supports’ => array( ‘title’, ‘editor’, ‘page-attributes’, ‘thumbnail’, ‘excerpt’, ‘author’,’revisions’)
) /* end of options */
); /* end of register post type */
}

// adding the function to the WordPress init
add_action( ‘init’, ‘cpt_register’);

/***********************************************
* Meta Boxes for SERVICES CPT
************************************************/

add_action( ‘cmb2_admin_init’, ‘your_prefix_register_services_metabox’ );
// Hook in and add a metabox. Can only happen on the ‘cmb2_admin_init’ or ‘cmb2_init’ hook.
// cmb2_admin_init is defined in bootstrap.php

function your_prefix_register_services_metabox() {
$prefix = ‘your_prefix_services_’;

$cmb_services = new_cmb2_box( array(
‘id’ => $prefix . ‘metabox’,
‘title’ => esc_html__( ‘Service Options’, ‘cmb2’ ),
‘object_types’ => array( ‘services’, ), // Post type
) );

$cmb_services->add_field( array(
‘name’ => esc_html__( ‘Icon’, ‘cmb2’ ),
‘desc’ => esc_html__( ‘Icon to be displayed on Services menu’, ‘cmb2’ ),
‘id’ => $prefix . ‘icon’,
‘type’ => ‘file’
) );

}

?>

 

4. Add require commands in functions.php

Open the child theme’s functions.php file and paste the following:

// Add Custom Meta Box
/**
* Load Custom Metabox infomation
*/
require get_stylesheet_directory() . ‘/inc/custom-metaboxes.php’;

/**
* Load Custom Post Type
*/
require get_stylesheet_directory() . ‘/inc/custom-post-type.php’;

Leave a Reply

Your email address will not be published. Required fields are marked *