Calling PHP Functions In Your Import Configuration
You can execute any PHP function (native functions or user-defined functions) from inside virtually any text box (post title, post content, Custom Field values, etc.) in WP All Import. You can even pass values from your data file to the PHP function.
Example 1 – (Inline PHP) Using the Native str_replace Function to Remove Commas
Here, we’re using the str_replace function to remove commas from the post title:
[str_replace(",", "", {title[1]})]
Note the use of double quotes instead of single quotes. You must use double quotes when executing PHP functions from within WP All Import. You can’t use single quotes.
Import Any CSV, XML, or Excel to WordPress
- Any theme or plugin
- Images & galleries
- Custom fields
- Categories & tags
- Woo, ACF, Meta Box, JetEngine
Example 2 – Custom Code in the Function Editor
WP All Import has a built-in code editor where you can write your own custom functions. You can access the Function Editor when editing your import template or via All Import › Settings in your WordPress dashboard.
The Function Editor makes code editing easy:
- Syntax highlighting
- Built-in linting to prevent site-breaking errors
- Code only runs during an import
- Write and test code on the edit import page using the Preview feature
For example, let’s say we are importing WooCommerce products and need to increase the price by 50%, as well as round it so that it looks nice.
We can do this by writing a custom PHP function to process and return the desired price.
Here’s an example of a function that you could use:
function round_price( $price = null, $multiplier = 1, $nearest = .01, $minus = 0 ) {
if ( !empty( $price ) ) {
// strip any extra characters from price
$price = preg_replace("/[^0-9,.]/", "", $price);
// perform calculations
return ( round ( ( $price * $multiplier ) / $nearest ) * $nearest ) - $minus;
}
}
This can be added directly to the Function Editor on the Edit Import page:
We can then call this function in the Regular Price field provided by our WooCommerce Add-On, where we can customize it based on our needs. We want to set the multiplier to 1.5, so our prices are increased by 50%. We want to set the round parameter to 10, so prices will be rounded to the nearest $10. And finally, we want to set the minus parameter to .01, so we get prices ending in $.99.
[round_price({price[1]},"1.5","10",".01")]
This way, a product with a price of $55 will be imported with a price of $79.99.
You can set the parameters however you wish or modify the code itself to do something else. Writing code in the Function Editor is easy, fast, and infinitely customizable. Once you click Save Functions, you can click the Preview button to see the results and then modify as needed.
Related Docs
Code snippets and examples that are available for WP All Import.
Learn about our action reference and our API.
Use PHP functions (either native or custom) in WP All Export.