Skip to content

Instantly share code, notes, and snippets.

@jonasskafte
Forked from m-thomson/import-string-mapping1.md
Created November 27, 2019 08:36
Show Gist options
  • Save jonasskafte/a3f26c5025fd0f8116b45f7bf99b17f2 to your computer and use it in GitHub Desktop.
Save jonasskafte/a3f26c5025fd0f8116b45f7bf99b17f2 to your computer and use it in GitHub Desktop.

Custom text mapping in WP All Import

Below is a PHP function you can use in WP All Import to enable custom text mapping anywhere in your import. You would use it in your import field like this:

[my_map({field[1]})]

Where "{field[1]}" would be the correct field name from your file.

The PHP code for the function editor:

function my_map($value) {
    $replacements = array(
        // YOU CAN ADD OR MODIFY REPLACEMENTS BELOW USING THE SAME SYNTAX
        'small'  => 'S',
        'medium' => 'M',
        'large'  => 'L',
    );
    
    return isset($replacements[$value]) ? $replacements[$value] : $value;
}

Note that the text replacements works on the entire string of text and not individual words. In the above example, "small" would become "S" but "It's a small world" would be unchanged.

Multiple values?

What if you are trying to map multiple values in a single field that have a separator character?

You can use this function:

function my_map_multiple($values, $separator){
    $map = array(
        // YOU CAN ADD OR MODIFY REPLACEMENTS BELOW USING THE SAME SYNTAX
        'BA' => 'Balcony',
        'BP' => 'Basement Parking',
        'BB' => 'BBQ Area',
        'AN' => 'Cable-Ready',
        'BW' => 'Built In Wardrobes',
       // ETC
    );

    $values = explode(',',$values);
    $replacements = array();
    foreach($values as $value){
        if(isset($map[$value])){
           $replacements[] = $map[$value];
        }
    }
    return implode($separator, $replacements);
}

And invoke it in your import with the data field as the first parameter and the separator character as the second:

[my_map_multiple({field[1]},"|")]

Where "{field[1]}" would be the correct field name from your file and | is the separator character

Example: If {field[1]} contained "BA|BP|AN" then it would map to "Balcony|Basement Parking|Cable-Ready" with this function.

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