Skip to content

Instantly share code, notes, and snippets.

@vishalbasnet23
Created November 17, 2014 10:06
Show Gist options
  • Select an option

  • Save vishalbasnet23/5769fe32aa1d3af484ca to your computer and use it in GitHub Desktop.

Select an option

Save vishalbasnet23/5769fe32aa1d3af484ca to your computer and use it in GitHub Desktop.
Adding Extra Field in Term Field both on Add and Edit Form WordPress
<?php
add_action( 'edited_album', 'save_extra_taxonomy_fields', 10, 2);
add_action( 'album_edit_form_fields', 'album_edit_tax_fields', 10, 2 );
//edit taxonomy update
add_action('album_edit_tax_fields','save_extra_taxonomy_fields',10,2);
function album_edit_tax_fields($tag) {
//check for existing taxonomy meta for term ID
$t_id = $tag->term_id;
$term_meta = get_option( "code_album_$t_id");
// var_dump($term_meta);
?>
<tr class="form-field">
<th scope="row" valign="top"><label for="upload_image">Album Cover</label></th>
<td>
<input id="upload_image_tax" type="text" size="36" name="term_meta[img]" value="<?php echo $term_meta["img"];?>" />
<input id="upload_image_button_tax" class="button" type="button" value="Upload Image" />
<p>Enter url or upload image.</p>
</td>
<td>
<script type="text/javascript">
jQuery('#upload_image_tax').focusout(function(){
if(jQuery('#upload_image_tax').val() == '' ) {
jQuery('#taxeditimg').attr('src','');
}
});
</script>
<div class="edittaximgdiv" id="imagediv">
<img id="taxeditimg" src="<?php if($term_meta["img"]) echo $term_meta["img"];else echo'';?>">
</div>
</td>
<br />
</tr>
<?php
}
/********************************************************/
/* this saves the fields */
/********************************************************/
add_action('created_album','save_extra_taxonomy_fields', 10, 2);
function save_extra_taxonomy_fields( $term_id ) {
if ( isset( $_POST['term_meta'] ) ) {
$t_id = $term_id;
$term_meta = get_option( "code_album_$t_id");
$cat_keys = array_keys($_POST['term_meta']);
foreach ($cat_keys as $key){
if (isset($_POST['term_meta'][$key])){
$term_meta[$key] = $_POST['term_meta'][$key];
}
}
//save the option array
update_option( "code_album_$t_id", $term_meta );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment