Report abuse

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<?php

$new_meta_boxes = 
array(
"area" => array(
"name" => "area",
"type" => "input",
"std" => "Unknown",
"title" => "Area",
"description" => "Where is the person located? (ex: Los Angeles, CA)"),

"age" => array(
"name" => "age",
"type" => "input",
"std" => "Unknown",
"title" => "Age",
"description" => "How old is this person"),

"country" => array(
"name" => "country",
"type" => "select",
"std" => "USA",
"title" => "Country",
"options" => array("USA","Europe","Asia","Australia"),
"description" => "Country of origin.  This should be obvious"),

"attachments" => array(
"name" => "attachments",
"type" => "attachment",
"std" => "",
"title" => "Attachment",
"description" => "Choose an attachment")
);

$new_meta_boxes_2 = 
array(
"image" => array(
"name" => "image",
"type" => "input",
"std" => "",
"title" => "Image",
"description" => "Paste image URL, ex: http://www.mydomain/folder/my_image_name.jpg"),

"imgalt" => array(
"name" => "imgalt",
"type" => "input",
"std" => "",
"title" => "Image Description",
"description" => "Enter a description for the image (alt tag).")
);


function new_meta_boxes() {
global $post, $new_meta_boxes, $new_meta_boxes_2;
	
	foreach($new_meta_boxes as $meta_box) {
		
		echo'<input type="hidden" name="'.$meta_box['name'].'_noncename" id="'.$meta_box['name'].'_noncename" value="'.wp_create_nonce( plugin_basename(__FILE__) ).'" />';
		
		echo'<h2>'.$meta_box['title'].'</h2>';
		
		if( $meta_box['type'] == "input" ) { 
		
			$meta_box_value = get_post_meta($post->ID, $meta_box['name'].'_value', true);
		
			if($meta_box_value == "")
				$meta_box_value = $meta_box['std'];
		
			echo'<input type="text" name="'.$meta_box['name'].'_value" value="'.$meta_box_value.'" size="55" /><br />';
			
		} elseif ( $meta_box['type'] == "select" ) {
			
			echo'<select name="'.$meta_box['name'].'_value">';
			
			foreach ($meta_box['options'] as $option) {

				echo'<option';
				if ( get_post_meta($post->ID, $meta_box['name'].'_value', true) == $option ) { 
					echo ' selected="selected"'; 
				} elseif ( $option == $meta_box['std'] ) { 
					echo ' selected="selected"'; 
				} 
				echo'>'. $option .'</option>';
			
			}
			
			echo'</select>';
			
		} elseif ( $meta_box['type'] == "attachment" ) {
			
			echo'<select name="'.$meta_box['name'].'_value">';
			$outer_post = $post->ID;
			$args = array(
				'post_type' => 'attachment',
				'numberposts' => -1,
				'post_status' => null,
				'post_parent' => $post->ID // any parent
			); 
			$attachments = get_posts($args);
			
			if ($attachments) {
				foreach ($attachments as $post) {
					setup_postdata($post);
					$option = $post->guid;
					echo'<option value="';
						if ( get_post_meta($outer_post, $meta_box['name'].'_value', true) == $option ) { 
							echo ' selected="selected"'; 
						} elseif ( $option == $meta_box['std'] ) { 
							echo ' selected="selected"'; 
						} 
					echo $option .'">'.$post->post_title.'</option>';
					
				}
			}
			
			echo'</select>';
			
		}  
		
		echo'<p><label for="'.$meta_box['name'].'_value">'.$meta_box['description'].'</label></p>';
	}

}

function new_meta_boxes_2() {
global $post, $new_meta_boxes, $new_meta_boxes_2;
	
	foreach($new_meta_boxes_2 as $meta_box) {
		
		echo'<input type="hidden" name="'.$meta_box['name'].'_noncename" id="'.$meta_box['name'].'_noncename" value="'.wp_create_nonce( plugin_basename(__FILE__) ).'" />';
		
		echo'<h2>'.$meta_box['title'].'</h2>';
		
		if( $meta_box['type'] == "input" ) { 
		
			$meta_box_value = get_post_meta($post->ID, $meta_box['name'].'_value', true);
		
			if($meta_box_value == "")
				$meta_box_value = $meta_box['std'];
		
			echo'<input type="text" name="'.$meta_box['name'].'_value" value="'.$meta_box_value.'" size="55" /><br />';
			
		} elseif ( $meta_box['type'] == "select" ) {
			
			echo'<select name="'.$meta_box['name'].'_value">';
			
			foreach ($meta_box['options'] as $option) {

				echo'<option';
				if ( get_post_meta($post->ID, $meta_box['name'].'_value', true) == $option ) { 
					echo ' selected="selected"'; 
				} elseif ( $option == $meta_box['std'] ) { 
					echo ' selected="selected"'; 
				} 
				echo'>'. $option .'</option>';
			
			}
			
			echo'</select>';
			
		}
		
		
		
		
		
		echo'<p><label for="'.$meta_box['name'].'_value">'.$meta_box['description'].'</label></p>';
	}

}

function create_meta_box() {
global $theme_name, $new_meta_boxes, $new_meta_boxes_2;
	if (function_exists('add_meta_box') ) {
	add_meta_box( 'new-meta-boxes', 'More Info', 'new_meta_boxes', 'movies', 'normal', 'high' );
	add_meta_box( 'new-meta-boxes_2', 'Images', 'new_meta_boxes_2', 'page', 'normal', 'high' );
	}
}

	function save_postdata( $post_id ) {
	global $post, $new_meta_boxes;  
		foreach($new_meta_boxes as $meta_box) {  
		
			// Verify  
			if ( !wp_verify_nonce( $_POST[$meta_box['name'].'_noncename'], plugin_basename(__FILE__) )) {  
			return $post_id;  
			}  
		
		if ( 'page' == $_POST['post_type'] ) {  
			if ( !current_user_can( 'edit_page', $post_id ))  
				return $post_id;  
		} else {  
			if ( !current_user_can( 'edit_post', $post_id ))  
				return $post_id;  
		}  
		
		$data = $_POST[$meta_box['name'].'_value'];  
		
		if(get_post_meta($post_id, $meta_box['name'].'_value') == "")  
			add_post_meta($post_id, $meta_box['name'].'_value', $data, true);  
		elseif($data != get_post_meta($post_id, $meta_box['name'].'_value', true))  
			update_post_meta($post_id, $meta_box['name'].'_value', $data);  
		elseif($data == "")  
			delete_post_meta($post_id, $meta_box['name'].'_value', get_post_meta($post_id, $meta_box['name'].'_value', true));  
	}
}

add_action('admin_menu', 'create_meta_box');
add_action('save_post', 'save_postdata');



?>