Set custom page title with hook

19,879

Solution 1

First make sure you don't have any SEO plugins installed that may also filter your title. Then try this in your functions.php file:

add_filter('wp_title','sweety_page_title',10,1);

function sweety_page_title($title){
    $title='Your new title'; //define your title here
    return $title;
}

Solution 2

For changing page titles, you should hook into document_title_parts:

function wpdocs_filter_wp_title( $title, $sep ) {

    $title['site'] = 'My Site Title';
    $title['tagline'] = 'My Site Tagline';

    return $title;
}
add_filter( 'document_title_parts', 'wpdocs_filter_wp_title', 10000, 2 );

Also, if you need more details, checking this file out might help:

\wp-includes\general-template.php: wp_get_document_title() method

Solution 3

add_filter('wpseo_title','custom_seo_title',10,1);

function custom_seo_title(){
    $title='Your new title'; //define your title here
    return $title;
}
Share:
19,879
Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin almost 2 years

    I'm trying to set a custom page title (<title></title>) with wp_title.

    The simplest function in the world:

    function sweety_page_title($string)
    {
        echo $string . ' - ';
    }
    

    After I call:

    do_action('wp_title', 'my title...');
    add_action('wp_title', 'sweety_page_title', 10, 1);
    

    But it does not work.

    Where am I wrong?

  • ion
    ion over 11 years
    I have set a page as home-page and when I use is_home() inside the hook it does not work...does anyone know why?