I'm relatively new to PHP & wordpress but not to programming.

I wanted to have a title relative to the $post title, so I created the following filter and added it to functions.php:

add_filter('wp_title', 'filter_pagetitle');
function filter_pagetitle($title) {
    global $post;
    return $title . $post->post_title;

However, the title was still the same as $title, didn't concatenate with the post title, so I took on another approach, using get_the_title():

add_filter('wp_title', 'filter_pagetitle');
function filter_pagetitle($title) {
    global $post;
    return $title . get_the_title();

However, this caused 500 - internal server error (even if I replaced with get_the_title($post->ID)), so I thought of catching the error and just concatenate the current fixed title with "error":

add_filter('wp_title', 'filter_pagetitle');
function filter_pagetitle($title) {
    global $post;

    try {
        $toReturn = $title . get_the_title();
    } catch (Exception $e) {
        $toReturn = $title . " error";
    return $toReturn;

Yet I still got the 500 internal server error. I ended up writing this in the header.php, which finally works:

 <title>Some fixed title | <?php echo get_the_title();  ?></title>

However, why didn't it work in functions.php? Am I declaring a new variable incorrectly? Or am I doing the concatenation incorrectly?

  • By the way, if you're wondering why I don't just look at the stacktrace, it's because I don't know how to do that yet (the server is somewhere remotely and I only have access to wp-admin and the ftp). Concatenating with "error" (or even $e->getMessage()) was my attempt to see the why, but I still had an error – Adelin Jun 7 '18 at 5:58
  • 1
    if you do not have access to error log file, you can not do much development, at least not proper one, although you should set WP_DEGUB to true and that might help with displaying the errors. Anyway sounds like you have an endless recursion. – Mark Kaplun Jun 7 '18 at 6:28

You can avoid hard coded title tag in your theme template. Just use theme support.


OR, If you want to use title tag then you can do something like this.

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

You can then apply filter as

 * Filters wp_title to print a neat <title> tag based on what is being viewed.
 * @param string $title Default title text for current view.
 * @param string $sep   Optional separator.
 * @return string The filtered title.
function wpdocs_theme_name_wp_title( $title, $sep ) {
    if ( is_feed() ) {
        return $title;

    global $page, $paged;

    // Add the blog name
    $title .= get_bloginfo( 'name', 'display' );

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

    // Add a page number if necessary:
    if ( ( $paged >= 2 || $page >= 2 ) && ! is_404() ) {
        $title .= " $sep " . sprintf( __( 'Page %s', '_s' ), max( $paged, $page ) );
    return $title;
add_filter( 'wp_title', 'wpdocs_theme_name_wp_title', 10, 2 );

See wp_title()

  • definitely +1 - but was the lack of add_theme_support('title-tag'); the reason for the 500 error? – Adelin Jun 7 '18 at 10:23
  • I don't think so. You should set WP_DEBUG to true in wp-config.php file to get the proper php error. – Bikash Waiba Jun 7 '18 at 10:46

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.