9

I am trying to change the title of my detail page to the name of the car. I have made a plugin that fills the detail page with the car information. But now I can't get the add_filter('wp_title') to work in my plugin.

Here is the code I tried:

function init() {
    hooks();
}
add_action('wp', 'init');

function hooks() {
    if(get_query_var('car_id') != "") {
        add_filter('wp_title', 'addTitle', 100);
        add_action('wp_head', 'fillHead');
    }
    add_shortcode('showCar', 'showCar');
}

function addTitle() {
    $api_url_klant = API_URL . '/gettitle/' . get_option("ac") . '/' . get_query_var('car_id');
    $title = getJSON($api_url_klant);
    return $title['merk'] . " " . $title['model'] . " - " . $title['bedrijf'];
}

The addTitle() function works just fine. It returns the right name. Also the add_action('wp_head') works as well. I only can't get the wp_title filter not to work.

Am I executing this filter at the wrong moment or what am I doing wrong?

0

5 Answers 5

33

If anyone else is having problems with this, it may be due to the Yoast plugin. Use:

add_filter( 'pre_get_document_title', function( $title ){
    // Make any changes here
    return $title;
}, 999, 1 );
1
  • 4
    To get around Yoast's title filters but keep the benefits of document_title_parts, we can also return an empty string in the wpseo_title filter: add_filter('wpseo_title', '__return_empty_string'); That seems to get Yoast to fallback to the native WordPress title.
    – joemaller
    Feb 14, 2017 at 20:39
12

I can't tell from the code you have provided, but are you using:

<title><?php wp_title(); ?></title>

in your <head>, under header.php?

UPDATE

Apparently a change was made to the way titles are handled as of 4.4. Here is a link that explains how to use the new code:

https://www.developersq.com/change-page-post-title-wordpress-4-4/

/*
 * Override default post/page title - example
 * @param array $title {
 *     The document title parts.
 *
 *     @type string $title   Title of the viewed page.
 *     @type string $page    Optional. Page number if paginated.
 *     @type string $tagline Optional. Site description when on home page.
 *     @type string $site    Optional. Site title when not on home page.
 * }
 *     @since WordPress 4.4
 *     @website: www.developersq.com
 *     @author: Aakash Dodiya
*/
add_filter('document_title_parts', 'dq_override_post_title', 10);
function dq_override_post_title($title){
   // change title for singular blog post
    if( is_singular( 'post' ) ){ 
        // change title parts here
        $title['title'] = 'EXAMPLE'; 
    $title['page'] = '2'; // optional
    $title['tagline'] = 'Home Of Genesis Themes'; // optional
        $title['site'] = 'DevelopersQ'; //optional
    }

    return $title; 
}
7
  • I think so. Thats what in the standard wordpress theme right? Its in a plugin so every kind of title should be replaced Mar 18, 2016 at 15:28
  • It appears to be in twentyfourteen, but I don't see it in twentyfifteen or twentysixteen. I never use default WP themes, so I'm not sure... but I have used similar code to what you have dozens of times. I always make sure the wp_title() is called manually... Add it in and see if things change.
    – Daniel C
    Mar 18, 2016 at 15:31
  • It should work from within the plugin because its going to be used on many sites. So I don't want to change anything to the theme Mar 18, 2016 at 15:32
  • 2
    Updated my answer to reflect new WordPress title method.
    – Daniel C
    Mar 18, 2016 at 15:47
  • 1
    For anyone else getting here after trying to change this, note that the tagline (site description) is not put in the title by default. I got it in easily by using this answer and setting $title['tagline'] = get_bloginfo('description');. May 10, 2019 at 17:21
4

I posted this answer to another question but since it is relevant and more up-to-date, I though it might be useful.

How document title is generated has changed since Wordpress v4.4.0. Now wp_get_document_title dictates how title is generated:

/**
 * Displays title tag with content.
 *
 * @ignore
 * @since 4.1.0
 * @since 4.4.0 Improved title output replaced `wp_title()`.
 * @access private
 */
function _wp_render_title_tag() {
    if ( ! current_theme_supports( 'title-tag' ) ) {
        return;
    }

    echo '<title>' . wp_get_document_title() . '</title>' . "\n";
}

Here is the code from v5.4.2. Here are the filters you can use to manipulate title tag:

function wp_get_document_title() {
    /**
    * Filters the document title before it is generated.
    *
    * Passing a non-empty value will short-circuit wp_get_document_title(),
    * returning that value instead.
    *
    * @since 4.4.0
    *
    * @param string $title The document title. Default empty string.
    */
    $title = apply_filters( 'pre_get_document_title', '' );
    if ( ! empty( $title ) ) {
        return $title;
    }
    // --- snipped ---
    /**
    * Filters the separator for the document title.
    *
    * @since 4.4.0
    *
    * @param string $sep Document title separator. Default '-'.
    */
    $sep = apply_filters( 'document_title_separator', '-' );

    /**
    * Filters the parts of the document title.
    *
    * @since 4.4.0
    *
    * @param array $title {
    *     The document title parts.
    *
    *     @type string $title   Title of the viewed page.
    *     @type string $page    Optional. Page number if paginated.
    *     @type string $tagline Optional. Site description when on home page.
    *     @type string $site    Optional. Site title when not on home page.
    * }
    */
    $title = apply_filters( 'document_title_parts', $title );
    // --- snipped ---
    return $title;
}

So here are two ways you can do it.

First one uses pre_get_document_title filter which short-circuits the title generation and hence more performant if you are not going make changes on current title:

function custom_document_title( $title ) {
    return 'Here is the new title';
}
add_filter( 'pre_get_document_title', 'custom_document_title', 10 );

Second way uses document_title_separator and document_title_parts hooks for the title and the title seperator that are executed later in the function, after title is generated using functions like single_term_title or post_type_archive_title depending on the page and about to be outputted:

// Custom function should return a string
function custom_seperator( $sep ) {
   return '>';
}
add_filter( 'document_title_separator', 'custom_seperator', 10 );

// Custom function should return an array
function custom_html_title( $title ) {
   return array(
     'title' => 'Custom Title',
     'site'  => 'Custom Site'
    );
}
add_filter( 'document_title_parts', 'custom_html_title', 10 );
0

I have trawled through each and every solution on the web trying 100s of examples.

In the end... Well Done joemaller As putting this first solved everything!

add_filter('wpseo_title', '__return_empty_string');
0

The simple answer is using this variable.

document_title_parts();

For example:

add_filter( 'document_title_parts', function( $title ){
  // Customize here
  return clean( $title );
}, 10 );

Output before:

#Page Title

After Output:

Page Title

Thanks.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.