How to Pass Exported WordPress Data Through PHP Functions

You can use PHP functions to process your WordPress exports in WP All Export with the following steps:

  1. Add or drag in the export field
  2. Click the field to edit it.
  3. Save your function in the Function Editor.
  4. Enable "Export the value returned by a PHP function" and type in the function name.
  5. Save the field.

Use PHP to process WordPress data while it's exported

WP All Export has the ability to process the data in any export field with PHP functions. You can write a custom function to process your data, or use a native PHP function to do it.

First, click the Add Field button under the export template and then select the field from the Select a field to export drop-down list. You can also click an existing field to bring up the edit export field modal.

From the edit export field modal, enable Export the value returned by a PHP function:

Export the value returned by a PHP function option.

In the example above, the Title is being exported. In this case, the title will be passed to your function as the first and only argument:

function my_process_title( $title ) {
     // do something with $title
     // return it
}

Using native PHP functions on export fields

In the your_function_name text box, you can use any native PHP function that uses one argument. For example, if you wanted to make the Title export field export all of the titles in uppercase, you could use strtoupper():

In fact, you can use any function that exists and is accessible during the export. That includes WordPress functions like wp_get_attachment_url, sanitize_title, etc.

Using custom PHP functions on export fields

For more complicated cases, you can write full-featured PHP functions inside the Function Editor.

As an example, let's say that you're exporting data from a real estate theme like WP Residence and you want to export the property gallery image URLs for each exported property. They're stored in the image_to_attach custom field as IDs. If we were just to export that custom field, we'd get this:

35,39,37,41,43,

We could use this function to extract the IDs, get the attachment URLs, then return a delimited list of the URLs:

function my_convert_ids_to_urls( $ids ) {
	// Return nothing if there are no IDs.
	if ( empty( $ids ) ) {
		return null;
	}
	
	// Turn the IDs into an array of IDs.
	$ids = explode( ',', $ids );
	
	// Convert the IDs to attachment URLs.
	$urls = array_map( function( $id ) {
		return wp_get_attachment_url( $id );
	}, $ids );
	
	// Get rid of empty array elements.
	$urls = array_filter( $urls );
	
	// Return comma-delimited list of image URLs.
	return implode( ',', $urls );
}

Let's use this function with the image_to_attach export field from WP Residence:

Using image on the image_to_attach field.

And that's it. Our export file will contain a column labeled Image Gallery URLs, and each row in that column will contain a comma delimited list of image URLs for the images in that property gallery.

Head over to https://www.wpallimport.com/documentation/developers/code-snippets/ for a full list of code snippets to get you started.

Using PHP functions in a Custom XML Feed

When you change the export type to a Custom XML Feed, you can also use PHP functions. You can use both native or custom PHP functions on the exported elements. The syntax is the following:

[str_replace(",","",{Title})]

[function_name({Export Element})]

You can also pass multiple elements to the function whenever necessary.

If you're outputting your own XML via PHP, you must disable CDATA tags to avoid issues and export everything correctly.

Then, you can add the CDATA tags manually, you would just use the corresponding placeholder. Here's a list of the supported placeholders that can be used:

<![CDATA[ replaced with CDATABEGIN
]]> replaced with CDATACLOSE
[ or ] replaced with **OPENSHORTCODE** or **CLOSESHORTCODE**
{ or } replaced with OPENCURVE or CLOSECURVE
( or ) replaced with OPENCIRCLE or CLOSECIRCLE
" replaced with **DOUBLEQUOT**
' replaced with **SINGLEQUOT**
< or > replaced with **LT** or **GT**

By using the placeholders, those characters are treated as a part of the XML, instead of being sanitized. The placeholders can be used either in the XML Editor or in the PHP functions that you create.

Here's an example custom XML feed that adds the date to the feed, and then converts an image ID into its URL using native WordPress and PHP functions:

Check our documentation to learn more about Custom XML Feeds for WordPress.


Related Docs

Learn how to create fields that can export data elements, function calls, and static text simultaneously.

Use the WordPress Query class to fetch posts directly from the database.

Learn about WP All Export's API and hooks.

Lifetime support. Lifetime updates. Pay once.

Packages
Standalone
Import
Pro Package
$249
one-time
  • Import Pro
Import Add-Ons
  • WooCo
  • ACF
  • Gravity Forms
  • Users
  • Types
  • Export Pro
Export Add-Ons
  • WooCo
  • ACF
  • Gravity Forms
  • Users
Add to Cart
90 Day Money Back Guarantee
Import + Export Pro Package
$299
one-time
  • Import Pro
Import Add-Ons
  • WooCo
  • ACF
  • Gravity Forms
  • Users
  • Types
  • Export Pro
Export Add-Ons
  • WooCo
  • ACF
  • Gravity Forms
  • Users
Add to Cart
90 Day Money Back Guarantee
WooCommerce Import Package
$149
one-time
  • Import Pro
Import Add-Ons
  • WooCo
  • ACF
  • Gravity Forms
  • Users
  • Types
  • Export Pro
Export Add-Ons
  • WooCo
  • ACF
  • Gravity Forms
  • Users
Add to Cart
90 Day Money Back Guarantee
Import Standalone
$99
one-time
  • Import Pro
Import Add-Ons
  • WooCo
  • ACF
  • Gravity Forms
  • Users
  • Types
  • Export Pro
Export Add-Ons
  • WooCo
  • ACF
  • Gravity Forms
  • Users
Add to Cart
90 Day Money Back Guarantee
Import + Export Standalone
$169
one-time
  • Import Pro
Import Add-Ons
  • WooCo
  • ACF
  • Gravity Forms
  • Users
  • Types
  • Export Pro
Export Add-Ons
  • WooCo
  • ACF
  • Gravity Forms
  • Users
Add to Cart
90 Day Money Back Guarantee
Export Standalone
$99
one-time
  • Import Pro
Import Add-Ons
  • WooCo
  • ACF
  • Gravity Forms
  • Users
  • Types
  • Export Pro
Export Add-Ons
  • WooCo
  • ACF
  • Gravity Forms
  • Users
Add to Cart
90 Day Money Back Guarantee
cross