Skip to content

Commit 94bf4ff

Browse files
committed
Prefix functions, space usage, add_sizes via hook
1 parent e5a0a65 commit 94bf4ff

File tree

1 file changed

+62
-61
lines changed

1 file changed

+62
-61
lines changed

wp-tevko-responsive-images.php

Lines changed: 62 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,75 @@
11
<?php
22

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.2
8-
Author: Tim Evko
9-
Author URI: http://timevko.com
10-
License: MIT
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.3
8+
Author: Tim Evko
9+
Author URI: http://timevko.com
10+
License: MIT
11+
*/
1212

1313

14-
// First we queue the polyfill
15-
function get_picturefill() {
16-
wp_enqueue_script('picturefill', plugins_url('/js/picturefill.js', __FILE__ ));
17-
}
18-
add_action('wp_enqueue_scripts', 'get_picturefill');
14+
// First we queue the polyfill
15+
function tevkori_get_picturefill() {
16+
wp_enqueue_script( 'picturefill', plugins_url( '/js/picturefill.js', __FILE__ ) );
17+
}
18+
add_action( 'wp_enqueue_scripts', 'tevkori_get_picturefill' );
1919

2020

21-
// Add support for our desired image sizes - if you add to these, you may have to adjust your shortcode function
22-
// TODO: Add UI for adjusting?
23-
add_image_size('large-img', 1000, 702);
24-
add_image_size('medium-img', 700, 372);
25-
add_image_size('small-img', 300, 200);
21+
// Add support for our desired image sizes - if you add to these, you may have to adjust your shortcode function
22+
// TODO: Add UI for adjusting?
23+
function tevkori_add_image_sizes() {
24+
add_image_size( 'large-img', 1000, 702 );
25+
add_image_size( 'medium-img', 700, 372 );
26+
add_image_size( 'small-img', 300, 200 );
27+
}
28+
add_action( 'plugins_loaded', 'tevkori_add_image_sizes' );
2629

27-
//alt tags will now be automatically included
28-
function get_img_alt($image) {
29-
$imgalt = trim(strip_tags( get_post_meta($image, '_wp_attachment_image_alt', true) ));
30-
return $imgalt;
31-
}
30+
// alt tags will now be automatically included
31+
function tevkori_get_img_alt( $image ) {
32+
$img_alt = trim( strip_tags( get_post_meta( $image, '_wp_attachment_image_alt', true ) ) );
33+
return $img_alt;
34+
}
3235

33-
function getPictureSrcs($image, $mappings) {
34-
$arr = array();
35-
foreach ($mappings as $size => $type) {
36-
$imageSrc = wp_get_attachment_image_src($image, $type, $alt);
37-
$arr[] ='<span data-src="'. $imageSrc[0] . ' "data-media="(min-width:'. $size .'px)"></span>';
38-
}
39-
return implode($arr);
36+
function tevkori_get_picture_srcs( $image, $mappings ) {
37+
$arr = array();
38+
foreach ( $mappings as $size => $type ) {
39+
$image_src = wp_get_attachment_image_src( $image, $type, $alt );
40+
$arr[] = '<span data-src="'. $image_src[0] . ' "data-media="(min-width:'. $size .'px)"></span>';
4041
}
42+
return implode( $arr );
43+
}
4144

42-
function responsiveShortcode($atts) {
43-
extract( shortcode_atts( array(
44-
'imageid' => 1,
45-
// You can add more sizes for your shortcodes here
46-
'size1' => 0,
47-
'size2' => 600,
48-
'size3' => 1000,
49-
), $atts ) );
45+
function tevkori_responsive_shortcode( $atts ) {
46+
extract( shortcode_atts( array(
47+
'imageid' => 1,
48+
// You can add more sizes for your shortcodes here
49+
'size1' => 0,
50+
'size2' => 600,
51+
'size3' => 1000,
52+
), $atts ) );
5053

51-
$mappings = array(
52-
$size1 => 'small-img',
53-
$size2 => 'medium-img',
54-
$size3 => 'large-img'
55-
);
54+
$mappings = array(
55+
$size1 => 'small-img',
56+
$size2 => 'medium-img',
57+
$size3 => 'large-img'
58+
);
5659

57-
return
58-
'<span data-picture data-alt="'. get_img_alt($imageid) .'">'
59-
. getPictureSrcs($imageid, $mappings) .
60-
'<noscript>' . wp_get_attachment_image($imageid, $size2) . ' </noscript>
61-
</span>';
62-
}
63-
// TODO: It this the best name? responsive_img? picture?
64-
add_shortcode('responsive', 'responsiveShortcode');
65-
66-
// Alter Media Uploader output to output shortcode instead
67-
// TODO: Make optional?
68-
// TODO: Make this know what sizes are chosen, rather than hardcoded
69-
function responsive_insert_image($html, $id, $caption, $title, $align, $url) {
70-
return "[responsive imageid='$id' size1='0' size2='600' size3='1000']";
71-
}
72-
add_filter('image_send_to_editor', 'responsive_insert_image', 10, 9);
60+
return
61+
'<span data-picture data-alt="'. get_img_alt( $imageid ) .'">'
62+
. tevkori_get_picture_srcs( $imageid, $mappings ) .
63+
'<noscript>' . wp_get_attachment_image( $imageid, $size2 ) . ' </noscript>
64+
</span>';
65+
}
66+
// TODO: It this the best name? responsive_img? picture?
67+
add_shortcode( 'responsive', 'responsive_shortcode' );
7368

74-
?>
69+
// Alter Media Uploader output to output shortcode instead
70+
// TODO: Make optional?
71+
// TODO: Make this know what sizes are chosen, rather than hardcoded
72+
function tevkori_responsive_insert_image( $html, $id, $caption, $title, $align, $url ) {
73+
return "[responsive imageid='$id' size1='0' size2='600' size3='1000']";
74+
}
75+
add_filter( 'image_send_to_editor', 'tevkori_responsive_insert_image', 10, 9 );

0 commit comments

Comments
 (0)