-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexamples.php
More file actions
184 lines (139 loc) · 6.12 KB
/
examples.php
File metadata and controls
184 lines (139 loc) · 6.12 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
/**
* Example usage of a custom metabox with several postmeta types included
*/
class Example_Metabox extends WP_Metabox {
public function __construct( $key, PostMetaFactory $post_meta_factory, $args = array() ) {
parent::__construct( $key, $post_meta_factory, $args );
# A basic text box called 'test'
$this->metadata['test'] = $post_meta_factory->create( 'test' );
# A select menu with custom labels
$this->metadata['label-select'] = $post_meta_factory->create(
'label-select',
array(
'label' => __('Select Menu with Custom Labels', 'wp-metabox' ),
'placeholder' => 'Pick one',
'type' => 'select',
'choices' => array(
'one' => 'ONE',
'two' => 'Tw0',
'three' => '3rEEE'
),
)
);
# checkbox and radio examples
$this->metadata['radio'] = $post_meta_factory->create(
'radio',
array(
'label' => __('Basic Radio Button', 'wp-metabox'),
'type' => 'radio',
'choices' => array( 'one', 'two', 'three' ),
)
);
# an image upload using the media uploader
$this->metadata['image_upload'] = $post_meta_factory->create( 'image_upload', array( 'type' => 'media' ) );
# a second image upload
$this->metadata['second_upload'] = $post_meta_factory->create( 'second_upload', array( 'type' => 'media', 'label' => __( 'Second Upload', 'wp-metabox' ) ) );
add_filter( 'the_content' , array($this, 'display') );
}
# displays some of the metadata automatically on the content
public function display( $content ) {
global $post;
if (get_post_meta($post->ID, 'test', true) != '') {
$content = get_post_meta($post->ID, 'test', true) . $content;
}
return $content;
}
}
/**
* Example Custom Content Type
* Creates a sample custom content type using WP Metabox to create custom postmeta boxes
*/
class Example_Content_Type extends WP_ContentType {
function __construct( $key = 'example-content-type', $args = array( 'singular' => 'Content Type') ) {
# registers the post type with provided arguments
parent::__construct( $key, $args );
$postmeta_factory = WP_PostMetaFactory::get_instance();
# creates a simple metabox with one url input using WP_SimpleMetabox
$this->metaboxes['project-url'] = new WP_SimpleMetabox( 'project-url', $postmeta_factory, array (
'label' => __('Project URL', 'wp-metabox' ),
'type' => 'url',
'posttype' => $this->key,
'placeholder' => 'Enter your URL here.'
)
);
# creates another simple metabox with one text input using WP_SimpleMetabox
$this->metaboxes['project-date'] = new WP_SimpleMetabox( 'project-date', $postmeta_factory, array (
'label' => __( 'Project Date', 'wp-metabox' ),
'posttype' => $this->key,
'type' => 'date'
)
);
# creates a simple metabox with an ordered list
$this->metaboxes['ordered-list'] = new WP_SimpleMetabox( 'ordered-list', $postmeta_factory, array (
'label' => __( 'Ordered List', 'wp-metabox' ),
'posttype' => $this->key,
'type' => 'ordered-list'
)
);
$postmeta_factory->register_postmeta_type( 'custom-ordered', 'OrderedGroup' );
# creates a simple metabox with an ordered list
$this->metaboxes['custom-ordered'] = new WP_SimpleMetabox( 'custom-ordered', $postmeta_factory, array (
'label' => __( 'Ordered Multi Text Inputs', 'wp-metabox' ),
'posttype' => $this->key,
'type' => 'custom-ordered'
)
);
}
}
# Example customized ordered list with two text areas
class OrderedGroup extends WP_OrderedListMeta {
public function display_postmeta( $post_id ) {
if ( ! $data ) $data = get_post_meta( $post_id, $this->key, true );
if ( ! is_array( $data ) ) $data = array();
$first = $data[ 'first' ];
$next = $data[ 'next' ];
$first[] = '';
echo "<p>";
$this->display_label();
echo "</p><ul class=\"wp-metabox-ordered-list\">";
foreach ( $first as $key => $value ) {
$data[ $this->key ][ 'first' ] = $value;
$data[ $this->key ][ 'next' ] = $next[ $key ];
$this->display_item( $data );
}
echo "</ul><p>";
echo "<button class=\"button button-large wp-metabox-add-new\">Add New</button>";
$this->display_description();
echo "</p>";
}
protected function display_input( $data ) {
if ( ! is_array( $data ) ) $data = array();
echo "<input type=\"{$this->input_type}\" class=\"wp-metabox-input\" name=\"{$this->key}[first][]\" value=\"{$data[$this->key]['first']}\" maxlength=\"{$this->max_length}\">";
echo "<input type=\"{$this->input_type}\" class=\"wp-metabox-input\" name=\"{$this->key}[next][]\" value=\"{$data[$this->key]['next']}\" maxlength=\"{$this->max_length}\">";
}
public function update( $post_id, $data ) {
if ( ! is_array( $data ) ) $data = array();
foreach ( $data[ 'first' ] as $key => $value ) {
if ( $value == '' && $data[ 'next' ][$key] == '' ) {
unset( $data[ 'first' ][$key] );
unset( $data[ 'next' ][$key] );
}
}
parent::update( $post_id, $data );
}
}
function init_example_content_type() {
global $example_content_type;
$example_content_type = new Example_Content_Type();
# add the Example_Metabox to the example-content-type content type
$example = new Example_Metabox(
'test',
WP_PostMetaFactory::get_instance(),
array(
'label' => __('Example Metabox', 'wp-metabox' ),
'posttype' => 'example-content-type',
)
);
}
add_action( 'init', 'init_example_content_type');