Customizing WordPress themes is very interesting. In this article I will show you how to add custom field to WordPress category management screen in the admin panel without using any WordPress plugins.

Following category screen shows the default view of the WordPress category maintenance screen in the admin panel.

Add Category Screen

 

Default wordpress add category screen - How to add custom field to wordpress category
Default WordPress add category screen

Edit Category Screen

 

default wordpress edit category screen - How to add custom field to wordpress category
Default WordPress edit category screen

While I was managing my WordPress website, I encounted a problem of missing SEO keywords in WordPress categories. I was using Yoast SEO plugin and at times this plugin moved certain features into Premium version only. So I had to find a way of including keywords to WordPress categories and display in the page header.

The best solution would be customizing your theme to add the missing fields. Let’s see how to do this by adding the meta keywords box.

How to add custom field to WordPress category?

First create a php file in your theme folder called category_meta_fields.php. Add a reference to it in the functions.php as follows;

include 'category-meta-fields.php';

 

How to Display custom input control in WordPress category screen?

Add the following function to above category_meta_fields.php file.

function wecode_category_fields($term) {
    // checking the name of the action to render different outputs
    if (current_filter() == 'category_edit_form_fields') {	
        $keywords = get_term_meta( $term->term_id, 'term_keywords', true ); 	
        ?>
        <tr class="form-field">
            <th valign="top" scope="row"><label for="keywords"><?php _e('Keywords'); ?></label></th>
            <td>
                <input type="text" size="40" value="<?php echo esc_attr( $keywords ) ? esc_attr( $keywords ) : ''; ?>"  name="keywords"><br/>
                <span class="description"><?php _e('Please enter your keywords'); ?></span>
            </td>
        </tr>   
    <?php } elseif (current_filter() == 'category_add_form_fields') {
        ?>
        <div class="form-field">
            <label for="keywords"><?php _e('Keywords'); ?></label>
            <input type="text" size="40" value=""  name="keywords">
            <p class="description"><?php _e('Please enter your keywords'); ?></p>
        </div>  
    <?php
    }
}

This function will render keyword input box based on the current filter. With current_filter() function will determine add or edit category screen. In the edit section we are retrieving the saved category keywords as follows;

$keywords = get_term_meta( $term->term_id, 'term_keywords', true );

get_term_meta() is a WordPress built in function. Using this function we can get the existing keyword value. For that we need to pass the parameters category ID (i.e. $term->term_id), meta key reference (i.e. ‘term_keywords’) and return single value as true. For meta key reference you can give any name you want. Rest of the code is self explanatory.

Still, don’t see any of your custom fields in category admin screens? How do you do it? By adding following lines of code under the function that we created above;

Add the following line; where you can enable your custom field in the add category screen.

add_action('category_add_form_fields', 'wecode_category_fields', 10, 2);

 

Default wordpress add category screen with custom field
Default WordPress add category screen with custom field

Add the following line of code, you can enable custom field in the edit category screen.

add_action('category_edit_form_fields', 'wecode_category_fields', 10, 2);

 

Wordpress edit category screen with custom field
WordPress edit category screen with custom field

 

How to save custom field value to WordPress database?

Even though you add or edit a category and hit save button nothing gets saved to database yet. How you can save the custom field value to WordPress database? Simply by using the following function and WordPress application hooks;

To save the custom field value in the WordPress category screen use the following code;

function wecode_save_category_fields($term_id) {
    if ( isset( $_REQUEST['keywords'] ) ) {
        $term_keywords = $_REQUEST['keywords'];
        if( $term_keywords ) {
                update_term_meta( $term_id, 'term_keywords', $term_keywords );
        }
    } 	
}
//Enable saving custom field in the edit screen
add_action('edited_category', 'wecode_save_category_fields', 10, 2);

//Enable saving custom field in the add screen
add_action('create_category', 'wecode_save_category_fields', 10, 2);

Now, adding or editing an existing category, you can add keywords to category and save them in to WordPress database.

wordpress edit category screen with custom field with saved value
WordPress edit category screen with custom field and saved value

 

How to show keywords in WordPress header category pages?

The newly added custom field to WordPress category will save it’s values into wp_term_meta table. By using get_term_meta(), is_categry() and wp_head wordpress hook we can display these category keywords in the header section for category pages as follows;

function display_category_key_words(){	
    if(is_category()){
        //show category keywords
        $termid = get_queried_object()->term_id;			
        $key_words =get_term_meta( $termid, 'term_keywords', true );			
    }    
    echo '<meta name="keywords" content="'. $key_words.'" >';   
}
add_action('wp_head','display_category_key_words',1);

The above mentioned function will first check whether the page is a category page or not. Then it uses get_queried_object() wordpress functions to fetch the current category. Then we use get_term_meta() function to fetch the keywords that we saved, using newly added custom field to WordPress category screen. Then we render the header meta keywords using wp_head() and add_action(). Browse your website now and view source. In the header section you can notice your new keywords. So, this is how to add custom field to WordPress category.

You can download the full code here