forked from soflyy/wp-all-import-action-reference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwpallimport_xml_row.php
More file actions
68 lines (58 loc) · 1.9 KB
/
wpallimport_xml_row.php
File metadata and controls
68 lines (58 loc) · 1.9 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
<?php
/**
* ==================================
* Filter: wpallimport_xml_row
* ==================================
*
* PLEASE NOTE: The XML generated by this function cannot be used in the Advanced Custom Fields Add-On's repeater "For each" fields:
* https://d.pr/i/pT8hIE
*
*
* Allows reading or modification of the data record before importing
*
* Note that this isn't just for XML imports. Data records from other formats (CSV, JSON,
* Excel) are converted to SimpleXML objects internally for processing.
*
* @param $xml_node SimpleXMLElement - An object holding values for the current record
*
* @return SimpleXMLElement
*/
function wpai_xml_row($xml_node)
{
// Modify simpleXML object as needed
return $xml_node;
}
add_filter('wpallimport_xml_row', 'wpai_xml_row', 10, 1);
// ----------------------------
// Example uses below
// ----------------------------
/**
* Example of adding a child element called "title"
*
*/
function add_title_node($node)
{
$result = $node->xpath('//mydata[1]/*[1]');
if (!empty($result[0])) {
$name = $result[0]->getName();
$node->addChild('title', $name);
}
return $node;
}
add_filter('wpallimport_xml_row', 'add_title_node', 10, 1);
/**
* Example of converting HTML or XML embeded within a specific tag into HTML usable for import
*
*/
function parse_content($node){
$result = $node->xpath('//content'); // replace this with the XPath of the node
if (!empty($result[0])) {
// Optional replacements to convert custom XML tags to HTML equivalent
$find_xml = array('section_title','section_content','section', 'texteparagraphe','titreparagraphe');
$replace_html = array('h1','p','div','p','h2');
$html = str_replace($find_xml, $replace_html, $result[0]->asXML());
$node->addChild('content_html', $html);
}
return $node;
}
add_filter('wpallimport_xml_row', 'parse_content', 10, 1);