1
1
<?php
2
2
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
+ */
12
12
13
13
14
- //first we get pictureTime and matchMedia.js
14
+ //first we get picturefill
15
15
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__ ));
22
18
}
23
19
24
- add_action ('init ' ,'get_pictureTime ' );
25
- add_action ('init ' ,'get_matchMedia ' );
26
20
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 );
31
27
32
28
function getPictureSrcs ($ image , $ mappings )
33
29
{
@@ -36,9 +32,35 @@ function getPictureSrcs($image, $mappings)
36
32
foreach ($ mappings as $ size => $ type )
37
33
{
38
34
$ 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 > ' ;
40
36
}
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> ' ;
42
63
}
64
+ add_shortcode ( 'responsive ' , 'responsiveShortcode ' );
43
65
44
66
?>
0 commit comments