0

I need to modify the outputted <title></title> tag of a few specific templates, and it has to be done in php so I can dynamically generate content for it. I've been playing around with these two snippets of code to get it to work, but so far the only change I've gotten is a duplicated title tag:

remove_filter('wp_title','genesis_default_title', 10, 3);
add_filter('wp_title', 'process_page_titles', 10, 3);

function process_page_titles($title){
    if ( is_page_template( 'landing-page-template.php' ) ) {
        return 'Select Your Car';
    } 
    elseif( is_page_template( 'dealer-page-template.php' ) ) {
        return 'Select Dealers';
    }
    elseif( is_page_template( 'thank-you-page-template.php' ) ) {
        return 'Thank You';
    }
    elseif( is_page_template( 'ad-page-template.php' ) ) {
        return 'Deals!';
    }
}

And this one

remove_action('genesis_title','genesis_do_title');
add_action('genesis_title', 'process_page_titles');

function process_page_titles(){
    if ( is_page_template( 'landing-page-template.php' ) ) {
        echo '<title>';
        wp_title( '|','Select Car','right' );
        echo '</title>';
    } 
    elseif( is_page_template( 'dealer-page-template.php' ) ) {
        echo '<title>';
        wp_title( '|','Select Dealer','right' );
        echo '</title>';
    }
    elseif( is_page_template( 'thank-you-page-template.php' ) ) {
        echo '<title>';
        wp_title( '|','Thank You','right' );
        echo '</title>';
    }
    elseif( is_page_template( 'ad-page-template.php' ) ) {
        echo '<title>';
        wp_title( '|','Deals!','right' );
        echo '</title>';
    }
    else{
        echo '<title>';
        wp_title();
        echo '</title>';
    }
}

These snippets are going to be placed in a PHP file that will be required by each of these page templates; so no need to worry about affecting other page templates since its not in functions.php.

2

Try this to create your custom titles.

remove_filter( 'wp_title', 'genesis_default_title', 10, 3 ); //Default title
remove_action( 'genesis_site_title', 'genesis_seo_site_title' ); //Genesis Extra Stuff
remove_action( 'genesis_site_description', 'genesis_seo_site_description' ); //Genesis Extra Stuff

add_filter( 'wp_title', 'genesis_default_title_new', 10, 3 );
function genesis_default_title_new( $title) {

    $title = 'Hello World!';
    return $title;
}
2
  • Genesis questions are better posted in Stackoverflow with a Genesis tag ;-) – Pat Gilmour Feb 18 '15 at 21:47
  • 1
    And if you're running WordPress SEO plugin, try disabling it. – Pat Gilmour Feb 18 '15 at 21:54
0

If you wanted to customize the title in the most Genesis way, you can create your own filter for document_title_parts

remove_filter( 'document_title_parts', 'genesis_document_title_parts' );
add_filter( 'document_title_parts', 'custom_document_title_parts' );

function custom_document_title_parts( $parts ) {
    $genesis_document_title_parts = new Genesis_SEO_Document_Title_Parts();
    $custom_document_title_parts = $genesis_document_title_parts->get_parts( $parts );
    $custom_document_title_parts['title'] = 'Hello world';
    return $custom_document_title_parts;
}

Background

In the latest version of Genesis, do_action( 'genesis_title' ); found inside of genesis/header.php doesn't actually do anything. There is never an action added to genesis_title in the latest version.

Instead, the <title> is being rendered as is rendered by wp_head(); in header.php, as is standard.

Genesis customizes the title by adding the filter genesis_document_title_parts to 'document_title_parts located in genesis/lib/structure/header.php.

To understand how this works you need to know what document_title_parts is doing. You'll find it in wp-includes/general-template.php within the function wp_get_document_title() as the last step before the title text is sanitized and returned. Because Genesis is adding a filter to this step, it is able to override the title with any custom value they choose.

If you continue following the logic, you'll find that wp_get_document_title() is responsible for creating the title used in wp_head.

If you want to understand how, look in /wp-includes/default-filters.php. There is an action added to wp_head for _wp_render_title_tag. And aha, this function simply wraps the output of wp_get_document_title() in <title> tags.

Back to how Genesis is using this functionality to customize the title, recall that genesis_document_title_parts was added as a filter to document_title_parts.

genesis_document_title_parts is a function that returns a custom SEO title if it is available in the database. It uses the class Genesis_SEO_Document_Title_Parts found in genesis/lib/classes/class-genesis-seo-document-title-parts.php.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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