Using WordPress ‘date_formats’ PHP filter

The date_formats WordPress PHP filter allows you to modify the default date formats.

Usage

add_filter('date_formats', 'my_custom_date_formats');

function my_custom_date_formats($default_date_formats) {
    // your custom code here
    return $default_date_formats;
}

Parameters

  • $default_date_formats (string[]): Array of default date formats.

More information

See WordPress Developer Resources: date_formats

Examples

Add a new date format

Add a new custom date format to the default date formats.

add_filter('date_formats', 'add_new_date_format');

function add_new_date_format($default_date_formats) {
    $default_date_formats[] = 'j F Y';
    return $default_date_formats;
}

Remove a specific date format

Remove a specific date format from the default date formats.

add_filter('date_formats', 'remove_date_format');

function remove_date_format($default_date_formats) {
    $key = array_search('F j, Y', $default_date_formats);
    if ($key !== false) {
        unset($default_date_formats[$key]);
    }
    return $default_date_formats;
}

Replace a date format with another one

Replace a specific date format with another one in the default date formats.

add_filter('date_formats', 'replace_date_format');

function replace_date_format($default_date_formats) {
    $key = array_search('F j, Y', $default_date_formats);
    if ($key !== false) {
        $default_date_formats[$key] = 'j F Y';
    }
    return $default_date_formats;
}

Modify all date formats

Modify all default date formats by adding a custom prefix.

add_filter('date_formats', 'modify_all_date_formats');

function modify_all_date_formats($default_date_formats) {
    foreach ($default_date_formats as $key => $format) {
        $default_date_formats[$key] = 'Custom: ' . $format;
    }
    return $default_date_formats;
}

Set a single custom date format

Set a single custom date format as the only available date format.

add_filter('date_formats', 'set_single_custom_date_format');

function set_single_custom_date_format($default_date_formats) {
    return array('j F Y');
}