Created
October 21, 2016 03:10
-
-
Save anilmeena/c05bb64a5bb35d860b523076b47e9c55 to your computer and use it in GitHub Desktop.
Add Custom Fields in Wordpress Post Category
This file contains hidden or 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
| <?php | |
| /* Add Custom Fields in Wordpress Post Category */ | |
| // the option name | |
| define('MY_CATEGORY_FIELDS', 'my_category_fields_option'); | |
| // your fields (the form) | |
| add_filter('edit_category_form', 'my_category_fields'); | |
| function my_category_fields($tag) { | |
| $tag_extra_fields = get_option(MY_CATEGORY_FIELDS); | |
| ?> | |
| <table class="form-table"> | |
| <tr class="form-field"> | |
| <th scope="row" valign="top"><label for="_ce4-categoryTitle">Full Category Title</label></th> | |
| <td><input name="_ce4-categoryTitle" id="_ce4-categoryTitle" type="text" size="40" aria-required="false" value="<?php echo $tag_extra_fields[$tag->term_id]['my_title']; ?>" /> | |
| <p class="description">The title is optional but will be used in place of the name on the home page category index.</p></td> | |
| </tr> | |
| <tr class="form-field"> | |
| <th scope="row" valign="top"><label for="_ce4_fullDescription">Full Category Text for Landing Page</label></th> | |
| <td><textarea style="height:70px; width:100%;margin-left:-5px;" name="_ce4_fullDescription" id="_ce4_fullDescription"><?php echo $tag_extra_fields[$tag->term_id]['my_description']; ?></textarea> | |
| <p class="description">This text will appear on the category landing page when viewing all articles in a category. The image, you supply above, if any, will be used here and this content will wrap around it.</p></td> | |
| </tr> | |
| </table> | |
| <?php | |
| } | |
| // when the form gets submitted, and the category gets updated (in your case the option will get updated with the values of your custom fields above | |
| add_filter('edited_terms', 'update_my_category_fields'); | |
| function update_my_category_fields($term_id) { | |
| if($_POST['taxonomy'] == 'category'): | |
| $tag_extra_fields = get_option(MY_CATEGORY_FIELDS); | |
| $tag_extra_fields[$term_id]['my_title'] = strip_tags($_POST['_ce4-categoryTitle']); | |
| $tag_extra_fields[$term_id]['my_description'] = strip_tags($_POST['_ce4_fullDescription']); | |
| update_option(MY_CATEGORY_FIELDS, $tag_extra_fields); | |
| endif; | |
| } | |
| // when a category is removed | |
| add_filter('deleted_term_taxonomy', 'remove_my_category_fields'); | |
| function remove_my_category_fields($term_id) { | |
| if($_POST['taxonomy'] == 'category'): | |
| $tag_extra_fields = get_option(MY_CATEGORY_FIELDS); | |
| unset($tag_extra_fields[$term_id]); | |
| update_option(MY_CATEGORY_FIELDS, $tag_extra_fields); | |
| endif; | |
| } | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment