Custom Field in Taxonomy

Để add custom field trong taxonomy edit page chúng ta sử dụng action: {taxonomy_name}_add_form_fields và {taxonomy_name}_edit_form

ví dụ ở đây ta có taxonomy là “test”:

[code]add_action(‘test_add_form_fields’,’tr_test_students_add_form_fields’);
add_action(‘test_edit_form’,’tr_test_students_add_form_fields’);
function tr_test_students_add_form_fields($tag = null, $taxonomy = ‘test’)
{
$tr_tag = get_option(‘tr_tag_test’.$tag->term_id,true);
?>
<div>
<label for=”tag-instructions”>Instructions</label>
<textarea cols=”40″ rows=”5″ id=”tag-instructions” name=”tr_tag[instructions]”><?php echo $tr_tag[‘instructions’]?></textarea>
</div>
<div>
<label for=”tag-instructions”>Score Pass</label>
<input type=”text” id=”tag-score_pass” name=”tr_tag[score_pass]” value=”<?php echo $tr_tag[‘score_pass’]?>” /> %
</div>

<?php
}

[/code]

Khi save những giá trị này chúng ta sẽ gọi action: created_{taxonomy_name} vả edited_{taxonomy_name}

ví dụ:

[code]

add_action( ‘created_test’, ‘tr_test_students_edited_form_fields’,10,2 );
add_action( ‘edited_test’, ‘tr_test_students_edited_form_fields’,10,2 );

function tr_test_students_edited_form_fields( $term_id, $tt_id ) {

if(isset($_POST[‘tr_tag’]))
{
$tr_tag_test = array();
foreach($_POST[‘tr_tag’] as $key => $vl)
{
$tr_tag_test[$key] = is_array($vl)? $vl : stripslashes($vl);
}

update_option(‘tr_tag_test’.$term_id,$tr_tag_test);
}
}

[/code]

Ngoài ra chúng ta cần delete option khi delete taxonomy

[code]

add_action(‘delete_test’,’tr_test_students_delete_test’,10,2);
function tr_test_students_delete_test( $term, $tt_id)
{
delete_option(‘tr_tag_test’.$term);
}

[/code]

,

No comments yet.

Leave a Reply