skip to Main Content

I want to change the og:image that Yoast SEO generates, to some custom height and width (eg 1200×630) because I’m uploading very large images. I’ve tried adding this code in functions.php but doesn’t work. Please help

function add_share_image_sizes() {
    add_image_size( 'facebook-share', 1200, 630, true );

}
add_action( 'after_setup_theme', 'add_share_image_sizes' );

function set_yoast_facebook_share_image_size() {
    return 'facebook-share';
}
add_filter( 'wpseo_opengraph_image_size', 'set_yoast_facebook_share_image_size' );

2

Answers


  1. You need to pass a parameter.

    function add_share_image_sizes() {
        add_image_size( 'facebook-share', 1200, 630, true );
    
    }
    add_action( 'after_setup_theme', 'add_share_image_sizes' );
    
    function set_yoast_facebook_share_image_size($img_size) {
        return 'facebook-share';
    }
    add_filter( 'wpseo_opengraph_image_size', 'set_yoast_facebook_share_image_size' );
    
    Login or Signup to reply.
  2. Yoast has a filter to not handle images for og:image for 2Mb max. so you can update that by using a filter. Please add this code in your themes functions.php file.

    // Increase image size for Yoast.
    add_filter( 'wpseo_image_image_weight_limit', $n( 'filter_yoast_image_weight' ), 20 );
    
    function filter_yoast_image_weight( $size ) {
    return 20971520; // 20MB. 
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search