Skip to content

Instantly share code, notes, and snippets.

@mdailey77
Created June 12, 2018 19:06
Show Gist options
  • Save mdailey77/c7df65e1492c5becd80d794dd9604e2b to your computer and use it in GitHub Desktop.
Save mdailey77/c7df65e1492c5becd80d794dd9604e2b to your computer and use it in GitHub Desktop.
Register WordPress custom fields using the CMB2 PHP library
<?php
/**
* Include and setup custom metabox and fields.
*
* @category sitetheme
* @package Metaboxes
* @license http://www.opensource.org/licenses/gpl-license.php GPL v2.0 (or later)
* @link https://github.com/CMB2/CMB2
*/
/**
* Get the bootstrap! If using the plugin from wordpress.org, REMOVE THIS!
*/
if ( file_exists( dirname( __FILE__ ) . '/init.php' ) ) {
require_once dirname( __FILE__ ) . '/init.php';
}
// Custom Text Field - Pages
add_action('cmb2_admin_init', 'cmb_pages_customtext_metabox');
function cmb_pages_customtext_metabox() {
$prefix = '_sitetheme_';
$cmb_pages_customtext = new_cmb2_box( array(
'id' => 'customtext',
'title' => esc_html__( 'Custom Text', 'sitetheme' ),
'desc' => esc_html__( 'This will display in the right-hand sidebar', 'sitetheme' ),
'object_types' => array( 'page' ),
'context' => 'normal',
'priority' => 'high',
'show_on' => array('key'=>'page-template', 'value'=>'template-page-nosidebarwidgets.php'),
'show_names' => true, // Show field names on the left
));
$cmb_pages_customtext->add_field( array(
'name' => esc_html__( 'Title', 'sitetheme' ),
'id' => $prefix . 'customtext_title',
'type' => 'text'
));
$cmb_pages_customtext->add_field( array(
'name' => esc_html__( 'Content', 'sitetheme' ),
'id' => $prefix . 'customtext_content',
'type' => 'wysiwyg',
'sanitization_cb' => false,
));
}
// Marketo Embed URL - Resource
add_action('cmb2_admin_init', 'cmb_resources_webinar_metabox');
function cmb_resources_webinar_metabox() {
$prefix = '_sitetheme_';
$cmb_resources_webinar = new_cmb2_box( array(
'id' => 'resources_webinar_meta',
'title' => esc_html__( 'Embed URL', 'sitetheme' ),
'object_types' => array( 'resource' ), // Post type
'context' => 'normal',
'priority' => 'high',
'show_names' => true, // Show field names on the left
'show_on' => array(
'key' => 'taxonomy',
'value' => array(
'category' => array( 'webinars', 'whitepapers' )
)
)
));
$cmb_resources_webinar->add_field( array(
'name' => esc_html__( 'Embed Title', 'sitetheme' ),
'id' => $prefix . 'webinar_form_title',
'type' => 'text',
));
$cmb_resources_webinar->add_field( array(
'name' => esc_html__( 'Embed Description', 'sitetheme' ),
'id' => $prefix . 'webinar_form_description',
'type' => 'textarea_small',
));
$cmb_resources_webinar->add_field( array(
'name' => esc_html__( 'iFrame Embed', 'sitetheme' ),
'id' => $prefix . 'webinar_form',
'type' => 'wysiwyg',
'options' => array(
'media_buttons' => true
),
));
}
// Attachment Information - Resource
add_action('cmb2_admin_init', 'cmb_resources_attach_metabox');
function cmb_resources_attach_metabox() {
$prefix = '_sitetheme_';
$cmb_resources_attach = new_cmb2_box( array(
'id' => 'resources_meta',
'title' => esc_html__( 'Attachment Information', 'sitetheme' ),
'object_types' => array( 'resource' ), // Post type
'context' => 'normal',
'priority' => 'high',
'show_names' => true,
));
$cmb_resources_attach->add_field( array(
'name' => esc_html__( 'Video URL', 'sitetheme' ),
'id' => $prefix . 'video_url',
'type' => 'text'
));
$cmb_resources_attach->add_field( array(
'name' => esc_html__( 'Video Runtime', 'sitetheme' ),
'id' => $prefix . 'video_runtime',
'type' => 'text_small'
));
$cmb_resources_attach->add_field( array(
'name' => esc_html__( 'Case Study', 'sitetheme' ),
'id' => $prefix . 'file',
'desc' => esc_html__( 'Use this to upload your Case Study file.', 'sitetheme' ),
'type' => 'file'
));
$cmb_resources_attach->add_field( array(
'name' => esc_html__( 'Solution Brief', 'sitetheme' ),
'id' => $prefix . 'solution_brief',
'desc' => esc_html__( 'Use this to upload your Solution Brief file.', 'sitetheme' ),
'type' => 'file'
));
$cmb_resources_attach->add_field( array(
'name' => esc_html__( 'Data Sheet', 'sitetheme' ),
'id' => $prefix . 'data_sheet',
'desc' => esc_html__( 'Use this to upload your Data Sheet file.', 'sitetheme' ),
'type' => 'file'
));
$cmb_resources_attach->add_field( array(
'name' => esc_html__( 'External URL', 'sitetheme' ),
'id' => $prefix . 'marketo',
'desc' => esc_html__( 'Point the download link to an external URL. (optional)', 'sitetheme' ),
'type' => 'text'
));
$cmb_resources_attach->add_field( array(
'name' => esc_html__( 'Webinar Embed URL', 'sitetheme' ),
'id' => $prefix . 'webinar_embed',
'type' => 'text',
'options' => array(
'media_buttons' => false
),
));
$cmb_resources_attach->add_field( array(
'name' => esc_html__( 'Additional Information', 'sitetheme' ),
'id' => $prefix . 'additional_info',
'type' => 'wysiwyg',
'options' => array(
'media_buttons' => false
),
));
}
// Featured Text - Resource
add_action('cmb2_admin_init', 'cmb_resources_add_content_metabox');
function cmb_resources_add_content_metabox() {
$prefix = '_sitetheme_';
$cmb_resources_add_content = new_cmb2_box( array(
'id' => 'resources_add_content',
'title' => esc_html__( 'Additional Content', 'sitetheme' ),
'object_types' => array( 'resource' ),
'context' => 'normal',
'priority' => 'core',
'show_names' => true,
));
$cmb_resources_add_content->add_field( array(
'name' => esc_html__( 'Featured Text', 'sitetheme' ),
'id' => $prefix . 'resources_featured_text',
'type' => 'wysiwyg',
));
}
// Footer Information - Resource
add_action('cmb2_admin_init', 'cmb_resources_footer_metabox');
function cmb_resources_footer_metabox() {
$prefix = '_sitetheme_';
$cmb_resource_footer = new_cmb2_box( array(
'id' => 'resources_footer',
'title' => esc_html__( 'Footer Information', 'sitetheme' ),
'object_types' => array( 'resource' ), // Post type
'context' => 'normal',
'priority' => 'high',
'show_names' => true, // Show field names on the left
));
$cmb_resource_footer->add_field( array(
'name' => esc_html__( 'Twitter Widget Title', 'sitetheme' ),
'id' => $prefix . 'footer_widget_title',
'type' => 'text',
'std' => esc_html__( 'Latest Tweets', 'sitetheme' )
));
$cmb_resource_footer->add_field( array(
'name' => esc_html__( 'Twitter Username', 'sitetheme' ),
'id' => $prefix . 'footer_username',
'type' => 'text',
'std' => 'extremenetworks'
));
$cmb_resource_footer->add_field( array(
'name' => esc_html__( 'Twitter Search Term', 'sitetheme' ),
'id' => $prefix . 'footer_search',
'type' => 'text'
));
$cmb_resource_footer->add_field( array(
'name' => esc_html__( 'Tweet Count', 'sitetheme' ),
'id' => $prefix . 'footer_count',
'desc' => esc_html__( 'How many tweets should display.', 'sitetheme' ),
'type' => 'text_small',
'std' => 1
));
}
// Category Links - Partners Inner
add_action('cmb2_admin_init', 'cmb_partner_category_links_metabox');
function cmb_partner_category_links_metabox() {
$prefix = '_sitetheme_';
$cmb_category_links = new_cmb2_box( array(
'id' => 'category_links',
'title' => esc_html__( 'Category Links', 'sitetheme' ),
'object_types' => array( 'page' ),
'context' => 'normal',
'priority' => 'high',
'show_on' => array('key'=>'page-template', 'value'=>'template-partners-inner.php'),
'show_names' => true, // Show field names on the left
));
$cmb_category_links->add_field( array(
'name' => esc_html__( 'Category Link Title', 'sitetheme' ),
'id' => $prefix . 'category_link_title',
'type' => 'text'
));
$cmb_category_links->add_field( array(
'name' => esc_html__( 'Category Link Content', 'sitetheme' ),
'id' => $prefix . 'category_link_content',
'type' => 'wysiwyg'
));
}
// Product Details - Product
add_action('cmb2_admin_init', 'cmb_product_details_metabox');
function cmb_product_details_metabox() {
$prefix = '_sitetheme_';
$cmb_product_details = new_cmb2_box( array(
'id' => 'products_meta',
'title' => esc_html__( 'Product Details', 'sitetheme' ),
'object_types' => array( 'product' ),
'context' => 'normal',
'priority' => 'high',
'show_names' => true,
));
$cmb_product_details->add_field( array(
'name' => esc_html__( 'Product Name/Page Title', 'sitetheme' ),
'id' => $prefix . 'page_title',
'type' => 'text',
));
$cmb_product_details->add_field( array(
'name' => esc_html__( 'Product Name/Page Title Font Size', 'sitetheme' ),
'id' => $prefix . 'page_title_size',
'type' => 'select',
'options' => array(
'standard' => 'Standard (50px)',
'medium' => 'Medium (40px)',
'small-title' => 'Small (30px)',
)
));
$cmb_product_details->add_field( array(
'name' => esc_html__( 'Subtitle', 'sitetheme' ),
'id' => $prefix . 'subtitle',
'type' => 'text',
));
$cmb_product_details->add_field( array(
'name' => esc_html__( 'Intro Specs', 'sitetheme' ),
'id' => $prefix . 'intro',
'type' => 'wysiwyg',
));
$cmb_product_details->add_field( array(
'name' => esc_html__( 'Datasheet', 'sitetheme' ),
'id' => $prefix . 'datasheet',
'type' => 'file',
));
$cmb_product_details->add_field( array(
'name' => esc_html__( 'Overview', 'sitetheme' ),
'id' => $prefix . 'overview',
'type' => 'wysiwyg',
));
$cmb_product_details->add_field( array(
'name' => esc_html__( 'Features', 'sitetheme' ),
'id' => $prefix . 'features',
'type' => 'wysiwyg',
));
$cmb_product_details->add_field( array(
'name' => esc_html__( 'Benefits', 'sitetheme' ),
'id' => $prefix . 'benefits',
'type' => 'wysiwyg',
));
$cmb_product_details->add_field( array(
'name' => esc_html__( 'Specifications', 'sitetheme' ),
'id' => $prefix . 'specifications',
'type' => 'wysiwyg',
));
$cmb_product_details->add_field( array(
'name' => esc_html__( 'Call Out Title', 'sitetheme' ),
'id' => $prefix . 'call_out_title',
'type' => 'text',
));
$cmb_product_details->add_field( array(
'name' => esc_html__( 'Call Out Content', 'sitetheme' ),
'id' => $prefix . 'call_out_content',
'type' => 'textarea',
));
$cmb_product_details->add_field( array(
'name' => esc_html__( 'Call Out Button Text', 'sitetheme' ),
'id' => $prefix . 'call_out_button_text',
'type' => 'text',
));
$cmb_product_details->add_field( array(
'name' => esc_html__( 'Call Out Button Link', 'sitetheme' ),
'id' => $prefix . 'call_out_button_link',
'type' => 'text',
));
$cmb_product_details->add_field( array(
'name' => esc_html__( 'Product Guide Type', 'sitetheme' ),
'id' => $prefix . 'guide_type',
'type' => 'select',
'options' => array(
esc_html__( 'Hardware User Guide', 'sitetheme' ) => esc_html__( 'Hardware User Guide', 'sitetheme' ),
esc_html__( 'Software User Guide', 'sitetheme' ) => esc_html__( 'Software User Guide', 'sitetheme' )
)
));
$cmb_product_details->add_field( array(
'name' => esc_html__( 'Hardware/Software Guides', 'sitetheme' ),
'id' => $prefix . 'guide_files',
'type' => 'file_list'
));
$cmb_product_details->add_field( array(
'name' => esc_html__( 'Show From Our Blog', 'sitetheme' ),
'id' => $prefix . 'show_from_our_blog',
'type' => 'checkbox'
));
$cmb_product_details->add_field( array(
'name' => esc_html__( 'From Our Blog Category', 'sitetheme' ),
'id' => $prefix . 'from_our_blog_cat',
'desc' => esc_html__( 'Select a category from which posts will be displayed in the From Our Blog section.', 'sitetheme' ),
'type' => 'sitetheme_category_select',
));
}
// Custom HTML - Product, Resource
add_action('cmb2_admin_init', 'cmb_custom_html_metabox');
function cmb_custom_html_metabox() {
$prefix = '_sitetheme_';
$cmb_custom_html = new_cmb2_box( array(
'id' => 'sidebar_content',
'title' => esc_html__( 'Sidebar Content', 'sitetheme' ),
'object_types' => array( 'product', 'resource' ),
'context' => 'normal',
'priority' => 'high',
'show_names' => true,
));
$cmb_custom_html->add_field( array(
'name' => esc_html__( 'Custom HTML', 'sitetheme' ),
'id' => $prefix . 'custom_html',
'desc' => esc_html__( 'Add any custom HTML to this field such as iframe links or product categories', 'sitetheme' ),
'type' => 'wysiwyg',
));
}
// Case Study Details - Case Study
add_action('cmb2_admin_init', 'cmb_case_study_details_metabox');
function cmb_case_study_details_metabox() {
$prefix = '_sitetheme_';
$cmb_case_study_details = new_cmb2_box( array(
'id' => 'case_study_meta',
'title' => esc_html__( 'Case Study Details', 'sitetheme' ),
'object_types' => array( 'case_study' ),
'context' => 'normal',
'priority' => 'core',
'show_names' => true,
));
$cmb_case_study_details->add_field( array(
'name' => esc_html__( 'Project Highlights', 'sitetheme' ),
'id' => $prefix . 'highlights',
'type' => 'wysiwyg',
));
$cmb_case_study_details->add_field( array(
'name' => esc_html__( 'Case Study PDF', 'sitetheme' ),
'id' => $prefix . 'case_study_pdf',
'type' => 'file',
));
}
// Additional Content - Case Study
add_action('cmb2_admin_init', 'cmb_case_study_add_content_metabox');
function cmb_case_study_add_content_metabox() {
$prefix = '_sitetheme_';
$cmb_case_study_add_content = new_cmb2_box( array(
'id' => 'additional_content',
'title' => esc_html__( 'Additional Content', 'sitetheme' ),
'object_types' => array( 'case_study' ),
'context' => 'normal',
'priority' => 'core',
'show_names' => true,
));
$cmb_case_study_add_content->add_field( array(
'name' => esc_html__( 'Additional Highlights', 'sitetheme' ),
'id' => $prefix . 'add_highlights',
'type' => 'wysiwyg',
));
}
// Upcoming Webinar Widget - Case Study, Events, Resource
add_action('cmb2_admin_init', 'cmb_upcoming_webinar_metabox');
function cmb_upcoming_webinar_metabox() {
$prefix = '_sitetheme_';
$cmb_upcoming_webinar = new_cmb2_box( array(
'id' => 'upcoming_webinar',
'title' => esc_html__( 'Upcoming Webinar Widget', 'sitetheme' ),
'desc' => esc_html__( 'This will display in the left-hand sidebar', 'sitetheme' ),
'object_types' => array( 'case_study', 'events', 'resource' ),
'context' => 'normal',
'priority' => 'core',
'show_names' => true,
));
$cmb_upcoming_webinar->add_field( array(
'name' => esc_html__( 'Widget Title', 'sitetheme' ),
'id' => $prefix . 'webinar_title',
'type' => 'text',
));
$cmb_upcoming_webinar->add_field(array(
'name' => esc_html__( 'Select A Post', 'sitetheme' ),
'id' => $prefix . 'webinar_post',
'type' => 'sitetheme_events_posts',
));
$cmb_upcoming_webinar->add_field( array(
'name' => esc_html__( 'Description', 'sitetheme' ),
'id' => $prefix . 'webinar_description',
'type' => 'textarea',
));
$cmb_upcoming_webinar->add_field( array(
'name' => esc_html__( 'Link URL', 'sitetheme' ),
'id' => $prefix . 'webinar_url',
'type' => 'text',
));
$cmb_upcoming_webinar->add_field( array(
'name' => esc_html__( 'Link Text', 'sitetheme' ),
'id' => $prefix . 'webinar_link_text',
'type' => 'text',
));
}
// Speaker Information - Guest Speaker
add_action('cmb2_admin_init', 'cmb_speaker_info_metabox');
function cmb_speaker_info_metabox() {
$prefix = '_sitetheme_';
$cmb_speaker_info = new_cmb2_box( array(
'id' => 'speaker_information',
'title' => esc_html__( 'Speaker Information', 'sitetheme' ),
'object_types' => array( 'guest-speaker' ), // Post type
'context' => 'normal',
'priority' => 'high',
'show_names' => true, // Show field names on the left
));
$cmb_speaker_info->add_field( array(
'name' => esc_html__( 'Job Title', 'sitetheme' ),
'id' => $prefix . 'job_title',
'type' => 'text_medium'
));
$cmb_speaker_info->add_field( array(
'name' => esc_html__( 'Facebook URL', 'sitetheme' ),
'id' => $prefix . 'facebook',
'type' => 'text_medium'
));
$cmb_speaker_info->add_field( array(
'name' => esc_html__( 'LinkedIn URL', 'sitetheme' ),
'id' => $prefix . 'linkedin',
'type' => 'text_medium'
));
$cmb_speaker_info->add_field( array(
'name' => esc_html__( 'Twitter URL', 'sitetheme' ),
'id' => $prefix . 'twitter',
'type' => 'text_medium'
));
}
// Additional Information - Posts
add_action('cmb2_admin_init', 'cmb_post_additional_info_metabox');
function cmb_post_additional_info_metabox() {
$prefix = '_sitetheme_';
$cmb_post_additional_info = new_cmb2_box( array(
'id' => 'post_information',
'title' => esc_html__( 'Additional Information', 'sitetheme' ),
'object_types' => array( 'post' ), // Post type
'context' => 'normal',
'priority' => 'high',
'show_names' => true, // Show field names on the left
));
$cmb_post_additional_info->add_field( array(
'name' => esc_html__( 'Subtitle', 'sitetheme' ),
'id' => $prefix . 'subtitle',
'type' => 'text'
));
}
// News and Media Details - News and Media
add_action('cmb2_admin_init', 'cmb_news_media_details_metabox');
function cmb_news_media_details_metabox() {
$prefix = '_sitetheme_';
$cmb_news_media_details = new_cmb2_box( array(
'id' => 'news_and_media_meta',
'title' => esc_html__( 'News and Media Details', 'sitetheme' ),
'object_types' => array( 'news-and-media' ),
'context' => 'normal',
'priority' => 'low',
'show_names'=> true,
));
$cmb_news_media_details->add_field( array(
'name' => esc_html__( 'External Article Link', 'sitetheme' ),
'id' => $prefix . 'external_link',
'type' => 'text',
));
$cmb_news_media_details->add_field( array(
'name' => esc_html__( 'Press Release Location', 'sitetheme' ),
'id' => $prefix . 'release_loc',
'type' => 'text',
));
$cmb_news_media_details->add_field( array(
'name' => esc_html__( 'Press Release Date', 'sitetheme' ),
'id' => $prefix . 'release_date',
'type' => 'text',
));
}
// Contact Details - Pages
// appears after page is saved
add_action('cmb2_admin_init', 'cmb_contact_details_metabox');
function cmb_contact_details_metabox() {
$prefix = '_sitetheme_';
$cmb_contact_details = new_cmb2_box( array(
'id' => 'contact_info',
'title' => esc_html__( 'Contact Details', 'sitetheme' ),
'object_types' => array( 'page' ),
'show_on' => array( 'key' => 'page-template', 'value' => 'template-contact.php' ),
'context' => 'normal',
'priority' => 'high',
'show_names'=> true,
));
$cmb_contact_details->add_field( array(
'name' => esc_html__( 'Call Support Button Link', 'sitetheme'),
'id' => $prefix . 'call_support_link',
'type' => 'text',
));
$cmb_contact_details->add_field( array(
'name' => esc_html__( 'Call Support Button Text', 'sitetheme'),
'id' => $prefix . 'call_support_text',
'type' => 'text',
));
$cmb_contact_details->add_field( array(
'name' => esc_html__( 'Call Support Call Out Text', 'sitetheme'),
'id' => $prefix . 'call_support_callout_text',
'type' => 'text',
));
$cmb_contact_details->add_field( array(
'name' => esc_html__( 'Call Support Call Out Text Alignment', 'sitetheme' ),
'id' => $prefix . 'call_support_callout_alignment',
'type' => 'select',
'options' => array(
'left' => 'Left',
'center' => 'Center',
'right' => 'Right'
)
));
$cmb_contact_details->add_field( array(
'name' => esc_html__( 'Create Case Button Link', 'sitetheme'),
'id' => $prefix . 'create_case_link',
'type' => 'text',
));
$cmb_contact_details->add_field( array(
'name' => esc_html__( 'Create Case Button Text', 'sitetheme'),
'id' => $prefix . 'create_case_text',
'type' => 'text',
));
$cmb_contact_details->add_field( array(
'name' => esc_html__( 'Create Case Call Out Text', 'sitetheme'),
'id' => $prefix . 'create_case_callout_text',
'type' => 'text',
));
$cmb_contact_details->add_field( array(
'name' => esc_html__( 'Create Case Call Out Text Alignment', 'sitetheme' ),
'id' => $prefix . 'create_case_callout_alignment',
'type' => 'select',
'options' => array(
'left' => 'Left',
'center' => 'Center',
'right' => 'Right'
)
));
$cmb_contact_details->add_field( array(
'name' => esc_html__( 'iFrame Embed', 'sitetheme' ),
'id' => $prefix . 'contact_form',
'type' => 'wysiwyg',
'options' => array('media_buttons' => true ),
)
);
}
// Product Gallery - Products
add_action('cmb2_admin_init', 'cmb_product_gallery');
function cmb_product_gallery() {
$prefix = '_sitetheme_';
$cmb_product_gallery = new_cmb2_box( array(
'id' => 'product-gallery',
'title' => esc_html__( 'Product Image Gallery', 'sitetheme' ),
'object_types' => array( 'product' ),
'context' => 'side',
'priority' => 'low',
'show_names'=> false,
));
$cmb_product_gallery->add_field( array(
'name' => esc_html__( 'Product Gallery', 'sitetheme' ),
'id' => 'gallery',
'type' => 'file_list',
'preview_size' => array( 150, 150 ), // Default post thumbnail size
'query_args' => array( 'type' => 'image' ), // images only
// Optional, override default text strings
'text' => array(
'add_upload_files_text' => 'Add Image', // default: "Add or Upload Files"
),
) );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment