Skip to content
This repository has been archived by the owner on Apr 7, 2022. It is now read-only.
Ben Ward edited this page Dec 11, 2017 · 1 revision

The Twitter plugin for WordPress automatically generates Cards markup for your website. Cards delivery content summaries, site and author attribution when your site's links are shared on Twitter.

image0

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

Card customization WordPress post editor

Filters

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

twitter_card_type

Customize the 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 Card markup builder attempts to auto-generate a Card based on the template properties of the selected Card. Providing an override at this specific filter, instead of the more generic twitter_card filter, allows the markup generator to include or exclude features such as a description or number of images when building 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 the 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 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 Card, if supported by the chosen card template. Twitter may truncate this value. Return an empty string to omit the twitter:description meta element from your page.

A description is only passed through this filter when not explicitly provided by the website for display in a 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 the Card description after a specified number of words. Twitter may further truncate this text depending on display context. 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 the 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 the Card image URL. Default: large

The 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 display in various dimensions 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 Card markup before they are output on the webpage as <meta> elements. 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 Cards markup builder at 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');
Clone this wiki locally