WordPress.org

Plugin Reviews

VK Image

Forces vk.com to use the first image from post while sharing a link.

Average Rating
3 out of 5 stars
Not gettin from post-thumbnail
By , for WP 3.9.1

This code will fix it:

<?php
/*
Plugin Name: VK Image
Plugin URI: http://ajayver.com
Description: Forces vk.com to use the first image from post while sharing a link.
Version: 1.1
Author: ajayver
Author URI: http://ajayver.com
*/

add_action('wp_head', 'add_image_for_vk');

function add_image_for_vk() {

	if (is_single()) {
		global $post;
		$current_post = get_post( $post->ID );
		$thumbnail_id = get_post_thumbnail_id($current_post->ID);
		if ($thumbnail_id) {
			$post_attachment = wp_get_attachment_image_src($thumbnail_id, 'post-thumbnail');
			$image = reset($post_attachment);
		} else {
			$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"]/i', $current_post->post_content, $matches);
			if (!empty($matches[1][0])) {
				$image = $matches[1][0];
			} else {
				$image = get_option('vk_default_image');
			}
		}
	}

	if(!empty($image)) {
		echo "<!-- Added by VK Image Plugin -->\n";
		echo '<link rel="image_src" href="' . $image . '" />' . "\n";
	}

}

include( plugin_dir_path( __FILE__ ) . 'admin.php');

You must log in to submit a review. You can also log in or register using the form near the top of this page.