Documentation / Code Snippets

Modifying dates using custom methods

The use of custom methods require ImportWP version 2.2.0+

Some WordPress fields require dates to be entered in a specific format, you can easily change the format of a date using ImportWP’s custom methods, which allows you to create a function/method in your themes function file or via a plugin, which will process the data before before it is imported.

Add the following code snippet to your website:

function iwpe01_format_date($input = '', $output_format = 'd/m/Y', $input_format = 'date')
{

    switch ($input_format) {
        case 'date':
            $time = strtotime($input);
            break;
        case 'timestamp':
        default:
            $time = $input;
            break;
    }

    if (intval($time) == 0) {
        return '';
    }

    return date($output_format, $time);
}

Then when editing your importer on the “Template Fields” step, you can use the custom method, each argument withing “” can be replaced with data from your importer e.g. csv: {0} or xml: {/date}

Example returning the default date format:

[iwpe01_format_date("2021-06-04T18:59:59.999Z")]

Returns: 04/06/2021

Example changing the returned date format:

[iwpe01_format_date("2021-06-04T18:59:59.999Z", "d-m-Y")]

Returns: 04-06-2021

Example converting a timestamp into a date format:

[iwpe01_format_date("1622833199", "d-m-Y", "timestamp")]

Returns: 04-06-2021