Skip to content

Instantly share code, notes, and snippets.

@janizde
Created November 9, 2014 21:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save janizde/7af43c58210323836581 to your computer and use it in GitHub Desktop.
Save janizde/7af43c58210323836581 to your computer and use it in GitHub Desktop.
WP Detail Fields registration examples
<?php
/**
* WP Detail Field: Registration Examples
*/
function register_my_custom_detail_fields () {
/**
* Field Types: text, textarea, date, time, email, url
*
* Field registration for these fields is similar, just change the type in the config array
*/
register_detail_field( 'post', array(
'type' => 'text',
'slug' => 'my-text-field',
'caption' => __('My Text Field', 'textdomain'),
'description' => __('This is my text field', 'textdomain') // description can be used with any field type
) );
/**
* Field Type: select (single)
*/
register_detail_field( 'post', array(
'type' => 'select',
'slug' => 'my-select',
'caption' => __('My Select', 'textdomain'),
'options' => array(
'opt-1' => __('Option 1', 'textdomain'),
'opt-2' => __('Option 2', 'textdomain')
),
'default-val' => 'opt-1' // default-val has to correspond with one key in the options-array
'caption-none' => __('--None--', 'textdomain') // none-option only appears when no default-val is set
) );
/**
* Field Type: post-select
*
* post-select automatically loads a list of posts according to config array into the select field
*/
register_detail_field( 'post', array(
'type' => 'post-select',
'slug' => 'linked-page',
'caption' => __('Linked Page', 'textdomain'),
'post-args' => array( // post-args are directly passed to get_posts(). Read the WP doc to get more info
'post_type' => 'page',
'orderby' => 'menu_order',
'order' => 'ASC'
)
) );
/**
* Field Types: checkbox (multi), radio (multi)
*
* default-val doesn't work for checkbox
*/
register_detail_field( 'post', array(
'type' => 'radio', // or 'checkbox'
'slug' => 'my-radio',
'caption' => __('My Radio', 'textdomain'),
'options' => array(
'opt-1' => __('Option 1', 'textdomain'),
'opt-2' => __('Option 2', 'textdomain')
),
'default-val' => 'opt-1' // doesn't work for checkbox
) );
}
add_action( 'register_my_custom_detail_fields', 'add_detail_fields' );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment