Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am using a child theme with WordPress twentytwelve.

I'm confused as to what function I can use in the child theme's functions.php to just change the site title separator, from its default vertical slash | to a hyphen -

share|improve this question
3  
Please post relevant code (either from functions.php or else from header.php), and we can help you. (Hint: look for the call to wp_title() in header.php.) –  Chip Bennett Feb 20 '13 at 18:17
    
I know the title could be defined in the header.php file as <?php wp_title( '-', true, 'right' ); ?> but I din't want to touch the existing header.php, but do it in a child theme's functions.php instead –  John Doe Feb 20 '13 at 18:57

2 Answers 2

From this comment:

I know the title could be defined in the header.php file as but I din't want to touch the existing header.php, but do it in a child theme's functions.php instead

But Twenty Twelve isn't modifying the separator in its wp_title filter callback; rather, it is defining it in its call to wp_title(), in header.php:

<title><?php wp_title( '|', true, 'right' ); ?></title>

Note that the wp_title callback merely passes $sep along, without modifying it:

function twentytwelve_wp_title( $title, $sep ) {
    global $paged, $page;

    if ( is_feed() )
        return $title;

    // Add the site name.
    $title .= get_bloginfo( 'name' );

    // Add the site description for the home/front page.
    $site_description = get_bloginfo( 'description', 'display' );
    if ( $site_description && ( is_home() || is_front_page() ) )
        $title = "$title $sep $site_description";

    // Add a page number if necessary.
    if ( $paged >= 2 || $page >= 2 )
        $title = "$title $sep " . sprintf( __( 'Page %s', 'twentytwelve' ), max( $paged, $page ) );

    return $title;
}
add_filter( 'wp_title', 'twentytwelve_wp_title', 10, 2 );

You have two choices:

  1. Replace header.php via Child Theme, and call:

    <title><?php wp_title( '-', true, 'right' ); ?></title>
    
  2. Remove Twenty Twelve's filter callback, and replace it with your own:

    function wpse87673_filter_wp_title( $title, $sep ) {
        // Your callback code goes here
    }
    add_filter( 'wp_title', 'wpse87673_filter_wp_title', 11, 2 );
    remove_filter( 'wp_title', 'twentytwelve_wp_title', 10, 2 );
    

I don't think there is a more simple way to filter $sep directly. It is defined by the wp_title() function call parameter.

share|improve this answer
    
Your suggestion No. 1 is what I meant by defining the code in the header.php, but this wouldn't be as preferable as including code in a child theme's functions.php, but no callback code for that works. –  John Doe Feb 20 '13 at 21:46

This seems to work:

 <?php

function johndoe999_filter_wp_title( $title, $sep ) {
global $paged, $page;

$sep = ' - ';

$title = get_the_title();

if ( is_feed() ) {
return $title;
}

$title .= $sep;

$blogname = get_bloginfo('name');

// Add the site name.
$title .= $blogname;

// Add the site description for the home/front page.
$site_description = get_bloginfo( 'description', 'display' );
if ( $site_description && ( is_home() || is_front_page() ) ) {
$title = "$blogname - $site_description";
}

// Add a page number if necessary.
if ( $paged >= 2 || $page >= 2 ) {
$title = "$title $sep " . sprintf( __( 'Page %s', 'twentytwelve' ), max( $paged, $page ) );
}

return $title;
}


add_filter( 'wp_title', 'johndoe999_filter_wp_title', 10, 2 );

function johndoe999_remove_title () {
remove_filter( 'wp_title', 'twentytwelve_wp_title', 10, 2 );
}

add_action( 'after_setup_theme', 'johndoe999_remove_title' );


?>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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