• nagomitaro

    (@nagomitaro)


    Hi,
    I am using Twitter Intents as part of my theme and I can successfully share posts’ URLs on Twitter. However, I would like to be able to show the images of the shared posts. Is that even possible? I was thinking to make use of Twitter cards: https://dev.twitter.com/cards/overview Is that a viable solution?

    This is an excerpt from the code:

    <div class="myclass>
    <a href="https://twitter.com/intent/tweet?text=<?php echo urlencode( the_permalink() ) ?>" target="_blank"><i class="icon-twitter"></i></a>
    </div>

    Thank you so much for your help,

Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator bcworkz

    (@bcworkz)

    It’s possible, though I don’t see what your excerpt snippet has to do with it. It is apparently all managed through meta tags, which is not very convenient for WP content, but still possible. To output the required meta tags, hook the “wp_head” action, use get_queried_object_id() to see if the query is for the post in question. If it is, output the required meta tags.

    Thread Starter nagomitaro

    (@nagomitaro)

    Hi,
    thanks for your reply, much appreciated. I have a social anchor at the bottom of my article’s page and it currently shares the post’s url via Twitter intent. You can use YOAST to add meta tags by enabling that feature, but then it looks like you need to go into each post to add the meta tags (I have also a current restriction on using YOAST, so for now it is not a viable option). Can you provide an example? I have seen an example like this:

    add_action( 'wp_head', 'wpse_add_twitter_metadata' );
    function wpse_add_twitter_metadata() {
        global $post;
        printf(
            '<meta name="twitter:card" content="summary" />
            <meta name="twitter:title" content="%s" />
            <meta name="twitter:description" content="%s" />
            <meta name="twitter:image" content="%s" />',
            get_the_title( $post ),
            get_the_excerpt( $post ),
            get_the_post_thumbnail_url($post)
        );
    }

    But when implemented locally, it did not seem to work.

    Thank you.

    Moderator bcworkz

    (@bcworkz)

    The global $post has no reliable value, if any, in the page’s head section. You can get the WP_Post object from get_queried_object() provided the query is for a single post. You were able to share URLs on Twitter from your local installation?? Many local installs prevent external access for security reasons. Unless you are experienced in running public servers, allowing external access is generally a bad idea.

    Aside from the $post issue, you basically have the right idea. Also confirm that the queried object you get is actually an instanceof WP_Post prior to outputting anything. Unless you’re sure of public access to your local install, it’d be best to test on a publicly hosted server. AFAIK that should do it, but I’ve no experience with Twitter cards. I’m going on a quick review of their docs.

    Thread Starter nagomitaro

    (@nagomitaro)

    Hi again and thank you for putting up with this ?? So much appreciated. So, I am now testing on a staging environment, but it is password protected so strictly speaking not publicly available and I am returned with WARN: no metatags available (although the page is successfully fetched). I believe if a password protected env would be an issue in term of testing, correct? (as much as localhost)

    As per the use of get_queried_object(), is this what you are suggesting:

    add_action( 'wp_head', 'wpse_add_twitter_metadata' );
    function wpse_add_twitter_metadata() {
      	$post = $wp_query->get_queried_object();
        printf(
            '<meta name="twitter:card" content="summary" />
            <meta name="twitter:title" content="%s" />
            <meta name="twitter:description" content="%s" />
            <meta name="twitter:image" content="%s" />',
            get_the_title( $post ),
            get_the_excerpt( $post ),
            get_the_post_thumbnail_url($post)
        );
    }

    Thank you!

    Thread Starter nagomitaro

    (@nagomitaro)

    So, I have ended up with these that puts the right meta tags:

    function my_twitter_cards() {
        if (is_singular()) {
            global $post;
    	$twitter_user = str_replace('@', '', get_the_author_meta('twitter'));
    	$twitter_url = get_permalink();
    	$twitter_title = get_the_title();
    	$twitter_excerpt = get_the_excerpt();
    	$twittercard_image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'full');
    	$twittercard_thumb = $twittercard_image[0];
    	if ($twitter_user) {
    	    echo '<meta name="twitter:creator" content="@' . esc_attr($twitter_user) . '" />' . "\n";
    	}
    	echo '<meta name="twitter:card" content="summary" />' . "\n";
    	echo '<meta name="twitter:url" content="' . esc_url($twitter_url) . '" />' . "\n";
    	echo '<meta name="twitter:title" content="' . esc_attr($twitter_title) . '" />' . "\n";
    	echo '<meta name="twitter:description" content="' . esc_attr($twitter_excerpt) . '" />' . "\n";
    	echo '<meta name="twitter:image" content="' . esc_url($twittercard_thumb) . '" />' . "\n";
        }
    }
    add_action('wp_head', 'my_twitter_cards');

    Still same message, although I am validating the link by passing username and password of the staging instance. Any idea why it is not working?

    Cheers,

    • This reply was modified 8 years ago by nagomitaro.
    Moderator bcworkz

    (@bcworkz)

    If you clear all of your site’s cookies (to remove any auth cookies resulting from passwords), then fetch a page and all of the resources twitter needs (not just meta tags, the linked resources too) are available in the response, then you should be good to go as far as access is concerned.

    I’m still leery of global $post, but it seems to work in my limited testing. Thus the only reason I can see would be access issues by twitter bot.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Is it possible to use Twitter intents with the post’s image in WordPress?’ is closed to new replies.