-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathpmxi_custom_field.php
70 lines (61 loc) · 2.24 KB
/
pmxi_custom_field.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
<?php
/**
* ==================================
* Filter: pmxi_custom_field
* ==================================
*
* Custom field values can be filtered before save using this hook.
*
* [IMPORTANT NOTE]: THE $original_value parameter is new in WP All Import v4.5.6-beta-4.8 and should be excluded
* in older versions of WP All Import.
*
* @param $value string - The new custom field value from the data file
* @param $post_id int - The id of the post
* @param $key string - The custom field key
* @param $original_value string - Original, unserialized, value. (NOTE: NEW IN WP All Import v4.5.6-beta-4.8)
* @param $existing_meta_keys mixed - ??? TODO: Document
* @param $import_id int - The id of the import
*
* @return mixed
*/
function my_custom_field($value, $post_id, $key, $original_value, $existing_meta_keys, $import_id)
{
// Unless you want this code to execute for every import, check the import id
// if ($import_id === 5) { ... }
return $value;
}
add_filter('pmxi_custom_field', 'my_custom_field', 10, 6);
// ----------------------------
// Example uses below
// ----------------------------
/**
* Only update the custom field if the new value is not empty
*
*
*/
function keep_existing_if_empty($value, $post_id, $key, $original_value, $existing_meta, $import_id)
{
if ($key == 'ENTER-YOUR-CUSTOM-FIELD-NAME-HERE') {
if (empty($value)) {
$value = isset($existing_meta[$key][0]) ? $existing_meta[$key][0] : $value;
}
}
return $value;
}
add_filter('pmxi_custom_field', 'keep_existing_if_empty', 10, 6);
/**
* Only update the custom field if it's currently empty
* This code has only been lightly tested/reviewed and is not guaranteed to be completely bug free. Backup first!
*/
function update_existing_if_empty($value, $post_id, $key, $existing_meta, $import_id)
{
if ($import_id == 5) { // ENTER THE IMPORT ID HERE
if ($key == 'ENTER-YOUR-CUSTOM-FIELD-NAME-HERE') {
if (!isset($existing_meta[$key][0]) || empty($existing_meta[$key][0])) {
$value = $existing_meta[$key][0];
}
}
}
return $value;
}
add_filter('pmxi_custom_field', 'update_existing_if_empty', 10, 5);