Wordpress How-To

How to Add Custom Fields in WordPress Category Form

Custom fields in WordPress Category form

custom-field

As you can see in the above image i have added Image URL field in Category form. In this article will see how to add these extra fields in WordPress category form. The first step is to edit active theme’s Functions.php page.

i,e : Goto active Theme directory and open functions.php file and paste the following codes below.

 

function category_fields_new( $taxonomy ) { // Function has one field to pass – Taxonomy
wp_nonce_field( 'category_meta_new', 'category_meta_new_nonce' ); // Create a Nonce so that we can verify the integrity of our data
?>
<label for='category_fa'>Image Url</label>
<input type='text' id='imgUrl' name='imgUrll' style='width: 100%'>
<p class='description'>Enter Image Url </p>
<?php
}
add_action( 'category_add_form_fields', 'category_fields_new', 10 );

 

The above codes will add extra field in category form, same field we have to add in category edit options also for that paste the below code.

 

function category_fields_edit( $term, $taxonomy ) {
wp_nonce_field( 'category_meta_edit', 'category_meta_edit_nonce' ); // Create a Nonce so that we can verify the integrity of our data
$category_imageUrl = get_option( '{$taxonomy}_{$term->term_id}_imageUrl' ); // Get the imageUrl if one is set already
?>
<tr class='form-field'>
<th scope='row' valign='top'>
<label for='category_fa'>Image Url</label>
</th>
<td>
<input name='imgUrll' id='category_fa' type='text' 
value='<?php echo ( ! empty( $category_imageUrl ) ) ? $category_imageUrl : ; ?>' style='width:100%;' />
<p class='description'>Enter Image URL</p>
</td>
</tr>

<?php
}
add_action( 'category_edit_form_fields', 'category_fields_edit', 10, 2 );

 

The last and final step is to save this values in database

 

function save_category_fields( $term_id ) {

$taxonomy = $_POST['taxonomy'];
// $category_imageUrl = get_option( “{$taxonomy}_{$term_id}_imageUrl” ); // Grab our imageUrl if one exists

if( ! empty( $_POST['imgUrll'] ) ) { // IF the user has entered text, update our field.
update_option( '{$taxonomy}_{$term_id}_imageUrl', $_POST['imgUrll'] ); // Sanitize our data before adding to the database
} elseif( ! empty( $category_imageUrl ) ) { // Category imageUrl IS empty but the option is set, they may not want an imageUrl on this category
delete_option( '{$taxonomy}_{$term_id}_imageUrl' ); // Delete our option
}

} // End Function
add_action ( 'created_category', 'save_category_fields' );
add_action ( 'edited_category', 'save_category_fields' );

 

In this  “{$taxonomy}_{$term_id}_imageUrl ” ( category_category_id_refrence-string)  format i’m going to save in the database.

You can now save your changes and visit the Categories where you added the custom field to see it in action.

 

 

That’s all, we hope this article helped you to learn more about Custom Fields in WordPress Category Form.

If you liked this article, then please subscribe to our updates and YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

About the author

Chetan Hegde

Chetan Hegde

Chetan Hegde is the architect of ProCodersOnline.com. I'm a Software Engineer and technology enthusiast person who loves to learn and explore new things and technologies. When i'm not writing content for my websites, I love to travel and explore the nature.

Add Comment

Click here to post a comment