Skip to content

Instantly share code, notes, and snippets.

@Shelob9 Shelob9/fields-api-example.php Secret
Last active Feb 18, 2016

Embed
What would you like to do?
<?php
/**
Plugin Name: Fields API & Term Meta Example
*/
/**
* Actions
*/
add_action( 'init', 'josh_tfx_register_state', 0 );
add_action( 'init', 'josh_tfx_register_city', 0 );
add_action( 'fields_register', 'josh_city_term_fields' );
/**
* @param WP_Fields_API $wp_fields
*/
function josh_city_term_fields( $wp_fields ) {
// What WP Object type?
$object_type = 'term';
// Which taxonomy?
$object_name = 'city';
// Form: Which screen to add to
$form_id = 'term-edit';
//create section
$section_id = 'city-state'; //
$section_args = array(
'title' => __( 'State', 'josh-tfx' ),
'form' => $form_id,
);
$wp_fields->add_section( $object_type, $section_id, $object_name, $section_args );
// Add a field
$field_id = 'state';
//setup field
$field_args = array(
'control' => array(
'type' => 'dropdown-terms',
'taxonomy' => 'state',
'section' => $section_id,
'label' => __( 'State', 'josh-tfx' ),
'description' => __( 'What city is this state in?', 'josh-tfx' ),
),
);
$wp_fields->add_field( $object_type, $field_id, $object_name, $field_args );
}
/**
* Register City Taxonomy
*/
function josh_tfx_register_city() {
$labels = array(
'name' => _x( 'Cities', 'Taxonomy General Name', 'josh-tfx' ),
'singular_name' => _x( 'City', 'Taxonomy Singular Name', 'josh-tfx' ),
'menu_name' => __( 'Cities', 'josh-tfx' ),
'all_items' => __( 'All Items', 'josh-tfx' ),
'parent_item' => __( 'Parent Item', 'josh-tfx' ),
'parent_item_colon' => __( 'Parent Item:', 'josh-tfx' ),
'new_item_name' => __( 'New Item Name', 'josh-tfx' ),
'add_new_item' => __( 'Add New Item', 'josh-tfx' ),
'edit_item' => __( 'Edit Item', 'josh-tfx' ),
'update_item' => __( 'Update Item', 'josh-tfx' ),
'view_item' => __( 'View Item', 'josh-tfx' ),
'separate_items_with_commas' => __( 'Separate items with commas', 'josh-tfx' ),
'add_or_remove_items' => __( 'Add or remove items', 'josh-tfx' ),
'choose_from_most_used' => __( 'Choose from the most used', 'josh-tfx' ),
'popular_items' => __( 'Popular Items', 'josh-tfx' ),
'search_items' => __( 'Search Items', 'josh-tfx' ),
'not_found' => __( 'Not Found', 'josh-tfx' ),
'no_terms' => __( 'No items', 'josh-tfx' ),
'items_list' => __( 'Items list', 'josh-tfx' ),
'items_list_navigation' => __( 'Items list navigation', 'josh-tfx' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
);
register_taxonomy( 'city', array( 'post' ), $args );
}
/**
* Register State Taxonomy
*/
function josh_tfx_register_state() {
$labels = array(
'name' => _x( 'States', 'Taxonomy General Name', 'josh-tfx' ),
'singular_name' => _x( 'State', 'Taxonomy Singular Name', 'josh-tfx' ),
'menu_name' => __( 'States', 'josh-tfx' ),
'all_items' => __( 'All Items', 'josh-tfx' ),
'parent_item' => __( 'Parent Item', 'josh-tfx' ),
'parent_item_colon' => __( 'Parent Item:', 'josh-tfx' ),
'new_item_name' => __( 'New Item Name', 'josh-tfx' ),
'add_new_item' => __( 'Add New Item', 'josh-tfx' ),
'edit_item' => __( 'Edit Item', 'josh-tfx' ),
'update_item' => __( 'Update Item', 'josh-tfx' ),
'view_item' => __( 'View Item', 'josh-tfx' ),
'separate_items_with_commas' => __( 'Separate items with commas', 'josh-tfx' ),
'add_or_remove_items' => __( 'Add or remove items', 'josh-tfx' ),
'choose_from_most_used' => __( 'Choose from the most used', 'josh-tfx' ),
'popular_items' => __( 'Popular Items', 'josh-tfx' ),
'search_items' => __( 'Search Items', 'josh-tfx' ),
'not_found' => __( 'Not Found', 'josh-tfx' ),
'no_terms' => __( 'No items', 'josh-tfx' ),
'items_list' => __( 'Items list', 'josh-tfx' ),
'items_list_navigation' => __( 'Items list navigation', 'josh-tfx' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
);
register_taxonomy( 'state', array( 'post' ), $args );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.