Last active
February 21, 2019 03:36
-
-
Save zArubaru/4607ca5bd619ea7d578f509e93d9dea8 to your computer and use it in GitHub Desktop.
Limit The SEO Framework og:image and twitter:image sizes.
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 | |
// Edit these to suit your needs | |
add_image_size( 'og-image', 1200, 630, true ); | |
add_image_size( 'twitter-image', 1024, 512, true ); | |
/** | |
* Limit The SEO Framework og:image and twitter:image sizes. | |
* | |
* For: https://fi.wordpress.org/plugins/autodescription/ | |
*/ | |
function change_seo_image_size( $image, $id ) { | |
$image_id = get_post_thumbnail_id( $id ); | |
$new_image = ''; | |
if ( $image_id ) { | |
$some = strpos( current_filter(), 'twitter' ) !== false ? 'twitter' : 'og'; | |
$image_size = $some . '-image'; | |
$new_image = wp_get_attachment_image_src( $image_id, $image_size )[0] ?? ''; | |
if ( $new_image ) { | |
$default_w = 'twitter' === $some ? '1024' : '1200'; // edit as needed | |
$default_h = 'twitter' === $some ? '512' : '630'; // edit as needed | |
$meta = wp_get_attachment_metadata( $image_id ); | |
// Set new image dimensions for The SEO Framework | |
the_seo_framework()->image_dimensions[ $id ] = array( | |
'width' => $meta['sizes'][ $image_size ]['width'] ?? $default_w, | |
'height' => $meta['sizes'][ $image_size ]['height'] ?? $default_h, | |
); | |
} | |
} | |
$new_image = $new_image ?: $image; | |
// Reformat to full url if begins with /wp-content, might need a tweak depending on the environment. | |
if ( substr( $new_image, 0, strlen( WP_CONTENT_URL ) ) === WP_CONTENT_URL ) { | |
$new_image = home_url( $new_image ); | |
} | |
return $new_image; | |
} | |
add_filter( 'the_seo_framework_twitterimage_output', __NAMESPACE__ . '\\change_seo_image_size', 10, 2 ); | |
add_filter( 'the_seo_framework_ogimage_output', __NAMESPACE__ . '\\change_seo_image_size', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment