Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?

WP All Import - IF/ELSE statement examples

Here are some example WP All Import [IF] statements. The criteria for an IF statment is written in XPath 1.0 syntax and can use XPath functions. Although powerful, XPath syntax can be quite complex. In many cases it might be easier to use a PHP function as shown here.

Note: The [ELSE]<something> part is optional

Number is equal to:

[IF({price[.=0]})]Zero[ELSE]Not Zero[ENDIF]

Number is greater than:

[IF({price[.>0]})]Greater than 0[ENDIF]

Text is equal to:

[IF({title[.='Foo']})]The title is Foo[ENDIF]

Text is not equal to:

[IF({title[.!='Foo']})]The title is not Foo[ENDIF]

Text is empty (see note below):

[IF({title[.='']})]The title is empty[ENDIF]

Text contains:

[IF({title[contains(.,’Foo')]})]Has foo[ENDIF]

Text length is:


IF/ELSE example:

[IF({title[.='']})]The title is empty[ELSE]{title}[ENDIF]

Note: You might have seen examples using not(text()) to check for empty fields. While this is valid XPath syntax, it seems to fail on some servers. The reasons are unclear. Just use the Text is empty syntax above instead.


m-thomson commented May 10, 2017

Compare two fields within the file like this:

[IF({sku[.=../parentsku]})]The fields are equal[ELSE]They're not equal[ENDIF]

m-thomson commented Jul 13, 2017

Tip: You can often simplify your XPath statements by removing the [1] notations. That's only needed if there are multiple nodes with the same name (to indicate the one you want). By default the first one is grabbed so [1] is unnecessary. These are functionally equivalent if there is only one title field:

[IF({title[1][text() = 'Foo']})]The title is Foo[ENDIF]
[IF({title[text() = 'Foo']})]The title is Foo[ENDIF]


m-thomson commented Aug 17, 2017

Nested IF statements are allowed:

[IF({color[.= '']})]
  [IF({size[.= '']})]
   One size fits all. Color is as pictured.
  One size fits all. Color: {color}
  Size: {size} Color: {color}

Note #1: This is written on multiple lines here for readability but can be placed on all a single line in your import.
Note #2: Just because you can nest IF statements doesn't always mean that you should :-). A custom PHP function is usually a cleaner solution... especially in single line fields.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment