Using WordPress ‘wp_title’ PHP filter

The wp_title WordPress PHP filter allows you to modify the page title displayed in the browser tab.

Usage

add_filter('wp_title', 'your_custom_function', 10, 3);
function your_custom_function($title, $sep, $seplocation) {
    // your custom code here
    return $title;
}

Parameters

  • $title (string) – The current page title.
  • $sep (string) – The title separator.
  • $seplocation (string) – Location of the separator (‘left’ or ‘right’).

More information

See WordPress Developer Resources: wp_title

Examples

Add a custom prefix to the title

Adds “My Blog – ” before the current title.

add_filter('wp_title', 'add_custom_prefix', 10, 3);
function add_custom_prefix($title, $sep, $seplocation) {
    $title = "My Blog - " . $title;
    return $title;
}

Change the title separator

Changes the separator from the default to a pipe (|) character.

add_filter('wp_title', 'change_title_separator', 10, 3);
function change_title_separator($title, $sep, $seplocation) {
    $sep = '|';
    return $title;
}

Reverse title and separator order

Reverses the order of the title and the separator.

add_filter('wp_title', 'reverse_title_order', 10, 3);
function reverse_title_order($title, $sep, $seplocation) {
    if ($seplocation == 'left') {
        $seplocation = 'right';
    } else {
        $seplocation = 'left';
    }
    return $title;
}

Append a custom string to the title

Adds “- My Custom String” after the current title.

add_filter('wp_title', 'append_custom_string', 10, 3);
function append_custom_string($title, $sep, $seplocation) {
    $title .= " - My Custom String";
    return $title;
}

Display custom title for a specific page

Displays a custom title for a specific page with ID 42.

add_filter('wp_title', 'set_custom_title_for_page', 10, 3);
function set_custom_title_for_page($title, $sep, $seplocation) {
    if (is_page(42)) {
        $title = "My Custom Title";
    }
    return $title;
}