7

I need a plugin that adds the featured image option for categories, but I can't code it myself.

4 Answers 4

10

I had missed that you can easily add fields to taxonomy terms with Advanced Custom Fields: https://www.advancedcustomfields.com/resources/get-values-from-a-taxonomy-term/

1

There are already some solutions out there for adding custom fields to taxonomy. See: Add custom field to Category

You will see a 2016 update near the bottom that should help you.

With this you can simply copy an image url once you have uploaded it to your Media Library but I am assuming you want an Upload Media Modal (popup window) utilizing wp.media right? You will have to do that with javascript. This post explains several methods in which to instantiate a media uploader modal: https://mikejolley.com/2012/12/21/using-the-new-wordpress-3-5-media-uploader-in-plugins/

1

Not everything needs a plugin.

If you want to add a simple field to add IMG URL use add_term_meta(), the update_term_meta() and get_term_meta() WP functions.

function addTitleFieldToCat(){
    $cat_title = get_term_meta($_POST['tag_ID'], '_pagetitle', true);
    ?> 
    <tr class="form-field">
        <th scope="row" valign="top"><label for="cat_page_title"><?php _e('Category Page Title'); ?></label></th>
        <td>
        <input type="text" name="cat_title" id="cat_title" value="<?php echo $cat_title ?>"><br />
            <span class="description"><?php _e('Title for the Category '); ?></span>
        </td>
    </tr>
    <?php

}
add_action ( 'edit_category_form_fields', 'addTitleFieldToCat');

function saveCategoryFields() {
    if ( isset( $_POST['cat_title'] ) ) {
        update_term_meta($_POST['tag_ID'], '_pagetitle', $_POST['cat_title']);
    }
}
add_action ( 'edited_category', 'saveCategoryFields');

src: Luke Simmons answer from: Add custom field to Category

1
<?php

add_action ( 'category_edit_form_fields', 'addTitleFieldToCat', 10, 2 );
function addTitleFieldToCat( $tag, $taxonomy ) {
    $cat_title = get_term_meta( $tag->term_id, '_pagetitle', true );
    ?>
    <tr class="form-field">
        <th scope="row" valign="top"><label for="cat_page_title"><?php _e('Category Page Title'); ?></label></th>
        <td>
            <input type="text" name="cat_title" id="cat_page_title" value="<?php echo $cat_title ?>"><br />
            <p class="description"><?php _e('Title for the category'); ?></p>
        </td>
    </tr>
    <?php
} 

add_action ( 'edited_category', 'saveCategoryFields' );
function saveCategoryFields() {
    if ( isset( $_POST['cat_title'] ) ) {
        update_term_meta( $_POST['tag_ID'], '_pagetitle', $_POST['cat_title'] );
    }
}

Code modifications suggested by user @talk-nerdy-to-me - https://wordpress.stackexchange.com/users/122776/talk-nerdy-to-me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.