| 1 |
<?php |
| 2 |
/** |
| 3 |
* Example field, group and multifield registrations for the Custom Metadata Manager plugin. |
| 4 |
* |
| 5 |
* Copyright 2010-2013 The Contributors |
| 6 |
* |
| 7 |
* GNU General Public License, Free Software Foundation <http://creativecommons.org/licenses/GPL/2.0/> |
| 8 |
* |
| 9 |
* This program is free software; you can redistribute it and/or modify |
| 10 |
* it under the terms of the GNU General Public License as published by |
| 11 |
* the Free Software Foundation; either version 2 of the License, or |
| 12 |
* (at your option) any later version. |
| 13 |
* |
| 14 |
* This program is distributed in the hope that it will be useful, |
| 15 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 |
* GNU General Public License for more details. |
| 18 |
* |
| 19 |
* You should have received a copy of the GNU General Public License |
| 20 |
* along with this program; if not, write to the Free Software |
| 21 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 22 |
* |
| 23 |
* @package Automattic\CustomMetadata |
| 24 |
*/ |
| 25 |
|
| 26 |
// Exit if accessed directly. |
| 27 |
if ( ! defined( 'ABSPATH' ) ) { |
| 28 |
exit; |
| 29 |
} |
| 30 |
|
| 31 |
add_action( 'init', 'x_init_custom_post_types' ); |
| 32 |
/** |
| 33 |
* Registers a test post type just for the examples. |
| 34 |
* |
| 35 |
* @return void |
| 36 |
*/ |
| 37 |
function x_init_custom_post_types() { |
| 38 |
|
| 39 |
$labels = array( |
| 40 |
'name' => _x( 'Tests', 'post type general name', 'custom-metadata' ), |
| 41 |
'singular_name' => _x( 'Test', 'post type singular name', 'custom-metadata' ), |
| 42 |
'add_new' => _x( 'Add New', 'Test', 'custom-metadata' ), |
| 43 |
'add_new_item' => __( 'Add New Test', 'custom-metadata' ), |
| 44 |
'edit_item' => __( 'Edit Test', 'custom-metadata' ), |
| 45 |
'new_item' => __( 'New Test', 'custom-metadata' ), |
| 46 |
'all_items' => __( 'All Tests', 'custom-metadata' ), |
| 47 |
'view_item' => __( 'View Test', 'custom-metadata' ), |
| 48 |
'search_items' => __( 'Search Tests', 'custom-metadata' ), |
| 49 |
'not_found' => __( 'No Tests found', 'custom-metadata' ), |
| 50 |
'not_found_in_trash' => __( 'No Tests found in Trash', 'custom-metadata' ), |
| 51 |
'parent_item_colon' => '', |
| 52 |
'menu_name' => 'Tests', |
| 53 |
|
| 54 |
); |
| 55 |
$args = array( |
| 56 |
'labels' => $labels, |
| 57 |
'public' => true, |
| 58 |
'publicly_queryable' => true, |
| 59 |
'show_ui' => true, |
| 60 |
'show_in_menu' => true, |
| 61 |
'query_var' => true, |
| 62 |
'rewrite' => true, |
| 63 |
'capability_type' => 'post', |
| 64 |
'has_archive' => true, |
| 65 |
'hierarchical' => false, |
| 66 |
'menu_position' => null, |
| 67 |
'supports' => array( 'title' ), |
| 68 |
); |
| 69 |
register_post_type( 'x_test', $args ); |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
add_action( 'custom_metadata_manager_init_metadata', 'x_init_custom_fields' ); |
| 74 |
/** |
| 75 |
* Registers custom metadata groups and fields. |
| 76 |
* |
| 77 |
* This is the example code that you should use. Make sure to use the |
| 78 |
* 'custom_metadata_manager_init_metadata' hook as below. |
| 79 |
*/ |
| 80 |
function x_init_custom_fields() { |
| 81 |
|
| 82 |
// adds a new group to the test post type. |
| 83 |
x_add_metadata_group( |
| 84 |
'x_metaBox1', |
| 85 |
'x_test', |
| 86 |
array( |
| 87 |
'label' => 'Group with Multiple Fields', |
| 88 |
) |
| 89 |
); |
| 90 |
|
| 91 |
// adds another group to the test post type + posts + users. |
| 92 |
x_add_metadata_group( |
| 93 |
'x_metaBox2', |
| 94 |
array( 'x_test', 'post', 'user' ), |
| 95 |
array( |
| 96 |
'label' => 'Group for Post and User', |
| 97 |
'description' => "Here's a group with a description!", |
| 98 |
) |
| 99 |
); |
| 100 |
|
| 101 |
// adds a text field to the first group. |
| 102 |
x_add_metadata_field( |
| 103 |
'x_fieldName1', |
| 104 |
'x_test', |
| 105 |
array( |
| 106 |
'group' => 'x_metaBox1', // the group name. |
| 107 |
'description' => 'This is field #1. It\'s a simple text field.', // description for the field. |
| 108 |
'label' => 'Text Field', // field label. |
| 109 |
'display_column' => true, // show this field in the column listings. |
| 110 |
) |
| 111 |
); |
| 112 |
|
| 113 |
// adds a text field to the 2nd group. |
| 114 |
x_add_metadata_field( |
| 115 |
'x_fieldName2', |
| 116 |
'x_test', |
| 117 |
array( |
| 118 |
'group' => 'x_metaBox1', |
| 119 |
'display_column' => 'My Column (with Custom Callback)', // show this field in the column listings. |
| 120 |
'display_column_callback' => 'x_field_name2_callback', // custom function to display the column results (see below). |
| 121 |
'label' => 'Text with Custom Callback', |
| 122 |
) |
| 123 |
); |
| 124 |
|
| 125 |
|
| 126 |
// adds a cloneable textarea field to the 1st group. |
| 127 |
x_add_metadata_field( |
| 128 |
'x_fieldTextarea1', |
| 129 |
'x_test', |
| 130 |
array( |
| 131 |
'group' => 'x_metaBox1', |
| 132 |
'field_type' => 'textarea', |
| 133 |
'multiple' => true, |
| 134 |
'label' => 'Repeatable Text Area', |
| 135 |
) |
| 136 |
); |
| 137 |
|
| 138 |
// adds a readonly textarea field to the 1st group. |
| 139 |
x_add_metadata_field( |
| 140 |
'x_fieldTextareaReadOnly1', |
| 141 |
'x_test', |
| 142 |
array( |
| 143 |
'group' => 'x_metaBox1', |
| 144 |
'field_type' => 'textarea', |
| 145 |
'readonly' => true, |
| 146 |
'label' => 'Read Only Text Area', |
| 147 |
) |
| 148 |
); |
| 149 |
|
| 150 |
// adds a readonly text field to the 1st group. |
| 151 |
x_add_metadata_field( |
| 152 |
'x_fieldTextReadOnly1', |
| 153 |
'x_test', |
| 154 |
array( |
| 155 |
'group' => 'x_metaBox1', |
| 156 |
'readonly' => true, |
| 157 |
'label' => 'Read Only Text Area', |
| 158 |
) |
| 159 |
); |
| 160 |
|
| 161 |
// adds a wysiwyg (full editor) field to the 2nd group. |
| 162 |
x_add_metadata_field( |
| 163 |
'x_fieldWysiwyg1', |
| 164 |
array( 'x_test', 'user' ), |
| 165 |
array( |
| 166 |
'group' => 'x_metaBox2', |
| 167 |
'field_type' => 'wysiwyg', |
| 168 |
'label' => 'TinyMCE / Wysiwyg field', |
| 169 |
) |
| 170 |
); |
| 171 |
|
| 172 |
// adds a datepicker field to the 1st group. |
| 173 |
x_add_metadata_field( |
| 174 |
'x_fieldDatepicker1', |
| 175 |
'x_test', |
| 176 |
array( |
| 177 |
'group' => 'x_metaBox1', |
| 178 |
'field_type' => 'datepicker', |
| 179 |
'label' => 'Datepicker field', |
| 180 |
) |
| 181 |
); |
| 182 |
|
| 183 |
// adds a datetimepicker field to the 1st group. |
| 184 |
x_add_metadata_field( |
| 185 |
'x_fieldDatetimepicker1', |
| 186 |
'x_test', |
| 187 |
array( |
| 188 |
'group' => 'x_metaBox1', |
| 189 |
'field_type' => 'datetimepicker', |
| 190 |
'label' => 'Datetimepicker field', |
| 191 |
) |
| 192 |
); |
| 193 |
|
| 194 |
// adds a timepicker field to the 1st group. |
| 195 |
x_add_metadata_field( |
| 196 |
'x_fieldTimepicker1', |
| 197 |
'x_test', |
| 198 |
array( |
| 199 |
'group' => 'x_metaBox1', |
| 200 |
'field_type' => 'timepicker', |
| 201 |
'label' => 'Timepicker field', |
| 202 |
) |
| 203 |
); |
| 204 |
|
| 205 |
// adds a colorpicker field to the 1st group. |
| 206 |
x_add_metadata_field( |
| 207 |
'x_fieldColorpicker1', |
| 208 |
'x_test', |
| 209 |
array( |
| 210 |
'group' => 'x_metaBox1', |
| 211 |
'field_type' => 'colorpicker', |
| 212 |
'label' => 'Colorpicker field', |
| 213 |
) |
| 214 |
); |
| 215 |
|
| 216 |
// adds an upload field to the 1st group. |
| 217 |
x_add_metadata_field( |
| 218 |
'x_fieldUpload1', |
| 219 |
'x_test', |
| 220 |
array( |
| 221 |
'group' => 'x_metaBox1', |
| 222 |
'field_type' => 'upload', |
| 223 |
'readonly' => true, |
| 224 |
'label' => 'Upload field', |
| 225 |
) |
| 226 |
); |
| 227 |
|
| 228 |
// adds a checkbox field to the first group. |
| 229 |
x_add_metadata_field( |
| 230 |
'x_fieldCheckbox1', |
| 231 |
'x_test', |
| 232 |
array( |
| 233 |
'group' => 'x_metaBox1', |
| 234 |
'field_type' => 'checkbox', |
| 235 |
'label' => 'Checkbox field', |
| 236 |
) |
| 237 |
); |
| 238 |
|
| 239 |
// adds a radio button field to the first group. |
| 240 |
x_add_metadata_field( |
| 241 |
'x_fieldRadio1', |
| 242 |
'x_test', |
| 243 |
array( |
| 244 |
'group' => 'x_metaBox1', |
| 245 |
'field_type' => 'radio', |
| 246 |
'values' => array( // set possible value/options. |
| 247 |
'option1' => 'Option #1', // key => value pair (key is stored in DB). |
| 248 |
'option2' => 'Option #2', |
| 249 |
), |
| 250 |
'label' => 'Radio field', |
| 251 |
) |
| 252 |
); |
| 253 |
|
| 254 |
// adds a select box in the first group. |
| 255 |
x_add_metadata_field( |
| 256 |
'x_fieldSelect1', |
| 257 |
'x_test', |
| 258 |
array( |
| 259 |
'group' => 'x_metaBox1', |
| 260 |
'field_type' => 'select', |
| 261 |
'values' => array( // set possible value/options. |
| 262 |
'option1' => 'Option #1', // key => value pair (key is stored in DB). |
| 263 |
'option2' => 'Option #2', |
| 264 |
), |
| 265 |
'label' => 'Select field', |
| 266 |
) |
| 267 |
); |
| 268 |
|
| 269 |
// adds a multi-select field in the first group. |
| 270 |
x_add_metadata_field( |
| 271 |
'x_field_multi_select', |
| 272 |
'x_test', |
| 273 |
array( |
| 274 |
'group' => 'x_metaBox1', |
| 275 |
'field_type' => 'multi_select', |
| 276 |
'values' => array( // set possible value/options. |
| 277 |
'option1' => 'Option #1', // key => value pair (key is stored in DB). |
| 278 |
'option2' => 'Option #2', |
| 279 |
'option3' => 'Option #3', |
| 280 |
'option4' => 'Option #4', |
| 281 |
), |
| 282 |
'label' => 'Multi Select field', |
| 283 |
) |
| 284 |
); |
| 285 |
|
| 286 |
// adds a multi-select field with chosen in the first group |
| 287 |
// note: `select2` and `chosen` args do the exact same (add select2) |
| 288 |
// but for the purposes of testing, we're using chosen here. |
| 289 |
x_add_metadata_field( |
| 290 |
'x_field_multi_select_chosen', |
| 291 |
'x_test', |
| 292 |
array( |
| 293 |
'group' => 'x_metaBox1', |
| 294 |
'field_type' => 'multi_select', |
| 295 |
'values' => array( // set possible value/options. |
| 296 |
'option1' => 'Option #1', // key => value pair (key is stored in DB). |
| 297 |
'option2' => 'Option #2', |
| 298 |
'option3' => 'Option #3', |
| 299 |
'option4' => 'Option #4', |
| 300 |
), |
| 301 |
'label' => 'Multi Select field (with chosen)', |
| 302 |
'chosen' => true, |
| 303 |
) |
| 304 |
); |
| 305 |
|
| 306 |
// adds a select field with select2 in the first group. |
| 307 |
x_add_metadata_field( |
| 308 |
'x_field_select_select2', |
| 309 |
'x_test', |
| 310 |
array( |
| 311 |
'group' => 'x_metaBox1', |
| 312 |
'field_type' => 'select', |
| 313 |
'values' => array( // set possible value/options. |
| 314 |
'option1' => 'Option #1', // key => value pair (key is stored in DB). |
| 315 |
'option2' => 'Option #2', |
| 316 |
'option3' => 'Option #3', |
| 317 |
'option4' => 'Option #4', |
| 318 |
), |
| 319 |
'label' => 'Select field (with select2)', |
| 320 |
'select2' => true, |
| 321 |
) |
| 322 |
); |
| 323 |
|
| 324 |
// adds a taxonomy checkbox field in the first group. |
| 325 |
x_add_metadata_field( |
| 326 |
'x_field_taxonomy_checkbox', |
| 327 |
'x_test', |
| 328 |
array( |
| 329 |
'group' => 'x_metaBox1', |
| 330 |
'field_type' => 'taxonomy_checkbox', |
| 331 |
'taxonomy' => 'category', |
| 332 |
'label' => 'Category checkbox field', |
| 333 |
) |
| 334 |
); |
| 335 |
|
| 336 |
// adds a taxonomy select field in the first group. |
| 337 |
x_add_metadata_field( |
| 338 |
'x_field_taxonomy_select', |
| 339 |
'x_test', |
| 340 |
array( |
| 341 |
'group' => 'x_metaBox1', |
| 342 |
'field_type' => 'taxonomy_select', |
| 343 |
'taxonomy' => 'category', |
| 344 |
'label' => 'Category select field', |
| 345 |
) |
| 346 |
); |
| 347 |
|
| 348 |
// adds a taxonomy multiselect field in the first group. |
| 349 |
x_add_metadata_field( |
| 350 |
'x_field_taxonomy_multi_select', |
| 351 |
'x_test', |
| 352 |
array( |
| 353 |
'group' => 'x_metaBox1', |
| 354 |
'field_type' => 'taxonomy_multi_select', |
| 355 |
'taxonomy' => 'category', |
| 356 |
'label' => 'Category multiselect field', |
| 357 |
) |
| 358 |
); |
| 359 |
|
| 360 |
// adds a taxonomy multiselect w/ select2 field in the first group. |
| 361 |
x_add_metadata_field( |
| 362 |
'x_field_taxonomy_multi_select2', |
| 363 |
'x_test', |
| 364 |
array( |
| 365 |
'group' => 'x_metaBox1', |
| 366 |
'field_type' => 'taxonomy_multi_select', |
| 367 |
'taxonomy' => 'category', |
| 368 |
'label' => 'Category multiselect w/ select2 field', |
| 369 |
'select2' => true, |
| 370 |
) |
| 371 |
); |
| 372 |
|
| 373 |
// adds a number field in the first group (with no min/max). |
| 374 |
x_add_metadata_field( |
| 375 |
'x_field_number', |
| 376 |
'x_test', |
| 377 |
array( |
| 378 |
'group' => 'x_metaBox1', |
| 379 |
'field_type' => 'number', |
| 380 |
'label' => 'Number field', |
| 381 |
) |
| 382 |
); |
| 383 |
|
| 384 |
// adds a number field in the first group (with min/max). |
| 385 |
x_add_metadata_field( |
| 386 |
'x_field_number_with_min_max', |
| 387 |
'x_test', |
| 388 |
array( |
| 389 |
'group' => 'x_metaBox1', |
| 390 |
'field_type' => 'number', |
| 391 |
'min' => '-3', |
| 392 |
'max' => '25', |
| 393 |
'multiple' => true, |
| 394 |
'label' => 'Number field (with min/max + cloneable)', |
| 395 |
) |
| 396 |
); |
| 397 |
|
| 398 |
// adds an email field in the first group. |
| 399 |
x_add_metadata_field( |
| 400 |
'x_field_email', |
| 401 |
'x_test', |
| 402 |
array( |
| 403 |
'group' => 'x_metaBox1', |
| 404 |
'field_type' => 'email', |
| 405 |
'label' => 'Email field', |
| 406 |
) |
| 407 |
); |
| 408 |
|
| 409 |
// adds a link field in the first group. |
| 410 |
x_add_metadata_field( |
| 411 |
'x_field_link', |
| 412 |
'x_test', |
| 413 |
array( |
| 414 |
'group' => 'x_metaBox1', |
| 415 |
'field_type' => 'link', |
| 416 |
'label' => 'Link field', |
| 417 |
) |
| 418 |
); |
| 419 |
|
| 420 |
// adds a telephone field in the first group (with default value). |
| 421 |
x_add_metadata_field( |
| 422 |
'x_field_telephone', |
| 423 |
'x_test', |
| 424 |
array( |
| 425 |
'group' => 'x_metaBox1', |
| 426 |
'field_type' => 'tel', |
| 427 |
'label' => 'Telephone field', |
| 428 |
'default_value' => '123-4567', |
| 429 |
) |
| 430 |
); |
| 431 |
|
| 432 |
// adds a text field with a default value. |
| 433 |
x_add_metadata_field( |
| 434 |
'x_field_text_default', |
| 435 |
'x_test', |
| 436 |
array( |
| 437 |
'group' => 'x_metaBox1', |
| 438 |
'field_type' => 'text', |
| 439 |
'label' => 'Text field with default value', |
| 440 |
'default_value' => 'lorem ipsum', |
| 441 |
) |
| 442 |
); |
| 443 |
|
| 444 |
// adds a text field with placeholder. |
| 445 |
x_add_metadata_field( |
| 446 |
'x_field_textarea_placeholder', |
| 447 |
'x_test', |
| 448 |
array( |
| 449 |
'group' => 'x_metaBox1', |
| 450 |
'field_type' => 'textarea', |
| 451 |
'label' => 'Textarea field with placeholder', |
| 452 |
'placeholder' => 'some placeholder text', |
| 453 |
) |
| 454 |
); |
| 455 |
|
| 456 |
// adds a password field with placeholder. |
| 457 |
x_add_metadata_field( |
| 458 |
'x_field_password_placeholder', |
| 459 |
'x_test', |
| 460 |
array( |
| 461 |
'group' => 'x_metaBox1', |
| 462 |
'field_type' => 'password', |
| 463 |
'label' => 'Password field with placeholder', |
| 464 |
'placeholder' => 'some placeholder text', |
| 465 |
) |
| 466 |
); |
| 467 |
|
| 468 |
// adds a number field with placeholder. |
| 469 |
x_add_metadata_field( |
| 470 |
'x_field_number_placeholder', |
| 471 |
'x_test', |
| 472 |
array( |
| 473 |
'group' => 'x_metaBox1', |
| 474 |
'field_type' => 'number', |
| 475 |
'label' => 'Number field with placeholder', |
| 476 |
'placeholder' => 'some placeholder text', |
| 477 |
) |
| 478 |
); |
| 479 |
|
| 480 |
// adds an email field with placeholder. |
| 481 |
x_add_metadata_field( |
| 482 |
'x_field_email_placeholder', |
| 483 |
'x_test', |
| 484 |
array( |
| 485 |
'group' => 'x_metaBox1', |
| 486 |
'field_type' => 'email', |
| 487 |
'label' => 'Email field with placeholder', |
| 488 |
'placeholder' => 'some placeholder text', |
| 489 |
) |
| 490 |
); |
| 491 |
|
| 492 |
// adds a link field with placeholder. |
| 493 |
x_add_metadata_field( |
| 494 |
'x_field_link_placeholder', |
| 495 |
'x_test', |
| 496 |
array( |
| 497 |
'group' => 'x_metaBox1', |
| 498 |
'field_type' => 'link', |
| 499 |
'label' => 'Link field with placeholder', |
| 500 |
'placeholder' => 'some placeholder text', |
| 501 |
) |
| 502 |
); |
| 503 |
|
| 504 |
// adds an telephone field with placeholder. |
| 505 |
x_add_metadata_field( |
| 506 |
'x_field_telephone_placeholder', |
| 507 |
'x_test', |
| 508 |
array( |
| 509 |
'group' => 'x_metaBox1', |
| 510 |
'field_type' => 'tel', |
| 511 |
'label' => 'Telephone field with placeholder', |
| 512 |
'placeholder' => 'some placeholder text', |
| 513 |
) |
| 514 |
); |
| 515 |
|
| 516 |
// adds an upload field with placeholder. |
| 517 |
x_add_metadata_field( |
| 518 |
'x_field_upload_placeholder', |
| 519 |
'x_test', |
| 520 |
array( |
| 521 |
'group' => 'x_metaBox1', |
| 522 |
'field_type' => 'upload', |
| 523 |
'label' => 'Upload field with placeholder', |
| 524 |
'placeholder' => 'some placeholder text', |
| 525 |
) |
| 526 |
); |
| 527 |
|
| 528 |
// adds an datepicker field with placeholder. |
| 529 |
x_add_metadata_field( |
| 530 |
'x_field_datepicker_placeholder', |
| 531 |
'x_test', |
| 532 |
array( |
| 533 |
'group' => 'x_metaBox1', |
| 534 |
'field_type' => 'datepicker', |
| 535 |
'label' => 'Datepicker field with placeholder', |
| 536 |
'placeholder' => 'some placeholder text', |
| 537 |
) |
| 538 |
); |
| 539 |
|
| 540 |
// adds a datetimepicker field with placeholder. |
| 541 |
x_add_metadata_field( |
| 542 |
'x_field_datetimepicker_placeholder', |
| 543 |
'x_test', |
| 544 |
array( |
| 545 |
'group' => 'x_metaBox1', |
| 546 |
'field_type' => 'datetimepicker', |
| 547 |
'label' => 'Datetimepicker field with placeholder', |
| 548 |
'placeholder' => 'some placeholder text', |
| 549 |
) |
| 550 |
); |
| 551 |
|
| 552 |
// adds a timepicker field with placeholder. |
| 553 |
x_add_metadata_field( |
| 554 |
'x_field_timepicker_placeholder', |
| 555 |
'x_test', |
| 556 |
array( |
| 557 |
'group' => 'x_metaBox1', |
| 558 |
'field_type' => 'timepicker', |
| 559 |
'label' => 'Timepicker field with placeholder', |
| 560 |
'placeholder' => 'some placeholder text', |
| 561 |
) |
| 562 |
); |
| 563 |
|
| 564 |
// adds a mulitifield. |
| 565 |
x_add_metadata_multifield( |
| 566 |
'x_test_multifield', |
| 567 |
'x_test', |
| 568 |
array( |
| 569 |
'group' => 'x_metabox1', |
| 570 |
'label' => 'Multifield test', |
| 571 |
'description' => 'This is a great multifield', |
| 572 |
) |
| 573 |
); |
| 574 |
|
| 575 |
// adds a text field to the multifield. |
| 576 |
x_add_metadata_field( |
| 577 |
'multifield_field_1', |
| 578 |
'x_test', |
| 579 |
array( |
| 580 |
'group' => 'x_metabox1', // the group name. |
| 581 |
'multifield' => 'x_test_multifield', |
| 582 |
'description' => 'This is field #1 of the multifield.', |
| 583 |
'label' => 'Text Field', // field label. |
| 584 |
'field_type' => 'text', |
| 585 |
) |
| 586 |
); |
| 587 |
|
| 588 |
// adds an upload text field to the multifield. |
| 589 |
x_add_metadata_field( |
| 590 |
'multifield_field_2', |
| 591 |
'x_test', |
| 592 |
array( |
| 593 |
'group' => 'x_metabox1', // the group name. |
| 594 |
'multifield' => 'x_test_multifield', |
| 595 |
'description' => 'This is field #2 of the multifield.', |
| 596 |
'label' => 'Upload Field', // field label. |
| 597 |
'field_type' => 'upload', |
| 598 |
) |
| 599 |
); |
| 600 |
|
| 601 |
|
| 602 |
// adds an upload text field to the multifield. |
| 603 |
x_add_metadata_field( |
| 604 |
'multifield_field_3', |
| 605 |
'x_test', |
| 606 |
array( |
| 607 |
'group' => 'x_metabox1', // the group name. |
| 608 |
'multifield' => 'x_test_multifield', |
| 609 |
'description' => 'This is field #4 of the multifield.', |
| 610 |
'label' => 'Telephone Field', // field label. |
| 611 |
'field_type' => 'tel', |
| 612 |
) |
| 613 |
); |
| 614 |
|
| 615 |
// adds a text field to the multifield. |
| 616 |
x_add_metadata_field( |
| 617 |
'multifield_field_4', |
| 618 |
'x_test', |
| 619 |
array( |
| 620 |
'group' => 'x_metabox1', // the group name. |
| 621 |
'multifield' => 'x_test_multifield', |
| 622 |
'description' => 'This is field #4 of the multifield.', |
| 623 |
'label' => 'Password Field', // field label. |
| 624 |
'field_type' => 'password', |
| 625 |
) |
| 626 |
); |
| 627 |
|
| 628 |
|
| 629 |
// adds a field to posts and users. |
| 630 |
x_add_metadata_field( |
| 631 |
'x_fieldName2', |
| 632 |
array( 'post', 'user' ), |
| 633 |
array( |
| 634 |
'group' => 'x_metaBox2', |
| 635 |
'label' => 'Text field', |
| 636 |
) |
| 637 |
); |
| 638 |
|
| 639 |
// adds a field with a custom display callback (see below). |
| 640 |
x_add_metadata_field( |
| 641 |
'x_fieldCustomHidden1', |
| 642 |
'x_test', |
| 643 |
array( |
| 644 |
'group' => 'x_metaBox1', |
| 645 |
'display_callback' => 'x_field_customhidden1_callback', // this function is defined below. |
| 646 |
'label' => 'Hidden field', |
| 647 |
) |
| 648 |
); |
| 649 |
|
| 650 |
|
| 651 |
// field with capabilities limited. |
| 652 |
x_add_metadata_field( |
| 653 |
'x_cap-limited-field', |
| 654 |
'x_test', |
| 655 |
array( |
| 656 |
'label' => 'Cap Limited Field (edit_posts)', |
| 657 |
'required_cap' => 'edit_posts', // limit to users who can edit posts. |
| 658 |
) |
| 659 |
); |
| 660 |
|
| 661 |
// field with role limited. |
| 662 |
x_add_metadata_field( |
| 663 |
'x_author-cap-limited-field', |
| 664 |
'user', |
| 665 |
array( |
| 666 |
'label' => 'Cap Limited Field (author)', |
| 667 |
'required_cap' => 'author', // limit to authors. |
| 668 |
) |
| 669 |
); |
| 670 |
|
| 671 |
// comment field. |
| 672 |
x_add_metadata_field( |
| 673 |
'x_commentField1', |
| 674 |
'comment', |
| 675 |
array( |
| 676 |
'label' => 'Field for Comment', |
| 677 |
'display_column' => true, |
| 678 |
) |
| 679 |
); |
| 680 |
|
| 681 |
// field that exludes posts. |
| 682 |
x_add_metadata_field( |
| 683 |
'x_fieldNameExcluded1', |
| 684 |
'post', |
| 685 |
array( |
| 686 |
'description' => 'This field is excluded from Post ID#2476', |
| 687 |
'label' => 'Excluded Field', |
| 688 |
'exclude' => 2476, // phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_exclude -- Plugin field-visibility config key, not a WP_Query argument. |
| 689 |
) |
| 690 |
); |
| 691 |
|
| 692 |
// field that includes certain posts only. |
| 693 |
x_add_metadata_field( |
| 694 |
'x_fieldNameIncluded1', |
| 695 |
'post', |
| 696 |
array( |
| 697 |
'description' => 'This field is only included on Post ID#2476', |
| 698 |
'label' => 'Included Field', |
| 699 |
'include' => 2476, |
| 700 |
) |
| 701 |
); |
| 702 |
|
| 703 |
x_add_metadata_field( |
| 704 |
'x_fieldExcludeCallback', |
| 705 |
'post', |
| 706 |
array( |
| 707 |
'description' => 'This field is excluded using a custom callback; will be excluded from posts in the "aside" category', |
| 708 |
'label' => 'Excluded Field (with callback)', |
| 709 |
'exclude' => 'x_custom_exclude_callback', // phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_exclude -- Plugin field-visibility config key, not a WP_Query argument. |
| 710 |
) |
| 711 |
); |
| 712 |
|
| 713 |
/** |
| 714 |
* Determines whether a field should be excluded because the post is in the "aside" category. |
| 715 |
* |
| 716 |
* @param string $thing_slug Slug of the field or group. |
| 717 |
* @param object $thing Field or group args set up when registering. |
| 718 |
* @param string $object_type Type of object (post, comment, user). |
| 719 |
* @param int|string $object_id ID of the object. |
| 720 |
* @param string $object_slug Slug of the object. |
| 721 |
* @return bool Whether the field should be excluded. |
| 722 |
*/ |
| 723 |
function x_custom_exclude_callback( $thing_slug, $thing, $object_type, $object_id, $object_slug ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed -- Example callback showing the full signature the plugin provides. |
| 724 |
// exclude from all posts that are in the aside category. |
| 725 |
return in_category( 'aside', $object_id ); |
| 726 |
} |
| 727 |
|
| 728 |
x_add_metadata_field( |
| 729 |
'x_fieldIncludedCallback', |
| 730 |
'post', |
| 731 |
array( |
| 732 |
'description' => 'This field is included using a custom callback; will only be included for posts that are not published', |
| 733 |
'label' => 'Included Field (with callback)', |
| 734 |
'include' => 'x_custom_include_callback', |
| 735 |
) |
| 736 |
); |
| 737 |
|
| 738 |
/** |
| 739 |
* Includes a field only for posts that are not published. |
| 740 |
* |
| 741 |
* @param string $thing_slug Slug of the field or group. |
| 742 |
* @param object $thing Field or group args set up when registering. |
| 743 |
* @param string $object_type Type of object (post, comment, user). |
| 744 |
* @param int|string $object_id ID of the object. |
| 745 |
* @param string $object_slug Slug of the object. |
| 746 |
* @return bool Whether the field should be included. |
| 747 |
*/ |
| 748 |
function x_custom_include_callback( $thing_slug, $thing, $object_type, $object_id, $object_slug ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed -- Example callback showing the full signature the plugin provides. |
| 749 |
$post = get_post( $object_id ); |
| 750 |
return 'publish' != $post->post_status; |
| 751 |
} |
| 752 |
} |
| 753 |
|
| 754 |
/** |
| 755 |
* Example of a column callback function. |
| 756 |
* |
| 757 |
* It echoes out a bogus description, but it is just so you can see how it works. |
| 758 |
* |
| 759 |
* @param string $field_slug The slug/ID of the field. |
| 760 |
* @param object $field The field object. |
| 761 |
* @param string $object_type The object type the field is associated with. |
| 762 |
* @param int $object_id The ID of the current object. |
| 763 |
* @param string $value The value of the field. |
| 764 |
* @return void |
| 765 |
*/ |
| 766 |
function x_field_name2_callback( $field_slug, $field, $object_type, $object_id, $value ) { |
| 767 |
// Escape the dynamic values on output; the surrounding markup is our own, so it stays. |
| 768 |
printf( 'The value of field "%s" is %s. <br /><a href="http://icanhascheezburger.files.wordpress.com/2010/10/04dc84b6-3dde-45db-88ef-f7c242731ce3.jpg">Here\'s a LOLCat</a>', esc_html( $field_slug ), $value ? esc_html( $value ) : 'not set' ); |
| 769 |
} |
| 770 |
|
| 771 |
/** |
| 772 |
* Another example of a custom callback function. |
| 773 |
* |
| 774 |
* We have chosen not to include all of the params this time. |
| 775 |
* |
| 776 |
* @param string $field_slug The slug/ID of the field. |
| 777 |
* @param object $field The field object. |
| 778 |
* @param mixed $value The value of the field. |
| 779 |
* @return void |
| 780 |
*/ |
| 781 |
function x_field_customhidden1_callback( $field_slug, $field, $value ) { |
| 782 |
if ( ! $value ) { |
| 783 |
$value = 'This is a secret hidden value! Don\'t tell anyone!'; |
| 784 |
} |
| 785 |
?> |
| 786 |
<hr /> |
| 787 |
<p>This is a hidden field rendered with a custom callback. The value is "<?php echo esc_html( $value ); ?>".</p> |
| 788 |
<input type="hidden" name="<?php echo esc_attr( $field_slug ); ?>" value="<?php echo esc_attr( $value ); ?>" /> |
| 789 |
<hr /> |
| 790 |
<?php |
| 791 |
} |