Create a gist now

Instantly share code, notes, and snippets.

Updates to the twiter-cards WordPress plugin to include filters
diff --git a/twitter-cards.php b/twitter-cards.php
index 06f6c00..6aa751c 100644
--- a/twitter-cards.php
+++ b/twitter-cards.php
@@ -22,9 +22,8 @@ class Twitter_Cards {
* @since 1.0
*/
public static function init() {
- if ( apply_filters( 'twitter_cards_display', is_single() ) ) {
+ if ( is_single() )
add_action( 'wp_head', 'Twitter_Cards::markup' );
- }
}
/**
@@ -38,45 +37,21 @@ class Twitter_Cards {
if ( ! class_exists( 'Twitter_Card_WP' ) )
require_once( dirname(__FILE__) . '/class-twitter-card-wp.php' );
- // Get Defaults
+ $card = new Twitter_Card_WP();
+ $card->setURL( apply_filters( 'rel_canonical', get_permalink() ) );
$post_type = get_post_type();
- $url = apply_filters( 'rel_canonical', get_permalink() );
- $title = ( post_type_supports( $post_type, 'title' ) ) ? get_the_title() : '';
- // one line, no HTML
- $description = ( post_type_supports( $post_type, 'excerpt' ) ) ? self::make_description( $post ) : '';
+ if ( post_type_supports( $post_type, 'title' ) )
+ $card->setTitle( get_the_title() );
+ if ( post_type_supports( $post_type, 'excerpt' ) ) {
+ // one line, no HTML
+ $card->setDescription( self::make_description( $post ) );
+ }
// does current post type and the current theme support post thumbnails?
if ( post_type_supports( $post_type, 'thumbnail' ) && function_exists( 'has_post_thumbnail' ) && has_post_thumbnail() ) {
list( $post_thumbnail_url, $post_thumbnail_width, $post_thumbnail_height ) = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
- $thumbnail = array(
- 'url' => $post_thumbnail_url,
- 'width' => $post_thumbnail_width,
- 'height' => $post_thumbnail_height
- );
+ $card->setImage( $post_thumbnail_url, $post_thumbnail_width, $post_thumbnail_height );
}
- // Apply Filters
- $url = apply_filters( 'twitter_cards_url', $url );
- $title = apply_filters( 'twitter_cards_title', $title );
- $description = apply_filters( 'twitter_cards_description', $description );
- $thumbnail = apply_filters( 'twitter_cards_thumbnail', $thumbnail );
- $site = apply_filters( 'twitter_cards_site', '' );
- $creator = apply_filters( 'twitter_cards_creator', '' );
-
- // Generate Card
- $card = new Twitter_Card_WP();
- if ( !empty($url) )
- $card->setURL( $url );
- if ( !empty($title) )
- $card->setTitle( $title );
- if ( !empty($description) )
- $card->setDescription( $description );
- if ( !empty($thumbnail) )
- $card->setImage( $thumbnail['url'], $thumbnail['width'], $thumbnail['height'] );
- if ( !empty($site) )
- $card->setSiteAccount( $site );
- if ( !empty($creator) )
- $card->setCreatorAccount( $creator );
-
if ( apply_filters( 'twitter_cards_htmlxml', 'html' ) === 'xml' )
echo $card->asXML();
else
\ No newline at end of file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment