i am trying to add options with category specific names to all categories individually. I spent many hours on this and can't find a solution. This is my code:
add_action( 'category_edit_form_fields', 'my_category_custom_fields_x' );
add_action( 'edit_category', 'save_my_category_custom_fields_x' );
function my_category_custom_fields_x( $cat ) {
$category_meta = get_option( 'category_meta_'.$cat->term_id );
?>
<tr class="form-field">
<th scope="row" valign="top"><label for="category-title"><?php _e("Title"); ?></label></th>
<td>
<input id="category-title" name="category_meta_[<?php echo $cat->term_id ?>]"
value="<?php if ( isset( $category_meta[ $cat->term_id ] ) ) esc_attr_e( $category_meta[ $cat->term_id ] ); ?>" />
</td>
</tr>
<?php
}
function save_my_category_custom_fields_x() {
if ( isset( $_POST['category_meta_'] ) &&
!update_option('category_meta_'.$_POST['category_meta_'], $_POST['category_meta_']) )
add_option('category_meta_'.$_POST['category_meta_'], $_POST['category_meta_']);
}
This is the result in the database: For option name i get:
category_meta_Array
and for option value:
a:1:{i:7;s:5:"testX";}
How do i get the category id to show in the option name? The $_POST object is an array, right? How can i get hold of $cat->term_id? Isn't it part of the array?
category_meta_7
With the above function the input is always empty. How do i show 'textX' in the input field when the option is saved on edit_category?