WP All Import‘s drag-and-drop interface covers simple field mapping. For anything more complex – transforming data, conditional field values, combining multiple source fields, or cleaning up messy import data – the PHP function editor is where you work. This is a reference of real examples.
How PHP Functions Work in WP All Import
In the field mapping screen, each field has a small function icon. Click it to switch from drag-and-drop mode to PHP mode. Your code receives the source XML/CSV values as variables and must return the value to store in that field.
Available variables depend on your source file structure. For XML, elements are available as [element_name]. For CSV, columns are available as [column_name]. WP All Import wraps your code in a function, so you return the value rather than echoing it.
String Transformation
Combining first and last name columns into a display name:
return trim(sanitize_text_field([first_name]) . ' ' . sanitize_text_field([last_name]));
Converting a price string with currency symbols to a plain number:
$raw = [price]; // e.g. "$1,299.00" or "1.299,00"
$clean = preg_replace('/[^0-9.]/', '', str_replace(',', '', $raw));
return floatval($clean);
Converting a date from DD/MM/YYYY to YYYY-MM-DD for storage:
$raw = [date_field]; // e.g. "25/03/2024"
if (empty($raw)) return '';
$parts = explode('/', $raw);
if (count($parts) !== 3) return '';
return $parts[2] . '-' . $parts[1] . '-' . $parts[0];
Need this built properly? Describe the project and get a free estimate.
Conditional Field Values
Setting post status based on a source field value:
$status = strtolower(trim([availability]));
$map = array(
'active' => 'publish',
'inactive' => 'draft',
'discontinued'=> 'trash',
);
return isset($map[$status]) ? $map[$status] : 'draft';
Assigning a category based on a numeric price range:
$price = floatval([price]);
if ($price < 50) return 'budget';
if ($price < 200) return 'mid-range';
return 'premium';
Combining Multiple Source Values
Building an SEO meta description from multiple source fields:
$brand = sanitize_text_field([brand]);
$model = sanitize_text_field([model]);
$year = intval([year]);
$summary = sanitize_text_field([short_description]);
$desc = "{$brand} {$model} ({$year}) - {$summary}";
return mb_substr($desc, 0, 155);
XPath for Nested XML
When your XML has nested elements and WP All Import's default element selection does not reach deep enough, XPath lets you target specific nodes. In the XPath field for a data element:
attributes/attribute[@name="Color"]/value
This selects the value element inside an attribute element where the name attribute equals "Color" - a common pattern in product feed XML.
Selecting the second image URL from a list of images:
images/image[2]/url
Selecting all image URLs as a pipe-delimited string requires a PHP function since XPath itself returns a node list:
$images = [];
// WP All Import provides the current XML node as $node
foreach ($node->xpath('images/image/url') as $url) {{
$images[] = (string) $url;
}}
return implode('|', $images);
Handling Missing or Malformed Data
Production import files are rarely clean. Defensive PHP that handles missing values:
$value = isset([optional_field]) ? trim([optional_field]) : '';
// Fall back to a different field if primary is empty
if (empty($value)) {{
$value = isset([backup_field]) ? trim([backup_field]) : 'Unknown';
}}
return sanitize_text_field($value);