Documentation / Code Snippets

Change imported field values programmatically

How to use the iwp/template/process_field filter to alter the value of an importers template field, this example below restricts it to a specific importer using the importer id, and formats the post_date field into the mysql date format.

<?php

/**
 *  * Example code to alter the value of an Importer Templates field
 *  *
 *  * @param string $value Field value to insert
 *  * @param stirng $key Field name
 *  * @param ImportWP\Common\Model\ImporterModel $importer
 *  * @return string
 *  */
function iwpe_alter_field_value_example($value, $key, $importer)
{
    // restrict filter to specific importer (in this case id 47, change to what you need), comment out if not needed
    if ($importer->getId() !== 47) {
        return $value;
    }
    
    switch ($key) {
        case 'post_date':
            $value = date('Y-m-d H:i:s', strtotime($value));
        break;
    }
    
    return $value;
}

add_filter('iwp/template/process_field', 'iwpe_alter_field_value_example', 10, 3);