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:
- Add or drag in the export field
- Click the field to edit it.
- Save your function in the Function Editor.
- Enable Export the value returned by a PHP function and type in the function name.
- Save the field.
Here are the different ways to use PHP in WP All Export:
- Use PHP to Process WordPress Data While it's Exported
- Using Native PHP Functions on Export Fields
- Using Custom PHP Functions on Export Fields
- Using PHP functions in a Custom XML Feed
- Using PHP Functions In a Google Merchant Center Export
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:
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:
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/code-snippets/ for a full list of code snippets to get you started.
Export WordPress Data to any CSV, XML, or Excel
- Any theme or plugin
- Images & galleries
- Custom fields
- Real-time exports
- Woo, ACF, Meta Box, JetEngine
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:
Here's another example using a custom PHP function that returns all the XML. This function only exports the images that are in the Product gallery (i.e., while exporting WooCommerce products) since out of the box, all attached images would be exported:
function example_get_image_urls_for_xml ($product_id) {
$images = array();
$image_xml = '';
if ( $product = wc_get_product($product_id) ) {
$images[] = wp_get_attachment_url( $product->get_image_id() );
$gallery = $product->get_gallery_image_ids();
if ( ! empty( $gallery ) ) {
$gallery = maybe_unserialize( $gallery );
foreach ( $gallery as $gallery_id ) {
$images[] = wp_get_attachment_url( trim( $gallery_id ) );
}
}
foreach ($images as $image) {
$image_xml .= '**LT**ImageURL**GT**' . $image . '**LT**/ImageURL**GT**';
}
return $image_xml;
}
}
Used like so (see line 10):
Check our documentation to learn more about Custom XML Feeds for WordPress.
Using PHP Functions In a Google Merchant Center Export
When you're exporting data using a Google Merchant Center Export Feed (see How to Export WooCommerce to the Google Merchant Center), you can also pass the exported data through PHP functions.
To do so, choose Custom data in your desired GMC field and input the function call in the text field. For example, here's a function to append the text " - Exported by WP All Export" to the product's title:
function my_append_title($title){
return $title . " - Exported by WP All Export";
}
This is used like so:
[my_append_title({Title})]
You can apply the same logic to any other field found in the Google Merchant Center Product Feed.
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.