avatar Deluxe Blog Tips About Projects

How to get current page title in WordPress

Sometimes we want to get current page title, which is displayed in the meta <title> tag. Here I mean the page in general meaning, not just WordPress "page". This meta title is used for sharing posts on social networks, or is displayed on search engine results pages (SERPs).

How to get page title in WordPress

We have several WordPress functions that can help us to get page title:

  • get_the_title(): to get current post/page/any post type title
  • single_cat_title(): to get current category title. There are sibling functions for tags and terms as well: single_tag_title() and single_term_title()
  • get_bloginfo(): to get the blog name (site title)
  • etc.

Using those functions are great, but they have some disadvantages:

  • Those functions are specific for particular pages like single post/page, category/tag, ... pages. But we can have many page types in WordPress like: 404 page, search result page, date/author archive page, or even virtual page.
  • The title displayed differently in themes: you may use a theme that have title in format Post Title | Blog Name, but another theme can use Post Title only. You can make a combination of the functions above to achieve the correct title, but that won't be used widely for other themes.
  • Or you might set the title in various ways with SEO plugins.

So to get current page title, you have to make a lot of checks to see which type of page you're viewing, and use the corresponding functions. This process is time-consuming and error prone.

Fortunately, there are ways to solve this problem.

Get current page title

Most of WordPress themes support title-tag. With this feature, WordPress automatically outputs a proper meta title inside the <title> tag in your website's <head> section (in the source code):

function _wp_render_title_tag() {
    if ( ! current_theme_supports( 'title-tag' ) ) {
        return;
    }

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

So, to get current page title, we can use the wp_get_document_title() function:

$title = wp_get_document_title();

Using this method, you don't have to handle the logic of the template rendering to know which page you're visiting and get the corresponding page title. The function wp_get_document_title() also handles all the settings in your SEO plugin, and always returns the final title.

Please note that if your theme doesn't support title-tag, but you're using a good WordPress SEO plugin, then the plugin will automatically handle this for you.

Get current page title with wp_title()

While the method above works in most cases, there's another way to get the current page title. It uses the function wp_title(), which was widely used before the title-tag theme support was introduced.

So, if your theme uses this function to output the meta title tag, e.g. you see this code in your theme's header.php file:

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

Then you can get the current page title easily. The trick is switch the 2nd parameter from true to false to get the value of current page title:

$title = wp_title( '|', false, 'right' );

It's quite simple, isn't it?

Please note that this method only works if your theme uses wp_title() to display the meta title (like above) and doesn't support title-tag. In most cases, they are old themes.

An example using current page title

Here is an example where getting current page title is useful. We're going to display a Twitter button. I'm gonna to use another cool technique to get the current page URL:

global $wp;
$link = add_query_arg( $wp->query_string, '', home_url( $wp->request ) );
$text = wp_get_document_title();
printf(
    '<a href="https://twitter.com/intent/tweet?url=%s&text=%s">Tweet</a>',
    urlencode( $link ),
    rawurlencode( $text )
);

I'm currently use the script above (with some additions for Facebook share button) for my websites. And it's working great in archive pages where built in WordPress functions aren't very helpful.

🔥 HOT: Interested in boosting your WordPress SEO? My Slim SEO plugin is a super lighweight and automated plugin that handles most the hard work for you: meta tags, sitemap, redirection, schema & link building.

👉 Have a look and you will love it: wpslimseo.com

Comments