How to Pass Exported WordPress Data Through PHP Functions

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 fieldClick 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.

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.

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注