Twitter Cards in WordPress

The Twitter plugin for WordPress automatically generates Twitter Cards markup for your website powering link templates, site attribution, and possible author attribution when your site’s links are shared on Twitter.

image0

The plugin generates markup for a Twitter Card summary template for most of your site’s pages by default. Post authors may input a custom card title or description from the post editor.

Twitter Card customization WordPress post editor

Filters

The following filters are called during the wp_head action as part of Twitter Cards markup generation. Add your filter before the wp_head action to be included in Twitter Cards customizations.

twitter_card_type

Customize the Twitter Card template to be applied for the current webpage. The provided value should match what your site would provide in a twitter:card meta element value. Default: summary

The filter passes two additional parameters to assist in customizations based on the current context. The query type parameter is similar to a WP_Query conditional (archive, author, home, post). The object ID parameter passes an object identifier for the query type, if applicable, such as the post or author identifier.

The plugin’s Twitter Cards markup builder attempts to auto-generate Twitter Cards markup based on the template properties of the selected Twitter Card. Providing an override at this specific filter, instead of the more generic twitter_card filter, allows the plugin’s markup generator to include or exclude features such as a description or number of images when building Twitter Card markup for the page.

Customization example:

function twitter_card_type( $card_type, $query_type, $object_id )
{
  return 'summary_large_image';
}
add_filter('twitter_card_type', 'twitter_card_type', 10, 3);

twitter_card_title

Set the title template component of a Twitter Card. Twitter may truncate this provided value after 70 characters. Return an empty string to omit the twitter:title meta element from your page for possible selection of a more generic title such as an Open Graph protocol title property.

A title is only passed through this filter when not explicitly provided by the website for display in a Twitter Card template. This distinction is meant to simplify possible overrides by other plugins. The plugin will not generate a title for a post permalink if the post type does not support the title feature. Act on the twitter_card filter to always override this value late in the card builder process.

Defaults by query_type parameter (simplified):

Query Type Default
home “Site Title” set in Settings > General
author Author display name, set in a user profile’s “Display name publicly as” field
archive The result of get_the_archive_title() if supported (WP 4.1+)
post WP\_Post post\_title

Customization example:

function twitter_card_title( $title, $query_type, $object_id )
{
  return 'Custom title';
}
add_filter('twitter_card_title', 'twitter_card_title', 10, 3);

twitter_card_description

Set the description template component of a Twitter Card, if supported by the chosen card template. Twitter may truncate this value after 200 characters. Return an empty string to omit the twitter:description meta element from your page for possible selection of a more generic title such as an Open Graph protocol description property.

A description is only passed through this filter when not explicitly provided by the website for display in a Twitter Card template. This distinction is meant to simplify possible overrides by other plugins. The plugin will not generate a description for a post permalink if the post type does not support the description feature. Act on the twitter_card filter to always override this value late in the card builder process.

Defaults by query_type parameter (simplified):

query type default
home “Tagline” set in Settings > General
author Author description, set in a user profile’s “Biographical Info” field.
archive The result of get_the_archive_description() if supported (WP 4.1+)
post WP_Post post_excerpt after passing through the get_the_excerpt filter or a generated excerpt from post_content

Customization example:

function twitter_card_description( $description, $query_type, $object_id )
{
  return 'Custom description';
}
add_filter('twitter_card_description', 'twitter_card_description', 10, 3);

twitter_excerpt_length

Trim a Twitter Card description after a specified number of words. Twitter Cards will currently truncate text at 200 characters if a site does not truncate its own content. Default: 55

Similar to excerpt_length for a Twitter-specific excerpt.

Customization example:

function twitter_excerpt_length( $num_words )
{
  return 42;
}
add_filter('twitter_excerpt_length', 'twitter_excerpt_length');

twitter_excerpt_more

String to append to a Twitter Card description to indicate truncated content. Default: …

Similar to excerpt_more for a Twitter-specific excerpt.

Customization example:

add_filter('twitter_excerpt_more', '__return_empty_string');

twitter_card_intermediate_image_size

An intermediate image size name to be used to generate a Twitter Cards image URL. Default: large

The Twitter Cards indexer can consume images up to one megabyte in size. The Twitter Card markup generator will use the specified intermediate size if a full-size image exceeds the maximum file size supported by Twitter. Twitter will generate thumbnails appropriate for Twitter Card display in various dimension and DPI contexts based on the provided image.

Customization example:

function twitter_image_size( $size, $attachment_id )
{
  return 'extralarge';
}
add_filter('twitter_card_intermediate_image_size', 'twitter_image_size', 10, 2);

twitter_card

Customize Twitter Card markup values before they are output on the webpage as <meta> elements. Twitter Card values are passed as an associative array, with structured property values defined as array values.

Use a more specific filter if available to tie-in to the Twitter Cards markup builder in the most appropriate build stage.

Customization example:

function twitter_card( $card_properties )
{
  if ( empty( $card_properties['image'] ) ) {
    $card_properties['image'] = 'https://example.com/logo.jpg';
  }
  return $card_properties;
}
add_filter('twitter_card', 'twitter_card');