View difference between Paste ID: MzDpi9GE and 976LaLtR
SHOW: | | - or go back to the newest paste.
1
// Add term page
2
function pippin_taxonomy_add_new_meta_field() {
3
	// this will add the custom meta field to the add new term page
4
	?>
5
	
6
	<div class="form-field">
7
		<label for="term_meta[hex]"><?php _e( 'Hex Color', 'pippen' ); ?></label>
8
		<input type="text" name="term_meta[hex]" id="term_meta[hex]" value="">
9
		<p class="description"><?php _e( 'Enter a value for this field','pippen' ); ?></p>
10
	</div>
11
	
12
	<div class="form-field">
13
		<label for="term_meta[former_employee]"><?php _e( 'Former Employee?', 'pippen' ); ?></label>
14
		<input type="checkbox" name="term_meta[former_employee]" value="<?php echo esc_attr( $term_meta['former_employee'] ) ? esc_attr( $term_meta['former_employee'] ) : ''; ?>" <?php checked( $term_meta['former_employee'], 1 ); ?> />
15
		<p class="description"><?php _e( 'Enter a value for this field','pippen' ); ?></p>
16
	</div>
17
	
18
<?php
19
}
20
// {$taxonomy_name}_add_form_fields, function, priority
21
add_action( 'myplugin_employees_add_form_fields', 'pippin_taxonomy_add_new_meta_field', 10, 2 );
22
23
24
25
26
27
28
// Edit term page
29
function pippin_taxonomy_edit_meta_field($term) {
30
 
31
	// put the term ID into a variable
32
	$t_id = $term->term_id;
33
 
34
	// retrieve the existing value(s) for this meta field. This returns an array
35
	$term_meta = get_option( "taxonomy_$t_id" ); 
36
?>
37
38
39
40
	<tr class="form-field">
41
	<th scope="row" valign="top">
42
	  <label for="term_meta[hex]"><?php _e( 'Hex Color', 'pippen' ); ?></label>
43
	</th>
44
		<td>
45
			<input type="text" name="term_meta[hex]" id="term_meta[hex]" value="<?php echo esc_attr( $term_meta['hex'] ) ? esc_attr( $term_meta['hex'] ) : ''; ?>">
46
			<p class="description"><?php _e( 'Enter a value for this field','pippen' ); ?></p>
47
		</td>
48
	</tr>
49
50
	<tr class="form-field">
51
	<th scope="row" valign="top">
52
	  <label for="term_meta[former_employee]"><?php _e( 'Former Employee?', 'pippen' ); ?></label>
53
	</th>
54
		<td>
55
			<input type="checkbox" name="options[postlink]" value="<?php echo esc_attr( $term_meta['former_employee'] ) ? esc_attr( $term_meta['former_employee'] ) : ''; ?>" <?php checked( $term_meta['former_employee'], 1 ); ?> />		
56
			<p class="description"><?php _e( 'Enter a value for this field','pippen' ); ?></p>
57
		</td>
58
	</tr>
59
	
60
<?php
61
}
62
// {$taxonomy_name}_edit_form_fields, function, priority
63
add_action( 'myplugin_employees_edit_form_fields', 'pippin_taxonomy_edit_meta_field', 10, 2 );
64
65
66
 
67
68
// Save extra taxonomy fields callback function.
69
function save_taxonomy_custom_meta( $term_id ) {
70
	if ( isset( $_POST['term_meta'] ) ) {
71
		$t_id = $term_id;
72
		$term_meta = get_option( "taxonomy_$t_id" );
73
		if ( ! isset( $_POST['term_meta']['former_employee'] ) ) {
74
			$_POST['term_meta']['former_employee'] = '';
75
		}
76
		$cat_keys = array_keys( $_POST['term_meta'] );
77
		foreach ( $cat_keys as $key ) {
78
			if ( isset ( $_POST['term_meta'][$key] ) ) {
79
				$term_meta[$key] = $_POST['term_meta'][$key];
80
			}
81
		}
82
		// Save the option array.
83
		update_option( "taxonomy_$t_id", $term_meta );
84
	}
85
}
86
// edited_{$taxonomy_name}, function, priority
87
// create_{$taxonomy_name}, function, priority
88
add_action( 'edited_myplugin_employees', 'save_taxonomy_custom_meta', 10, 2 );  
89
add_action( 'create_myplugin_employees', 'save_taxonomy_custom_meta', 10, 2 );