Calling PHP Functions In Your Import ConfigurationYou 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.) of WP All Import. You can even pass values from your data file to the PHP function.
Example 1 – 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 the 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.
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 highlightingBuilt-in linting to prevent site-breaking errorsCode only runs during an importWrite and test code on the edit import page using the 「Preview」 feature
As an example, let』s say we are importing WooCommerce products and need to increase the price by 50%, and 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 calculationsreturn ( round ( ( $price * $multiplier ) / $nearest ) * $nearest ) - $minus; }}
Which can be added directly to the Function Editor on the Edit Import page:
Then we call this function in the 「Regular Price」 field provided by our WooCommerce Add-On, where we would customize it based on our needs. We want to set the multiplier to 1.5 so our prices are increased by 50%. We want set the round parameter to 10 so prices will be rounded off 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 to be whatever you like, 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
IF Statements
FOREACH Loops