Last active
September 8, 2017 01:14
-
-
Save schemapress/9ba7b259c069a8c10623c444d0665a44 to your computer and use it in GitHub Desktop.
Override Schema media image with an attachment id from post meta https://schema.press
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
<?php //* do not include php tag | |
add_filter( 'schema_wp_filter_media', 'schema_wp_override_media_with_image_attachment_id_post_meta_716235' ); | |
/** | |
* Override media image with an attachment id from post meta | |
* | |
* @param array $media. | |
* @since 1.5 | |
* @return array contains the image attributes | |
*/ | |
function schema_wp_override_media_with_image_attachment_id_post_meta_716235( $media ) { | |
global $post; | |
// Get attachmemnt id from post meta | |
// | |
// Note: replace ATTACHMENT_ID_POST_META_KEY with the actual post meta key you use to save attachment id value | |
// | |
$attachment_id = get_post_meta( $post->ID, 'ATTACHMENT_ID_POST_META_KEY', true); | |
// Get image attributes | |
$image_attributes = wp_get_attachment_image_src( $attachment_id, 'full' ); | |
// Get image other details | |
$image_url = $image_attributes[0]; | |
$image_width = ( $image_attributes[1] > 696 ) ? $image_attributes[1] : 696; // Images should be at least 696 pixels wide | |
$image_height = $image_attributes[2]; | |
// Build our media array | |
$media = array ( | |
'@type' => 'ImageObject', | |
'url' => $image_url, | |
'width' => $image_width, | |
'height' => $image_height, | |
); | |
return $media; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment