Skip to content

Instantly share code, notes, and snippets.

@m-thomson
Last active December 2, 2019 10:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save m-thomson/921e3f5d9e7a4687d91a906b9d3af2d9 to your computer and use it in GitHub Desktop.
Save m-thomson/921e3f5d9e7a4687d91a906b9d3af2d9 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.

@jonasskafte
Copy link

jonasskafte commented Nov 28, 2019

Thanks for this @m-thomson! -really useful and much-appreciated snippet!

I was wondering how you would use this with ACF Repeater fields? I've tried all combinations I can think of, but I haven't been successful in mapping repeater fields yet.

Any ideas? Do you have ACF Pro and can you see what I mean? If not then I'll try and flesh out the challenge that I am dealing with some more...

I hope you can help and many thanks!

@jonasskafte
Copy link

Hi again @m-thomson, I just wanted to let you know that I've reached out to the ACF community as well in the hope of help. In case it's useful, here's the post I've made about this challenge in the ACF forum:

https://support.advancedcustomfields.com/forums/topic/mapping-input-string-for-repeater-sub-field/

@m-thomson
Copy link
Author

HI @jonasskafte. I wrote this and the other gists a few years ago when I was working as a support tech for WP All Import. Unfortunately, my memory is hazy on some of the details. It's actually been at least a couple years since I've even run an import.

I will say that (to the best of my recollection) what you're trying should be possible.

Have you tried reaching out to the WP All Import support team yet? They're a fantastic bunch. Either Trey or Michael should be able to help you. http://www.wpallimport.com/support/

Best of luck.

@jonasskafte
Copy link

Hi @m-thomson,

Many thanks for getting back to me! I really appreciate your thoughts on this and I was just about to get in touch with Trey or Michael, but then I came up with another idea of how to get the result that I am looking for – also because I realized that there are a few downsides to using ACF fields rather than a custom taxonomy, which is what I've ended up going forward with instead.

Anyway, many thanks again!!

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