WordPress.org

Ready to get started?Download WordPress

Forums

Add Custom Field to Categories in Custom Post Types (4 posts)

  1. Howdy_McGee
    Member
    Posted 11 months ago #

    I'm able to add a field to the Categories, but only under Posts - as in it's not showing up under my CPT categories, I want them to Only show up under my CPT Categories. Do I need a different hook?

    Here's what I have so far:

    /** Add New Field To Category **/
    function extra_category_fields( $tag ) {
        $t_id = $tag->term_id;
        $cat_meta = get_option( "category_$t_id");
    ?>
    <tr class="form-field">
    <th scope="row" valign="top"><label for="cat_Image_url"><?php _e('Category Image Url'); ?></label></th>
    <td>
    <input type="text" name="Cat_meta[bgc]" id="colorinput" size="3" style="width:20%;" value="<?php echo $cat_meta['bgc'] ? $cat_meta['bgc'] : '#fff'; ?>" class="my-color-field" />
    <div id="colorpicker"></div><br />
                <span class="description"><?php _e(''); ?></span>
    				<br />
            </td>
    </tr>
    <?php
    }
    add_action ( 'category_add_form_fields', 'extra_category_fields');
    
    /** Save Category Meta **/
    function save_extra_category_fileds( $term_id ) {
        if ( isset( $_POST['Cat_meta'] ) ) {
            $t_id = $term_id;
            $cat_meta = get_option( "category_$t_id");
            $cat_keys = array_keys($_POST['Cat_meta']);
                foreach ($cat_keys as $key){
                if (isset($_POST['Cat_meta'][$key])){
                    $cat_meta[$key] = $_POST['Cat_meta'][$key];
                }
            }
            //save the option array
            update_option( "category_$t_id", $cat_meta );
        }
    }
    add_action ( 'edited_category', 'save_extra_category_fileds');
  2. bcworkz
    Member
    Posted 11 months ago #

    Are you saying you are not seeing the Categories page for your CPT? Then use register_taxonomy_for_object_type().

    Or that your new field is shown when the screen is from the Posts menu but not from your CPT menu? This should not be possible. The same hook is called either way.

    You can control from which menu the extra field is displayed by placing the output code inside a conditional that checks $_GET['post_type'];

  3. Howdy_McGee
    Member
    Posted 11 months ago #

    I added an extra field to the Categories Page - it's a color field as shown above. The extra field shows up in the Posts Categories page but not on my Custom Post Type Categories page.

  4. bcworkz
    Member
    Posted 11 months ago #

    Please excuse these dumb questions, I want to be sure we are not missing something simple.

    Have you checked the page source and confirmed the field is really not there, as opposed to being there and is hidden by CSS or covered by another element?

    Are your CPT categories the exact same category taxonomy as for regular posts, as opposed to a different taxonomy used for category purposes?

    Failing those, something strange is going on. When I add such a field to the category page on my installation, it consistently shows up for all CPTs that had been associated with the taxonomy. Perhaps one of your plugins is fouling things up? If you deactivate them do you still have the problem?

Reply

You must log in to post.

About this Topic