Skip to content

Commit 564717a

Browse files
committed
Now using picturefill AND a shortcode option has been added
1 parent ee10e45 commit 564717a

File tree

5 files changed

+59
-40
lines changed

5 files changed

+59
-40
lines changed

js/matchMedia.js

Lines changed: 0 additions & 2 deletions
This file was deleted.

js/pictureTime.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

js/picturefill.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

readme.txt

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,24 @@ This plugin adds 3 custom image sizes to your wordpress thumbnail function, and
66

77
It works best with advanced custom fields. If you'd like to add more sizes, you can do so in the php file of this plugin.
88

9-
Put following code wherever you'd like the image to be
9+
Put following code wherever you'd like the image to be in your template files
1010

11-
<picture>
11+
<span data-picture>
1212
<?php
13-
$image = 'the image id or ACF field name';
13+
$image ='the id of your image';
1414
$mappings = array(
1515
0 => 'small-img', // zero maps to default
1616
500 => 'medium-img',
1717
800 => 'large-img'
1818
);
19-
echo getPictureSrcs($image, $mappings);
19+
echo getPictureSrcs($image, $mappings),
20+
'<noscript>',wp_get_attachment_image( $image, 800 ),' </noscript>';
2021
?>
21-
</picture>
22+
</span>
23+
24+
To use as a shortcode, pass in the image id and breakpoint sizes. ex:
25+
26+
[responsive imageid="12" size1="0" size2="500" size3="1000"]
27+
28+
2229

wp-tevko-responsive-images.php

Lines changed: 46 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,29 @@
11
<?php
22

3-
/*
4-
Plugin Name: WP Tevko Responsive Images
5-
Plugin URI: http://timevko.com
6-
Description: Fully responsive image solution using pictureTime and the ID of your image.
7-
Version: 0.2.0
8-
Author: Tim Evko
9-
Author URI: http://timevko.com
10-
License: Creative Commons
11-
*/
3+
/*
4+
Plugin Name: WP Tevko Responsive Images
5+
Plugin URI: http://timevko.com
6+
Description: Fully responsive image solution using picturefill and the ID of your image.
7+
Version: 0.2.0
8+
Author: Tim Evko
9+
Author URI: http://timevko.com
10+
License: Creative Commons
11+
*/
1212

1313

14-
//first we get pictureTime and matchMedia.js
14+
//first we get picturefill
1515

16-
function get_pictureTime() {
17-
wp_enqueue_script( 'pictureTime', plugins_url( '/js/pictureTime.js', __FILE__ ));
18-
}
19-
20-
function get_matchMedia() {
21-
wp_enqueue_script( 'matchMedia', plugins_url( '/js/matchMedia.js', __FILE__ ));
16+
function get_picturefill() {
17+
wp_enqueue_script( 'picturefill', plugins_url( '/js/picturefill.js', __FILE__ ));
2218
}
2319

24-
add_action('init','get_pictureTime');
25-
add_action('init','get_matchMedia');
2620

27-
//add support for our desired image sizes
28-
add_image_size( 'large-img', 1000, 702);
29-
add_image_size( 'medium-img', 700, 372);
30-
add_image_size( 'small-img', 300, 200);
21+
add_action('init','get_picturefill');
22+
23+
//add support for our desired image sizes - if you add to these, you may have to adjust your shortcode function
24+
add_image_size( 'large-img', 1000, 702);
25+
add_image_size( 'medium-img', 700, 372);
26+
add_image_size( 'small-img', 300, 200);
3127

3228
function getPictureSrcs($image, $mappings)
3329
{
@@ -36,9 +32,35 @@ function getPictureSrcs($image, $mappings)
3632
foreach ($mappings as $size => $type)
3733
{
3834
$imageSrc = wp_get_attachment_image_src($image, $type);
39-
$arr[] ='<source srcset="'. $imageSrc[0] . ' "media="(min-width:'. $size .'px)"/>';
35+
$arr[] ='<span data-src="'. $imageSrc[0] . ' "data-media="(min-width:'. $size .'px)"></span>';
4036
}
41-
return implode(array_reverse($arr));
37+
return implode($arr);
38+
}
39+
40+
// Yes you can use it as a short code
41+
function responsiveShortcode( $atts ) {
42+
43+
44+
45+
extract( shortcode_atts( array(
46+
'imageid' => 1,
47+
// you can add more sizes for your shortcodes here
48+
'size1' => 0,
49+
'size2' => 600,
50+
'size3' => 1000,
51+
), $atts ) );
52+
53+
$mappings = array(
54+
$size1 => 'small-img',
55+
$size2 => 'medium-img',
56+
$size3 => 'large-img'
57+
);
58+
59+
return '<span data-picture>'
60+
. getPictureSrcs($imageid, $mappings) .
61+
'<noscript>' . wp_get_attachment_image( $imageid, $size2 ) . ' </noscript>
62+
</span>';
4263
}
64+
add_shortcode( 'responsive', 'responsiveShortcode' );
4365

4466
?>

0 commit comments

Comments
 (0)