Skip to content

Instantly share code, notes, and snippets.

@peteroravec
Created August 14, 2014 15:42
Show Gist options
  • Save peteroravec/9a3f2cfdeb4233466cc4 to your computer and use it in GitHub Desktop.
Save peteroravec/9a3f2cfdeb4233466cc4 to your computer and use it in GitHub Desktop.
Wordpress - Custom fields in category (posts)
<?php
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// CUSTOM FIELDS IN CATEGORY - BLOG
///////////////////////////////////
// retrieve values like this:
// -------------------------
// $category_extra_fields = get_option('category_extra_fields');
// $section_subtitle = $category_extra_fields[$category->term_id]['section_subtitle'][0];
define('MY_BLOG_CATEGORY_FIELDS', 'category_extra_fields');
add_action('category_add_form_fields','post_category_edit_form_fields');
add_action('category_edit_form_fields','post_category_edit_form_fields');
add_filter('edited_terms', 'update_my_post_category_fields');
function update_my_post_category_fields($term_id) {
if($_POST['taxonomy'] == 'category'):
$tag_id = intval($_POST['tag_ID']);
$tag_extra_fields = get_option(MY_BLOG_CATEGORY_FIELDS);
$tag_extra_fields[$tag_id]['section_color'][0] = $_POST['section_color'];
$tag_extra_fields[$tag_id]['section_subtitle'][0] = $_POST['section_subtitle'];
$tag_extra_fields_term = get_option(MY_BLOG_CATEGORY_FIELDS);
$tag_extra_fields_term['term-' .$term_id]['section_color'] = $_POST['section_color'];
$tag_extra_fields_term['term-' .$term_id]['section_subtitle'] = $_POST['section_subtitle'];
update_option(MY_BLOG_CATEGORY_FIELDS, $tag_extra_fields_term);
update_option(MY_BLOG_CATEGORY_FIELDS, $tag_extra_fields);
endif;
}
function post_category_edit_form_fields () {
$tag_id = intval($_GET['tag_ID']);
$tag_extra_fields = get_option(MY_BLOG_CATEGORY_FIELDS);
$section_color = $tag_extra_fields[$tag_id]['section_color'][0];
$section_subtitle = $tag_extra_fields[$tag_id]['section_subtitle'][0];
?>
<tr class="form-field">
<th valign="top" scope="row">
<label>Subtitle</label>
</th>
<td>
<input type="text" name="section_subtitle" value="<?php echo $section_subtitle; ?>" />
</td>
</tr>
<tr class="form-field">
<th valign="top" scope="row">
<label>Section color:</label>
</th>
<td>
<input type="color" name="section_color" value="<?php echo $section_color; ?>" />
</td>
</tr>
<?php
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment