-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions-cmb2.php
More file actions
119 lines (110 loc) · 3.63 KB
/
functions-cmb2.php
File metadata and controls
119 lines (110 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
/**
* Include and setup custom metaboxes and fields. (make sure you copy this file to outside the CMB directory)
*
* @category YourThemeOrPlugin
* @package Metaboxes
* @license http://www.opensource.org/licenses/gpl-license.php GPL v2.0 (or later)
* @link https://github.com/webdevstudios/Custom-Metaboxes-and-Fields-for-WordPress
*/
/**
* Get the bootstrap! If using the plugin from wordpress.org, REMOVE THIS!
*/
if ( file_exists( __DIR__ . '/cmb2/init.php' ) ) {
require_once __DIR__ . '/cmb2/init.php';
} elseif ( file_exists( __DIR__ . '/CMB2/init.php' ) ) {
require_once __DIR__ . '/CMB2/init.php';
}
/**
* Conditionally displays a field when used as a callback in the 'show_on_cb' field parameter
*
* @param CMB2_Field object $field Field object
*
* @return bool True if metabox should show
*/
function cmb2_hide_if_no_cats( $field ) {
// Don't show this field if not in the cats category
if ( ! has_tag( 'cats', $field->object_id ) ) {
return false;
}
return true;
}
add_filter( 'cmb2_meta_boxes', 'cmb2_sample_metaboxes' );
/**
* Define the metabox and field configurations.
*
* @param array $meta_boxes
* @return array
*/
function cmb2_sample_metaboxes( array $meta_boxes ) {
// Start with an underscore to hide fields from custom fields list
$prefix = '_apcorp_';
$meta_boxes['product_line_options'] = array(
'id' => 'product-line-options',
'title' => __( 'Options', 'cmb2' ),
'object_types' => array( 'product-line', ), // Post type
'context' => 'normal',
'priority' => 'high',
'show_names' => true, // Show field names on the left
// 'cmb_styles' => false, // false to disable the CMB stylesheet
'fields' => array(
array(
'name' => __( 'Related Company', 'cmb2' ),
'desc' => __( 'Copy the company slug and paste it here.', 'cmb2' ),
'id' => $prefix . 'company_id',
'type' => 'text',
// 'repeatable' => true,
),
),
);
/**
* Repeatable Field Groups
*/
$meta_boxes['home_slider'] = array(
'id' => 'home-slider',
'title' => __( 'Home Page Slider', 'cmb2' ),
'object_types' => array( 'page', ),
'show_on' => array( 'key' => 'page-template', 'value' => 'home.php' ),
'fields' => array(
array(
'id' => $prefix . 'home-slides',
'type' => 'group',
'options' => array(
'group_title' => __( 'Slide #{#}', 'cmb2' ), // {#} gets replaced by row number
'add_button' => __( 'Add Another Slide', 'cmb2' ),
'remove_button' => __( 'Remove Slide', 'cmb2' ),
'sortable' => true, // beta
),
// Fields array works the same, except id's only need to be unique for this group. Prefix is not needed.
'fields' => array(
array(
'name' => 'Slide Title',
'id' => 'title',
'type' => 'text',
// 'repeatable' => true, // Repeatable fields are supported w/in repeatable groups (for most types)
),
array(
'name' => 'Slide Description',
'description' => 'Write a short description for this entry',
'id' => 'description',
'type' => 'textarea_small',
),
array(
'name' => 'Slide Image',
'id' => 'image',
'type' => 'file_list',
'preview_size' => array( 100, 100 ),
'allow' => array( 'attachment' ), // limit to just attachments
),
array(
'name' => 'Image Caption',
'id' => 'image_caption',
'type' => 'text',
),
),
),
),
);
// Add other metaboxes as needed
return $meta_boxes;
}