Skip to content

Instantly share code, notes, and snippets.

@ajtatum
Created February 20, 2023 05:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ajtatum/97b65444a4be96854d207653c7c15bc1 to your computer and use it in GitHub Desktop.
Save ajtatum/97b65444a4be96854d207653c7c15bc1 to your computer and use it in GitHub Desktop.
Articles Custom OG Image
<?
add_filter("rank_math/opengraph/facebook/og_image", function($content) {
if(is_singular('articles') && is_main_query()){
$ogimg = get_post_meta(get_the_ID(), '_dcms_eufi_img', true);
if(isset($ogimg) && !empty($ogimg))
{
return $ogimg;
}
}
return $content;
});
add_filter("rank_math/opengraph/facebook/og_image_secure_url", function($content) {
if(is_singular('articles') && is_main_query()){
$ogimg = get_post_meta(get_the_ID(), '_dcms_eufi_img', true);
if(isset($ogimg) && !empty($ogimg))
{
return $ogimg;
}
}
return $content;
});
add_filter("rank_math/opengraph/facebook/og_image_width", function($content) {
if(is_singular('articles') && is_main_query()){
$ogimg = get_post_meta(get_the_ID(), '_dcms_eufi_img', true);
if(isset($ogimg) && !empty($ogimg))
{
$content = false;
}
}
return $content;
});
add_filter("rank_math/opengraph/facebook/og_image_height", function($content) {
if(is_singular('articles') && is_main_query()){
$ogimg = get_post_meta(get_the_ID(), '_dcms_eufi_img', true);
if(isset($ogimg) && !empty($ogimg))
{
$content = false;
}
}
return $content;
});
add_filter("rank_math/opengraph/facebook/og_image_type", function($content) {
if(is_singular('articles') && is_main_query()){
$ogimg = get_post_meta(get_the_ID(), '_dcms_eufi_img', true);
if(isset($ogimg) && !empty($ogimg))
{
$mimetype = get_image_mime_type($ogimg);
if($mimetype) {
$content = $mimetype;
} else {
$content = false;
}
}
}
return $content;
});
add_filter("rank_math/opengraph/twitter/image", function($content) {
if(is_singular('articles') && is_main_query()){
$ogimg = get_post_meta(get_the_ID(), '_dcms_eufi_img', true);
if(isset($ogimg) && !empty($ogimg))
{
return $ogimg;
}
}
return $content;
});
function get_image_mime_type($image_path)
{
$mimes = array(
IMAGETYPE_GIF => "image/gif",
IMAGETYPE_JPEG => "image/jpg",
IMAGETYPE_PNG => "image/png",
IMAGETYPE_WEBP => "image/webp"
);
if (($image_type = exif_imagetype($image_path))
&& (array_key_exists($image_type ,$mimes)))
{
return $mimes[$image_type];
}
else
{
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment