$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>How to increase image size returned from Flickr oEmbed in Twenty Twelve theme|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

How to increase image size returned from Flickr oEmbed in Twenty Twelve theme

matteradmin10PV0评论

I've been using the excellent Flickr Gallery plugin for a while but while upgrading a site to WP3.5 and the Twenty Twelve theme, I stumbled across oEmbeds and notice that Flickr is supported natively.

I did a quick test and sure enough, simply placing the URL to a Flickr image page produces an image in the post – but it's a tiny 320 pixels wide. Given the content space appears to be about 625 pixels wide, it would be much better to have the 500 pixel image returned.

I've found mention of the max width setting, which it now appears is not present in the WP3.5 admin interface. The only reason I was considering switching to the native capability was to remove customisation (the plugin I have is dead easy to use). Do I have to create a child theme simply to set this width? Or am I missing something?

Any tips appreciated.

I've been using the excellent Flickr Gallery plugin for a while but while upgrading a site to WP3.5 and the Twenty Twelve theme, I stumbled across oEmbeds and notice that Flickr is supported natively.

I did a quick test and sure enough, simply placing the URL to a Flickr image page produces an image in the post – but it's a tiny 320 pixels wide. Given the content space appears to be about 625 pixels wide, it would be much better to have the 500 pixel image returned.

I've found mention of the max width setting, which it now appears is not present in the WP3.5 admin interface. The only reason I was considering switching to the native capability was to remove customisation (the plugin I have is dead easy to use). Do I have to create a child theme simply to set this width? Or am I missing something?

Any tips appreciated.

Share Improve this question asked Dec 30, 2012 at 22:59 zkarjzkarj 1413 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 2

It doesn't have to do with the theme.

Flickr oEmbed is returning an image file which name ends with _n.jpg, and effectively it has a width of 320px. The bigger version ends with _b.jpg

The filter hook that we need to use is embed_oembed_html.

The following manipulates the result of the returning html to increase the image size, check the comments:

add_filter( 'embed_oembed_html', 'wpse_77745_flickr', 10, 4 );

function wpse_77745_flickr( $html, $url, $attr, $post_ID )
{
    // Check if oEmbedding from Flicker
    $provider = parse_url( $url ); 
    if( 'www.flickr' != $provider['host'] )
        return $html;

    // Get the image attributes from $html
    // http://stackoverflow/q/138313/1287812
    preg_match_all( '/(alt|title|src)=("[^"]*")/i', $html, $img );

    // Change small for big
    $src = str_replace( '_n.jpg', '_b.jpg', $img[2][0] );

    // Build output
    // SRC and ALT vars already contain quotes
    $big_flick = "<a href='{$url}'><img src={$src} alt={$img[2][4]} width='{$attr["width"]}' height='{$attr["width"]}'></a>";

    return $big_flick;
}

For reference, the parameter values:

$html => '<a href="http://www.flickr/photos/maheshguild/8299345724/"><img src="https://i.sstatic/f31ow.jpg" alt="Flamingos !!!" width="320" height="213" /></a>'
$url  => 'http://www.flickr/photos/maheshguild/8299345724/'
$attr => array(
    ['width'] => 625
    ['height'] => 938
    )
$post_ID => 143

It's easier to track down all our manipulations using FirePHP ( library and add-on ).

I discovered the plugin I was using had failed under Wordpress 3.5, forcing me to assess a workaround with minimum fuss. Here's what worked.

I modified my existing child theme of Twenty Eleven (I have yet to change said site to twenty twelve) by adding the following in functions.php

if ( ! isset( $content_width ) ) $content_width = 640;

This made Flickr return 640 pixel wide images, which suits my current theme. Note that when I set the width to 639 it once again returned the 320 pixel wide image, so there is still more work to figure out the exact behaviour. For Twenty Twelve, the default width is 625, so I still need to figure out how to get 500 pixel wide images returned.

Post a comment

comment list (0)

  1. No comments so far