forked from codepress/admin-columns-hooks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathac-export-value.php
109 lines (81 loc) · 2.67 KB
/
ac-export-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
<?php
/**
* This hook allows you to alter the exported value when generating a CSV file
*/
add_filter( 'ac/export/value',function( $value, AC\Column $column, $id ) {
return $value;
}, 10, 3 );
/**
* Change the exported date format to 'Y-m-d' for a custom field.
*
* @param string $value
* @param AC\Column $column
*
* @return string
*/
function acp_export_change_export_date_format( $value, AC\Column $column ) {
if ( $column instanceof AC\Column\CustomField && 'date' === $column->get_field_type() ) {
$timestamp = ac_helper()->date->strtotime( $value );
if ( $timestamp ) {
$value = date( 'Y-m-d', $timestamp );
}
}
return $value;
}
add_filter( 'ac/export/value', 'acp_export_change_export_date_format', 10, 2 );
/**
* Alter the column value from a `Attachment ID` to an `Image URL` for an ACF field of the type `Image`.
*
* @param string $value
* @param AC\Column $column
* @param int $id
*
* @return string
*/
function acp_export_change_acf_image_export_value( $value, AC\Column $column, $id ) {
if ( $column instanceof ACA\ACF\Column && 'image' === $column->get_acf_field_option( 'type' ) ) {
// In this example `$value` is an attachment ID
$attchment_id = (int) $value;
$image_src = wp_get_attachment_image_src( $attchment_id );
// Replace `$value` with the Image URL
$value = $image_src[0];
}
return $value;
}
add_filter( 'ac/export/value', 'acp_export_change_acf_image_export_value', 10, 3 );
/**
* Example on how to strip HTML from the exported value
*
* @param string $value
* @param AC\Column $column
*
* @return string
*/
function ac_column_export_value_strip_html( $value, AC\Column $column ) {
// Check for a custom field column
if ( $column instanceof ACP\Column\CustomField ) {
// The meta key you want the HTML stripped from
$meta_key = 'my_custom_field_key';
if ( $meta_key === $column->get_meta_key() ) {
// Strips all HTML.
// Example: '<a href="#">my label</a>' will become 'my label'.
$value = strip_tags( $value );
}
}
return $value;
}
add_filter( 'ac/export/value', 'ac_column_export_value_strip_html', 10, 2 );
/**
* Change the number format to use comma (,) as its decimal separator
*/
function acp_export_change_number_format( $value, AC\Column $column ) {
// The meta key you want to apply a different number format
$meta_key = 'my_number_meta_key';
// Check for a custom field column type and the correct meta key
if ( $column instanceof \AC\Column\CustomField && $meta_key === $column->get_meta_key() ) {
// Change decimal separator to a comma
$value = number_format( $value, 2, ',', '' );
}
return $value;
}
add_filter( 'ac/export/value', 'acp_export_change_number_format', 10, 2 );