Add title tag automatically in WordPress

Basic implementation

On functions.php:

add_theme_support( 'title-tag' );

<head> must not have any <title> tag declared.

Backwards compatibility

In case WordPress version is less than 4.1:

if ( ! function_exists( '_wp_render_title_tag' ) ) {
	function theme_slug_render_title() {
<title><?php wp_title( '|', true, 'right' ); ?></title>
	add_action( 'wp_head', 'theme_slug_render_title' );

Custom queries

Custom queries depends on type of query, so title may be rendered in a wrong way.

add_filter( 'document_title_parts', 'change_title_part', 10 );

function change_title_part( $title ) {
	if ( get_query_var( 'favorites' ) ) {
		$title['title'] = 'Favorites';
	return $title;


WordPress shortcodes

First, create a function with $atts as an argument. It holds all the attributes you create in the shortcode.

Simple shortcode

function foobar_func( $atts ){
  return "foo and bar";
add_shortcode( 'foobar', 'foobar_func' );

The shortcode to be used:


Shortcode with attributes

function bartag_func( $atts ) {
  $a = shortcode_atts( array(
    'foo' => 'something', // 'something' is default value
    'bar' => 'something else', // 'something else' is default value
  ), $atts );
  return "foo = {$a['foo']}";
add_shortcode( 'bartag', 'bartag_func' );

The shortcode to be used:

[bartag foo="value" bar="other value"]

Enclosing shortcode

Enclosing shortcodes need not only the $atts argument but a $content argument. This argument is what is enclosed by the shortcode. Use $content = null so it will work even if there’s no content.

function caption_shortcode( $atts, $content = null ) {
  return '<span class="caption">' . $content . '</span>';
add_shortcode( 'captions', 'caption_shortcode' );

Example shortcut:

[captions]My Caption[/captions]


<span class="caption">My Caption</span>