-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustom-fields-table-display.php
More file actions
233 lines (211 loc) · 7.59 KB
/
custom-fields-table-display.php
File metadata and controls
233 lines (211 loc) · 7.59 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<?php
/*
Plugin Name: Custom fields table display
Plugin URI: http://wordpress.org/extend/plugins/custom-fields-display/
Description: Display custom metadata as a table in post content. Ability to select specific custom fields; in any theme.
Version: 0.1
Author: Adam McGreggor after Shambhu Prasad Patnaik
*/
add_action('admin_menu', 'custom_fields_table_display_menus');
if (!function_exists('custom_fields_table_display_menus')):
function custom_fields_table_display_menus()
{
add_menu_page('Custom Fields Table Display', 'Custom Fields Table Display', 8, __FILE__, 'custom_fields_table_display_admin_page');
}
endif;
if (!function_exists('custom_fields_table_display_admin_page')):
function custom_fields_table_display_admin_page()
{
$plugin_page= plugin_basename(__FILE__);
$action_url=admin_url('admin.php?page='.$plugin_page);
$myCsseUrl = plugins_url('stylesheet.css', __FILE__);
global $wpdb;
$limit = (int) apply_filters( 'postmeta_form_limit', 30 );
$keys = $wpdb->get_col( "
SELECT meta_key
FROM $wpdb->postmeta
GROUP BY meta_key
HAVING meta_key NOT LIKE '\_%'
ORDER BY meta_key
LIMIT $limit" );
if ($keys)
natcasesort($keys);
$select_field =array();
$action = stripslashes_deep($_POST['update']);
if($action=='Show Now')
{
$default_option =array();
if(isset($_POST['check_box']))
{
$field_check = stripslashes_deep($_POST['check_box']);
foreach($field_check as $key =>$value)
$select_field[] = $value;
}
foreach($keys as $key)
{
$field_id = stripslashes_deep($key);
$field_label = stripslashes_deep($_POST['field_'.$field_id]);
if($field_label=='')
$field_label = stripslashes_deep($field['label']);
$default_option[$field_id] =$field_label;
}
$data =array('default_option' => $default_option,'select_field' =>$select_field);
$data=json_encode($data);
update_option('custom_fields_table_display_option',$data);
update_option('custom_fields_table_display_message','Successfully Updated.');
echo "<meta http-equiv='refresh' content='0;url=".$action_url."&message=update' />";
}
echo'<link rel="stylesheet" type="text/css" href="'.$myCsseUrl.'">';
echo'<div class="wrap">
<div id="icon-generic" class="icon32 "></div>
<h2>Custom Fields Table Display</h2><br>
<div>';
if($custom_fields_table_display_message = get_option('custom_fields_table_display_message') && !isset($_POST['update']))
{
$custom_fields_table_display_message = get_option('custom_fields_table_display_message');
echo' <div class="updated_setting"><p>'.$custom_fields_table_display_message.'</p></div> ';
update_option('custom_fields_table_display_message','');
}
$data = json_decode(get_option('custom_fields_table_display_option'));
//print_r($data);die();
$default_option = (array)$data->default_option;
$default_select = (array)$data->select_field;
?>
<h3 class="title">About</h3>
<p>Display custom metadata as a table in post content. Ability to select specific custom fields; in any theme.</p>
<SCRIPT LANGUAGE="JavaScript">
<!--
jQuery(document).ready(function(){
"use strict";
jQuery( "#field_order_box" ).sortable({placeholder: "field_content-highlight"});
});
//-->
</SCRIPT>
<?php
wp_enqueue_script("jquery-ui-sortable");
echo'<div><form method="post" action="'.$action_url.'" onsubmit="return ValidateForm(this)"><table border="0" width="98%" cellspacing="1" cellpadding="4" class="middle_table1">
<tbody id="field_order_box" >
<tr class="dataTableHeadingRow">
<th colspan="2">Show fields</th>
<th>Display Label</th>
<th>Order</th>
</tr>';
$i =0;
$data1 =array();
$total_record =count($keys);
foreach($default_select as $key =>$value)
{
if(in_array($value,$keys))
$data1[]=array('key_name'=>$value,'volume'=>$key);
}
foreach($keys as $key)
{
$field_id = $key;
if(!in_array($field_id,$default_select))
{
$data1[]=array('key_name'=>$field_id,'volume'=>$total_record);
}
}
foreach ($data1 as $key => $row) {
$volume[$key] = $row['volume'];
$name[$key] = $row['key_name'];
}
if($data1)
array_multisort($volume, SORT_ASC, $name, SORT_ASC, $data1);
foreach($data1 as $key =>$value)
{
$field_id = $value['key_name'];
$field_label = $field_id;
if(isset($default_option[$field_id]))
$field_value = $default_option[$field_id];
else
$field_value = $field_id;
if(in_array($field_id,$default_select))
$fild_checkbox = custom_fields_table_display_checkbox_field('check_box[]',$field_id, true,'', ' id="check_'.$field_id.'" ');
else
$fild_checkbox = custom_fields_table_display_checkbox_field('check_box[]',$field_id, false,'', ' id="check_'.$field_id.'" ');
echo '<tr id="'.$field_id.'" class="field_content" >
<td valign="top" >'.$fild_checkbox.'</td>
<td valign="top" width="30%"><nobr><label for="check_'.$field_id.'" >'.$field_label.'</label></nobr></td>
<td valign="top"><input type="text" name ="field_'.$field_id.'" value="'.esc_attr($field_value).'" size="40"></td>
<td><span class="ui-icon ui-icon-arrowthick-2-n-s"></span></td>
</tr>';
}
echo' <tr>
<td colspan="5" align="center"><input type="submit" name="update" value="Show Now" class="button button-primary button-large"></td>
</tr></tbody>
</table></form></div>';
echo'</div></div>';
}
endif;
if (!function_exists('custom_fields_table_display_activate')):
function custom_fields_table_display_activate()
{
update_option('custom_fields_table_display_option','');
}
endif;
if (!function_exists('custom_fields_table_display_checkbox_field')):
function custom_fields_table_display_checkbox_field($name, $value = '', $checked = false, $compare = '', $parameter = '')
{
$str = '<input type="checkbox" name="' . $name . '"';
if ($value != '')
{
$str .= ' value="' . $value . '"';
}
if ( ($checked == true) || ($value && ($value == $compare)) )
{
$str .= ' CHECKED';
}
if ($parameter != '')
{
$str .= ' ' . $parameter;
}
$str .= '>';
return $str;
}
endif;
//add_action( 'init', 'custom_fields_table_display_init' );
//function custom_fields_table_display_init()
{
add_filter( 'the_content', 'custom_fields_table_display_content_filter', 20 );
}
register_deactivation_hook(__FILE__, 'custom_fields_table_display_deactivate');
register_activation_hook(__FILE__, 'custom_fields_table_display_activate');
if (!function_exists('custom_fields_table_display_deactivate')):
function custom_fields_table_display_deactivate()
{
update_option('custom_fields_table_display_option','');
}
endif;
if ( ! function_exists( 'custom_fields_table_display_content_filter' ) ) :
function custom_fields_table_display_content_filter( $content )
{
global $post;
$data = json_decode(get_option('custom_fields_table_display_option'));
$default_option = (array)$data->default_option;
$default_select = (array)$data->select_field;
$add_content='';
if ( is_single() ):
if(count($default_select) > 0)
{
$add_content ='<table class=\'post-meta\'>';
foreach($default_select as $key)
{
$string = get_post_meta($post->ID,$key,true);
$value = make_clickable( $string );
if($value !='')
{
$add_content .='<tr>';
if(isset($default_option[$key]) && $value!='' )
$add_content .='<td><span class=\'post-meta-key\'>'.($default_option[$key]).'</span></td> ';
$add_content .='</td><td>';
$add_content .='<td>'.($value).'</tr>';
}
}
$add_content .='</table>';
}
endif;
return $add_content.$content;
}
endif;
?>