Created
July 5, 2011 21:01
-
-
Save mattboon/1065917 to your computer and use it in GitHub Desktop.
WordPress CPT with Taxonomy Custom Fields I
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//---- CUSTOM POST TYPE - FAQs -------------------------------------------------------- | |
add_action('init', '_init_question_post_type'); | |
function _init_question_post_type() { | |
// MAKE POST TYPE | |
//--------------------------- | |
register_post_type( 'question', | |
array( | |
'labels' => array( | |
'name' => __( 'FAQs' ), | |
'singular_name' => __( 'FAQ' ), | |
'add_new' => __( 'Add New' ), | |
'add_new_item' => __( 'Add New FAQ' ), | |
'edit' => __( 'Edit' ), | |
'edit_item' => __( 'Edit FAQ' ), | |
'new_item' => __( 'New FAQ' ), | |
'view' => __( 'View FAQ' ), | |
'view_item' => __( 'View FAQ' ), | |
'search_items' => __( 'Search FAQ' ), | |
'not_found' => __( 'No FAQs found' ), | |
'not_found_in_trash' => __( 'No FAQs found in Trash' ), | |
), | |
'public' => true, | |
'show_ui' => true, | |
'publicly_queryable' => true, | |
'exclude_from_search' => false, | |
'hierarchical' => false, | |
'menu_position' => 20, | |
'capability_type' => 'post', | |
'menu_icon' => '/assets/images/faq_menu.png', | |
'query_var' => true, | |
'rewrite' => array( 'slug' => 'about/faqs', 'with_front' => false ), | |
'supports' => array('title', 'editor'), | |
) | |
); | |
// MAKE PERMALINKS WORK | |
//--------------------------- | |
flush_rewrite_rules(); | |
// MAKE TAXONOMY | |
//--------------------------- | |
register_taxonomy_for_object_type('question', 'question-category'); | |
register_taxonomy( | |
'question-category', | |
array( 'question' ), | |
array( | |
'labels' => array( | |
'name' => __( 'FAQ Categories' ), | |
'singular_name' => __( 'FAQ Category' ), | |
'search_items' => __( 'Search FAQ Categories' ), | |
'popular_items' => __( 'Popular FAQ Categories' ), | |
'all_items' => __( 'All FAQ Categories' ), | |
'parent_item' => __( 'Parent FAQ Category' ), | |
'parent_item_colon' => __( 'Parent FAQ Category:' ), | |
'edit_item' => __( 'Edit FAQ Category' ), | |
'update_item' => __( 'Update FAQ Category' ), | |
'add_new_item' => __( 'Add New FAQ Category' ), | |
'new_item_name' => __( 'New FAQ Category' ), | |
), | |
'public' => true, | |
'show_in_nav_menus' => true, | |
'show_ui' => true, | |
'publicly_queryable' => true, | |
'exclude_from_search' => false, | |
//'rewrite' => array( 'slug' => 'topics', 'with_front' => false ), | |
'hierarchical' => true, | |
'query_var' => true, | |
) | |
); | |
// DEFINE CUSTOM TAXONOMY FIELDS | |
//--------------------------- | |
add_action( 'question-category_edit_form_fields', 'question_category_custom_fields', 10, 2 ); | |
function question_category_custom_fields($tag) { | |
// Check for existing taxonomy meta for the term you're editing | |
$t_id = $tag->term_id; // Get the ID of the term you're editing | |
$term_meta = get_option( "taxonomy_term_$t_id" ); // Do the check | |
// Get order field value | |
$question_category_order = $term_meta['question_category_order']; | |
// Default to zero | |
if (!$question_category_order) $question_category_order = "1"; | |
// Get column field value | |
$question_category_column = $term_meta['question_category_column']; | |
// Default to left | |
if (!$question_category_column) $question_category_column = "left"; | |
?> | |
<tr class="form-field"> | |
<th scope="row" valign="top"> | |
<label for="question_category_column">Column</label> | |
</th> | |
<td> | |
<select style="width: 100px;" name="term_meta[question_category_column]" id="question_category_column"> | |
<option value="left" selected="selected">Left</option> | |
<option value="right" <?php if ($question_category_column == "right") echo ' selected="selected";'; ?>>Right</option> | |
</select> | |
</td> | |
</tr> | |
<tr class="form-field"> | |
<th scope="row" valign="top"> | |
<label for="question_category_order">Order</label> | |
</th> | |
<td> | |
<input style="width: 98px;" type="text" name="term_meta[question_category_order]" id="question_category_order" size="2" maxlength="2" value="<?php echo $question_category_order ? $question_category_order : ''; ?>"> | |
</td> | |
</tr> | |
<?php | |
} | |
// SAVE CUSTOM TAXONOMY FIELDS | |
//--------------------------- | |
add_action( 'edited_question-category', 'save_question_category_custom_fields', 10, 2 ); | |
function save_question_category_custom_fields( $term_id ) { | |
if ( isset( $_POST['term_meta'] ) ) { | |
$t_id = $term_id; | |
$term_meta = get_option( "taxonomy_term_$t_id" ); | |
$cat_keys = array_keys( $_POST['term_meta'] ); | |
foreach ( $cat_keys as $key ){ | |
if ( isset( $_POST['term_meta'][$key] ) ){ | |
$term_meta[$key] = $_POST['term_meta'][$key]; | |
} | |
} | |
//save the option array | |
update_option( "taxonomy_term_$t_id", $term_meta ); | |
} | |
} | |
// DEFINE CUSTOM COLUMNS | |
//--------------------------- | |
add_filter("manage_edit-question_columns", "question_define_columns"); | |
function question_define_columns($question_columns) { | |
$new_columns['cb'] = '<input type="checkbox" />'; | |
$new_columns['title'] = __('Question'); | |
$new_columns['question-category'] = __('Category'); | |
//$new_columns['question-answer'] = __('Answer'); | |
//$new_columns['author'] = __('Author'); | |
//$new_columns['comments'] = __('Comments'); | |
//$new_columns['tags'] = __('Tags'); | |
//$new_columns['date'] = __('Date'); | |
//$new_columns['hsm_pagetitle'] = __('Page title'); | |
//$new_columns['hsm_pagedescription'] = __('Page description'); | |
return $new_columns; | |
} | |
// POPULATE CUSTOM COLUMNS | |
//--------------------------- | |
add_action('manage_question_posts_custom_column', 'question_fill_columns', 10, 2); | |
function question_fill_columns($column_name, $id) { | |
global $post; | |
switch ($column_name) { | |
case 'question-category': | |
echo strip_tags(get_the_term_list( $id, 'question-category', '', ', ', '' )); | |
break; | |
default: | |
break; | |
} | |
} | |
// SORTABLE COLUMN HEADERS | |
//--------------------------- | |
//add_filter("manage_edit-question_sortable_columns", 'question_sortable_columns'); | |
//function question_sortable_columns($columns) { | |
//$custom = array( | |
//'question-category' => 'question-category', | |
//); | |
//return wp_parse_args($custom, $columns); | |
//} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment