Making WordPress.org


Ignore:
Timestamp:
07/05/2015 07:26:25 PM (9 years ago)
Author:
Otto42
Message:

Add srcset to screenshot images, move img tag building into functions.php

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-showcase/functions.php

    r1474 r1705  
    3131    }
    3232
    33     if ( !empty( $_COOKIE['devicePixelRatio'] ) && $_COOKIE['devicePixelRatio'] >= 1.5 ) {
    34         $width = $width * 2;
    35     }
    36 
    3733    if ( '' != $width ) {
    3834        $screenshot .= '?w=' . $width;
     
    4642        return $screenshot;
    4743    }
     44}
     45
     46// build the whole img tag properly for the screenshot, with srcset support
     47function site_screenshot_tag( $width = '', $classes='screenshot' ) {
     48    global $post;
     49
     50    $screenshot = get_post_meta($post->ID, 'screenshot', true);
     51    if ( empty( $screenshot ) ) {
     52        $screenshot = 'https://wordpress.com/mshots/v1/http%3A%2F%2F' . get_site_domain( true, false );
     53        $srcset = $screenshot;
     54    }
     55
     56    if ( '' != $width ) {
     57        $screenshot .= '?w=' . $width;
     58        $srcset .= '?w=' . $width*2;
     59    }
     60
     61    // mshot images have a 4/3 ratio
     62    $height = (int)( $width * (3/4) );
     63
     64    $img = "<img src='{$screenshot}' srcset='$srcset 2x' width='{$width}' height='{$height}' alt='". the_title_attribute(array('echo'=>false)) . "' class='{$classes}' />";
     65
     66    echo $img;
    4867}
    4968
Note: See TracChangeset for help on using the changeset viewer.