Thank you! Here’s all the code together to make it easy.
require_once __DIR__ . '/inc/cmb2/init.php';
add_action( 'init', 'create_vendor_post_type' );
function create_vendor_post_type () {
$singular = 'Vendor';
$plural = 'Vendors';
$slug = sanitize_title($singular);
$textdomain = get_stylesheet();
# Create Post Type
$post_labels = array(
"name" => __( $plural, $textdomain ),
"singular_name" => __( $singular, $textdomain ),
);
$post_args = array(
"label" => __( $plural, $textdomain ),
"labels" => $post_labels,
"description" => "",
"public" => true,
"publicly_queryable" => true,
"show_ui" => true,
"delete_with_user" => false,
"show_in_rest" => true,
"rest_base" => "",
"rest_controller_class" => "WP_REST_Posts_Controller",
"has_archive" => true,
"show_in_menu" => true,
"show_in_nav_menus" => true,
"exclude_from_search" => false,
"capability_type" => "post",
"map_meta_cap" => true,
"hierarchical" => true,
"rewrite" => array( "slug" => $slug, "with_front" => true ),
"query_var" => true,
"menu_position" => 90,
'menu_icon' => 'dashicons-groups',
"supports" => array( "title", "editor", "thumbnail", "excerpt", "custom-fields", "revisions", "author", "page-attributes", "post-formats", "comments" ),
);
register_post_type( $slug, $post_args );
# Create Category
$cat_labels = array(
"name" => __( "Categories", $textdomain ),
"singular_name" => __( "Category", $textdomain ),
);
$cat_args = array(
"label" => __( "Categories", $textdomain ),
"labels" => $cat_labels,
"public" => true,
"publicly_queryable" => true,
"hierarchical" => true,
"show_ui" => true,
"show_in_menu" => true,
"show_in_nav_menus" => true,
"query_var" => true,
"rewrite" => array( 'slug' => $slug.'_category', 'with_front' => true, ),
"show_admin_column" => false,
"show_in_rest" => true,
"rest_base" => $slug."_category",
"rest_controller_class" => "WP_REST_Terms_Controller",
"show_in_quick_edit" => false,
);
register_taxonomy( $slug."_category", array( $slug ), $cat_args );
}
add_action( 'cmb2_admin_init', 'sofw_vendor_metaboxes' );
function sofw_vendor_metaboxes() {
$sofw_vendor_post_box = new_cmb2_box( array(
'id' => 'sofw_vendor_metabox',
'title' => __( 'Create New Vendor', 'sofw-vendors' ),
'object_types' => array( 'vendor', ),
'context' => 'normal',
'priority' => 'high',
'show_names' => true,
'show_in_rest' => false,
) );
$sofw_vendor_post_box->add_field( array(
'name' => esc_html__( 'Email Address*', 'sofw-vendors' ),
'id' => 'email',
'type' => 'text_email',
'attributes' => array(
'required' => 'required',
),
) );
}
add_action( 'cmb2_vendor_process_fields_sofw_vendor_metabox', 'my_custom_code', 10, 2 );
function my_custom_code( $cmb2_obj, $cmb2_obj_id ) {
my_log( $cmb2_obj );
}
function my_log($log_msg) {
$log_time = date('Y-m-d h:i:sa');
$log_filename = get_stylesheet_directory()."/log";
if (!file_exists($log_filename))
{
// create directory/folder uploads.
mkdir($log_filename, 0777, true);
}
$log_file_data = $log_filename.'/log_' . date('d-M-Y') . '.log';
$log_file_data = $log_filename.'/log_debug' . '.log';
file_put_contents($log_file_data, date('d-M-Y G:i:s') . "\n\n" . print_r($log_msg, true) . "\n\n", FILE_APPEND);
}