Skip to content

Instantly share code, notes, and snippets.

@meanthemes
Created July 22, 2014 11:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save meanthemes/607c6e3464886d243543 to your computer and use it in GitHub Desktop.
Save meanthemes/607c6e3464886d243543 to your computer and use it in GitHub Desktop.
<?php
// Overriding post thumbnail when necessary
add_filter( 'post_thumbnail_html', 'nelioefi_replace_thumbnail', 10, 5 );
function nelioefi_replace_thumbnail( $html, $post_id, $post_image_id, $size, $attr ) {
$image_url = get_post_meta( $post_id, '_nelioefi_url', true );
if ( $image_url && strlen( $image_url ) > 0 ) {
$width = false;
$height = false;
$additional_classes = '';
global $_wp_additional_image_sizes;
if ( is_array( $size ) ) {
$width = $size[0];
$height = $size[1];
}
else if ( isset( $_wp_additional_image_sizes[ $size ] ) ) {
$width = $_wp_additional_image_sizes[ $size ]['width'];
$height = $_wp_additional_image_sizes[ $size ]['height'];
$additional_classes = 'attachment-' . $size . ' ';
}
if ( $width && $width > 0 ) $width = "${width}";
else $width = '';
if ( $height && $height > 0 ) $height = "${height}";
else $height = '';
if ( isset( $attr['class'] ) )
$additional_classes .= $attr['class'];
$html = sprintf(
'<img src="%s" width="'.$width.'" height="'.$height.'" class="wp-post-image nelioefi" />',
$image_url, $width, $height, $additional_classes );
}
return $html;
}
add_action( 'the_post', 'nelioefi_fake_featured_image_if_necessary' );
function nelioefi_fake_featured_image_if_necessary( $post ) {
if ( is_array( $post ) ) $post_ID = $post['ID'];
else $post_ID = $post->ID;
$has_nelioefi = strlen( get_post_meta( $post_ID, '_nelioefi_url', true ) ) > 0;
$wordpress_featured_image = get_post_meta( $post_ID, '_thumbnail_id', true );
if ( $has_nelioefi && !$wordpress_featured_image )
update_post_meta( $post_ID, '_thumbnail_id', -1 );
if ( !$has_nelioefi && $wordpress_featured_image == -1 )
delete_post_meta( $post_ID, '_thumbnail_id' );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment