Mobile-Menu iFuturz Infoweb Inc. Contact Portfolio

29

Jun

How to create Custom Fields in Category page without using plugin in wordpress?

How to create Custom Fields in Category page without using plugin in wordpress?

Posted On : June 29, 2013

| No Comment

There are plenty of great plugins to automate this task like “Category Custom Fields Plugin”. But it is not suggested to use plugins for small tasks, as more plugin used can slow down your website and also all plugins does not guarantee security of your website.

So, we found to complete this task using simple coding in themes function.php file.

Code

//add_action() hook to call functions

add_action( ‘edit_category_form_fields’, ‘my_category_custom_fields’ );

add_action( ‘edit_category’, ‘save_my_category_custom_fields’ );


//function for creating field
function my_category_custom_fields( $tag ) {

// your custom field HTML will be here

// the $tag variable is a taxonomy term object with properties like $tag->name, $tag->term_id

// we need to know the values of our existing entries if any

$category_meta = get_option( ‘category_meta’ );

?>

//create custom field to add for category page.

<tr class=”form-field”>

<th scope=”row” valign=”top”><label for=”category-title”><?php _e(“Breadcrum for category”);  ?></label></th>

<td>

<textarea name=”category_meta”   id=”category-title”><?php if ( isset( $category_meta[ $tag->term_id ] ) ) esc_attr_e( $category_meta[ $tag->term_id]); ?></textarea>

</td>

</tr>

<!– repeat for other fields if needed –>

<?php

}

//function to save data in database that entered in custom fields.

function save_my_category_custom_fields() {

if ( isset( $_POST['category_meta'] )){

$category_meta = get_option( ‘category_meta’ );

$category_meta[$_POST["tag_ID"]]=$_POST['category_meta'];

update_option(‘category_meta’, $category_meta);

}

}

//code to display custom field content on category page. (add in themes category.php file)

<?php

$categories = get_category(get_query_var(‘cat’));

$current_catId = $categories->term_id;

$category_meta = get_option(‘category_meta’);

echo $category_meta[$current_catId];

?>

Screenshot of CustomField.


  • Tags:

Comment