Miracle
Miracle

Reputation: 65

Set custom page title with hook

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?

Upvotes: 2

Views: 22381

Answers (4)

Shahroq
Shahroq

Reputation: 1039

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

Upvotes: 2

Asif
Asif

Reputation: 647

<?php add_filter( 'wp_title', 'filter_function_name', 10, 3 ) ?>

look at this http://codex.wordpress.org/Plugin_API/Filter_Reference there is all hooks check.

get the variable $var=add_filter( 'wp_title', 'filter_function_name', 10, 3 ); now call ur function, sweety_page_title($var)

Upvotes: 0

Duvan
Duvan

Reputation: 365

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

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

Upvotes: 2

Adrian Spiac
Adrian Spiac

Reputation: 54

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;
}

Upvotes: 3

Related Questions