-
Notifications
You must be signed in to change notification settings - Fork 5
/
ac-column-value.php
210 lines (179 loc) · 6.67 KB
/
ac-column-value.php
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
<?php
/**
* The filter `ac/column/value` allows you to alter the value of a column cell
* You probably want to check for a specific column instance and check for extra conditionals related to the column in order to change the value for the correct column
*/
/**
* @param string $value The column value that is displayed within a cell on the list table
* @param int $id Post ID, User ID, Comment ID, Attachement ID or Term ID
* @param AC\Column $column Column object
*
* @return string
*/
function ac_column_value_usage($value, $id, AC\Column $column)
{
// Change the rendered column value
// $value = 'new value';
return $value;
}
add_filter('ac/column/value', 'ac_column_value_usage', 10, 3);
/** Shorter notation */
add_filter('ac/column/value', function ($value, $id, AC\Column $column) {
return $value;
}, 10, 3);
/**
* Example on how to wrap the value of a specific Custom Field column of the type 'color' in markup to give it a background color.
*
* @param string $value Column value
* @param int $id Post ID, User ID, Comment ID, Attachement ID or Term ID
* @param AC\Column $column Column object
*
* @return string
*/
function ac_column_value_custom_field_example($value, $id, AC\Column $column)
{
if ($column instanceof AC\Column\CustomField) {
// Custom Field Key
$meta_key = $column->get_meta_key();
// Custom Field Type can be 'excerpt|color|date|numeric|image|has_content|link|checkmark|library_id|title_by_id|user_by_id|array|count'. The default is ''.
$custom_field_type = $column->get_field_type();
if (
'my_hex_color' === $meta_key
&& 'color' === $custom_field_type
) {
$value = sprintf('<span style="background-color: %1$s">%1$s</span>', $value);
}
}
return $value;
}
add_filter('ac/column/value', 'ac_column_value_custom_field_example', 10, 3);
/**
* Example on how to add a `class` attribute to the rendered value that can be styled by CSS.
*
* @param string $value Column value
* @param int $id Post ID, User ID, Comment ID, Attachement ID or Term ID
* @param AC\Column $column Column object
*
* @return string
*/
function ac_column_value_add_class_attribute_based_on_value($value, $id, AC\Column $column)
{
if ($column instanceof AC\Column\CustomField) {
// Add a unqiue `class` attribute to the rendered value.
if ('my_custom_field_key' === $column->get_meta_key()) {
$value = sprintf(
'<span class="%s %s">%s</span>',
esc_attr('column-' . $column->get_name()), // Add the column name to the `class` attribute
esc_attr('value-' . sanitize_key($value)), // Add a sanitized column value to the `class` attribute
$value
);
}
}
return $value;
}
add_filter('ac/column/value', 'ac_column_value_add_class_attribute_based_on_value', 10, 3);
/**
* Example on how change the rendered custom field value on the 'Page' list table.
*
* @param string $value Column value
* @param int $id Post ID, User ID, Comment ID, Attachement ID or Term ID
* @param AC\Column $column Column object
*
* @return string
*/
function ac_column_value_display_word_count($value, $id, AC\Column $column)
{
if ($column instanceof AC\Column\CustomField) {
// Post type
$post_type = $column->get_post_type();
// Custom Field Key
$meta_key = $column->get_meta_key();
if (
'page' === $post_type
&& 'my_custom_text_field' === $meta_key
) {
// We use our own utility method to count the number of words. But feel free to use your own logic.
$value = ac_helper()->string->word_count($value);
}
}
return $value;
}
add_filter('ac/column/value', 'ac_column_value_display_word_count', 10, 3);
/**
* Example on how to alter the value based on an ACF Column. It defines different variables that can be used to check for specific conditionals
*
* @param string $value Column value
* @param int $id Post ID, User ID, Comment ID, Attachement ID or Term ID
* @param AC\Column $column Column object
*
* @return string
*/
function ac_column_value_acf_example($value, $id, AC\Column $column)
{
// Check for the ACF column
if ($column instanceof ACA\ACF\Column) {
/**
* @var array $acf_field Contains all ACF field information
*/
$acf_field = $column->get_acf_field();
/**
* @var string $meta_key Custom Field `meta_key`
*/
$meta_key = $column->get_meta_key();
/**
* @var string $acf_field_hash_key The unique identifier for the ACF field
*/
$acf_field_hash_key = $column->get_acf_field_option('key');
/**
* @var string $acf_field_type 'text|number|url|radio|post_object|link|wysiwyg'
* @see ACA\ACF\FieldType Contains the complete list of available ACF field types
*/
$acf_field_type = $column->get_acf_field_option('type');
// Modify the rendered column value for the ACF `text` field type
if (
'my_custom_field_key' === $meta_key
&& 'text' === $acf_field_type
) {
// In this example we will append a string
$value .= ' ( Additional Text )';
}
}
return $value;
}
add_filter('ac/column/value', 'ac_column_value_acf_example', 10, 3);
/**
* Example to target the taxonomy column and prefix it with the taxonomy name
*
* @param string $value Column value
* @param int $id Post ID, User ID, Comment ID, Attachement ID or Term ID
* @param AC\Column $column Column object
*
* @return string
*/
function ac_column_value_taxonomy_example($value, $id, AC\Column $column)
{
if ($column instanceof AC\Column\Post\Taxonomy) {
$value = $column->get_taxonomy() . ' > ' . $value;
}
return $value;
}
add_filter('ac/column/value', 'ac_column_value_taxonomy_example', 10, 3);
/**
* Example to wrap a custom field columns value in a link tag
*
* @param string $value Column value
* @param int $id Post ID, User ID, Comment ID, Attachement ID or Term ID
* @param AC\Column $column Column object
*
* @return string
*/
function ac_column_value_wrap_custom_field_in_post_link($value, $id, AC\Column $column)
{
if ($column->get_list_screen(
) instanceof AC\ListScreen\Post && $column instanceof AC\Column\CustomField && 'my_own_key' === $column->get_meta_key(
)) {
$value = sprintf('<a href="%s">%s</a>', get_permalink($id), $value);
}
return $value;
}
add_filter('ac/column/value', 'ac_column_value_wrap_custom_field_in_post_link', 10, 3);