forked from soflyy/wp-all-import-action-reference
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpmxi_saved_post.php
144 lines (123 loc) · 4.54 KB
/
pmxi_saved_post.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
<?php
/**
* ==================================
* Action: pmxi_saved_post
* ==================================
*
* Called after a post is created/updated by WP All Import.
*
* @param $post_id int - The id of the post just created/updated
* @param $xml_node SimpleXMLElement - An object holding values for the current record
* @param $is_update - Boolean showing whether the post is created or updated
*
*/
function my_saved_post($post_id, $xml_node, $is_update)
{
/*
* Here you can use standard WordPress functions like get_post_meta() and get_post() to
* retrieve data, make changes and then save them with update_post() and/or update_post_meta()
*
* There are two ways to access the data from the current record in your import file:
*
* 1) Custom fields. For example, you could import a value to a custom field called "_temp" and
* then retrieve it here. Since it's only temporary, you'd probably want to delete it immediately:
*
* $my_value = get_post_meta($post_id, "_temp", true);
* delete_post_meta($post_id,"_temp");
*
* 2) The $xml param (a SimpleXMLElement object). This can be complex to work with if you're not
* used to iterators and/or xpath syntax. It's usually easiest to convert it a nested array using:
*
* $record = json_decode(json_encode((array) $xml_node), 1);
*/
/*
* You can also conditionally run your code based on the import ID:
*
* $import_id = ( isset( $_GET['id'] ) ? $_GET['id'] : ( isset( $_GET['import_id'] ) ? $_GET['import_id'] : 'new' ) );
* if ( $import_id == '8' ) {
* // run code
* }
*/
}
add_action('pmxi_saved_post', 'my_saved_post', 10, 3);
// ----------------------------
// Example uses below
// ----------------------------
/**
* Append data to a custom field. Requires importing the data to be appended into
* a temporary custom field in the import.
*
* @param $id
*/
function custom_field_append($id)
{
$value = get_post_meta($id, 'your_meta_key', true);
$temp = get_post_meta($id, '_temp', true);
update_post_meta($id, 'your_meta_key', $value . $temp);
delete_post_meta($id, '_temp');
}
add_action('pmxi_saved_post', 'custom_field_append', 10, 1);
/**
* Conditionally update a custom field based on the value of a different field.
*
* @param $id
*/
function conditional_update($id)
{
$check = get_post_meta($id, '_my_update_check', true);
if ($check === 'yes') {
$new_value = get_post_meta($id, '_my_new_value', true);
update_post_meta($id, '_my_new_value', $new_value);
}
}
add_action('pmxi_saved_post', 'conditional_update', 10, 1);
/**
* Conditionally delete a custom field based on the value of a different field.
*
* @param $id
*/
function conditional_delete($id)
{
$check = get_post_meta($id, '_my_delete_check', true);
if ($check === 'yes') {
delete_post_meta($id, '_my_field');
}
}
add_action('pmxi_saved_post', 'conditional_delete', 10, 1);
/*
* In some cases it's desirable to have the featured image also appear in the product
* gallery in WooCommerce. This function will prepend the featured image to the gallery.
*/
function copy_featured_img_to_gallery($post_id)
{
$gallery = explode(",",get_post_meta($post_id, "_product_image_gallery", true));
array_unshift($gallery, get_post_thumbnail_id( $post_id ));
$gallery = array_unique($gallery);
update_post_meta($post_id, "_product_image_gallery", implode(",",$gallery));
}
add_action('pmxi_saved_post', 'copy_featured_img_to_gallery', 10, 2);
/*
* Need to import data to custom database tables along with your imported items? This should give
* you a general idea how, but would require modification for your exact needs.
*
* This is provided with the hope it will be useful but importing to custom tables is *not* officially supported.
* This is very advanced usage. It's only advised for developers with experience directly reading/writing MySQL tables.
*/
function custom_database_table_query($id)
{
global $wpdb;
$value = get_post_meta($id, '_your_temp_field', true);
$table_name = $wpdb->prefix . "your_table_name";
$wpdb->insert($table_name, array('post_id' => $id, 'column_name' => $value), array('%s','%s'));
delete_post_meta($id, '_your_temp_field');
}
/*
* Only perform an action if the post is created
*
*/
function soflyy_save_post( $post_id, $xml, $is_update ) {
if ( !$is_update ) {
// the post is being created, rather than updated.
}
}
add_action( 'pmxi_saved_post', 'soflyy_save_post', 10, 3 );