Last active
January 3, 2020 06:10
-
-
Save vietlongn/c8161adc2677297373d0958bc765a219 to your computer and use it in GitHub Desktop.
Custom OG image WP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
define( 'FB_COVER_SIZE_NAME', 'fb-cover' ); | |
function custom_theme_setup() { | |
add_image_size( FB_COVER_SIZE_NAME, 660, 347, true ); | |
// more img sizes | |
} | |
add_action( 'init', 'custom_theme_setup' ); | |
function lb_image_downsize( $value = false, $post_id, $size = 'medium' ) | |
{ | |
$arr_size = get_image_size($size); | |
if($arr_size && $img_url = lb_image_url($size, $post_id)){ | |
return array($img_url, $arr_size['width'], $arr_size['height'], false); | |
} | |
return false; | |
} | |
add_filter('image_downsize', 'lb_image_downsize', 10, 3); | |
function lb_image_url($size = 'medium', $post_id){ | |
$arr_size = get_image_size($size); | |
if($arr_size && $img_url = wp_get_attachment_url($post_id)){ | |
if('gif' == strtolower(substr($img_url,-3))){ | |
return $img_url; | |
} | |
$width_str = sprintf('w%s', $arr_size['width']); | |
$suffix = ''; | |
switch ($size) { | |
case FB_COVER_SIZE_NAME: | |
$width_str = 'fb'; | |
$suffix = '/cover.png'; | |
break; | |
case 'low-quality': | |
$width_str = sprintf('wl%s', $size['width']); | |
break; | |
default: | |
break; | |
} | |
$parts = parse_url($img_url); | |
return sprintf('%s://%s/%s%s%s', $parts['scheme'], $parts['host'], $width_str, $parts['path'], $suffix); | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment