Created
May 19, 2017 00:22
-
-
Save alexx855/9ab5b3aea1c77868bce48f50dc9605a8 to your computer and use it in GitHub Desktop.
CRUD custom taxonomy meta wordpress
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action( '{taxonomy}_add_form_fields', 'add_feature_group_field', 10, 2 ); | |
function add_feature_group_field($taxonomy) { | |
global $meta_name; | |
?> | |
<div class="form-field"> | |
<label for="meta_name">Meta Name</label> | |
<input name="meta_name" id="meta_name" type="text" value="<?php echo $meta_name; ?>" size="40" aria-required="true"> | |
</div> | |
<?php | |
} | |
add_action( 'created_{taxonomy}', 'save_feature_meta', 10, 2 ); | |
add_action( 'edited_{taxonomy}', 'save_feature_meta', 10, 2 ); | |
function save_feature_meta( $term_id, $tt_id ){ | |
if( isset( $_POST['meta_name'] ) && !empty($_POST['meta_name']) ){ | |
add_term_meta( $term_id, 'meta_key', $_POST['meta_name'], true ); | |
} | |
} | |
add_action( '{taxonomy}_edit_form_fields', 'edit_feature_group_field', 10, 2 ); | |
function edit_feature_group_field( $term, $taxonomy ){ | |
global $meta_name; | |
// get current group | |
$meta_name = get_term_meta( $term->term_id, 'meta_key', true ); | |
?> | |
<tr class="form-field"> | |
<th scope="row"><label for="meta_name">Meta Name</label></th> | |
<td><input name="meta_name" id="meta_name" type="text" value="<?php echo $meta_name ?>" size="40" aria-required="true"> | |
</td> | |
</tr> | |
<?php | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment