Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
SNIP Extension that lets you choose a certain post thumbnail size
<?php
/*
Plugin Name: SNIP: Post Thumbnail Sizes
Version: 0.1.0
*/
if ( ! defined('ABSPATH')) {
exit;
} // Exit if accessed directly
/**
*
* PHP Version check.
*
*/
if (version_compare(PHP_VERSION, '7.0', '<')) {
add_action('admin_notices', 'snip_post_thumbnail_old_php_notice');
function snip_post_thumbnail_old_php_notice()
{
printf(
'<div class="notice error"><p>%s</p></div>',
sprintf(
__('Hey mate! Sorry for interrupting you. It seem\'s that you\'re using an old PHP version (your current version is %s). You should upgrade to at least 7.0 or higher in order to use the this plugin. Thank you!',
'snip-post-thumbnails'),
esc_html(PHP_VERSION)
)
);
}
# sorry. The plugin will not work with an old PHP version.
return;
}
add_filter('wpbuddy/rich_snippets/fields/internal_subselect/values', 'snip_post_thumbnails_subselects');
/**
* Adds new field to the Global Snippets select boxes.
*
* @param array $values
*
* @return array
* @since 0.1.0
*
*/
function snip_post_thumbnails_subselects($values)
{
$values['http://schema.org/URL'][] = array(
'id' => 'textfield_current_post_thumbnail_url',
'label' => esc_html_x('Post: Thumbnail URL (size)', 'subselect field', 'snip-post-thumbnails'),
'method' => 'snip_post_thumbnail_size_url',
);
return $values;
}
/**
* Returns the post thumbnail URL according to the size name.
*
* @param $val
* @param \wpbuddy\rich_snippets\Rich_Snippet $rich_snippet
* @param array $meta_info
*
* @return string
* @since 0.1.0
*
*/
function snip_post_thumbnail_size_url($val, \wpbuddy\rich_snippets\Rich_Snippet $rich_snippet, array $meta_info)
{
$src = wp_get_attachment_image_src(
get_post_thumbnail_id($meta_info['current_post_id']),
$val
);
if (isset($src[0])) {
return $src[0];
}
return '';
}
@code-flow

This comment has been minimized.

Copy link
Owner Author

code-flow commented May 28, 2019

This is an extension to SNIP, my Structured Data WordPress Plugin.
You can find the documentation to this on the following page: https://rich-snippets.io/use-post-thumbnail-sizes-for-images/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.