{"id":130303,"date":"2014-06-27T08:00:00","date_gmt":"2014-06-27T12:00:00","guid":{"rendered":"http:\/\/premium.wpmudev.org\/blog\/?p=130303"},"modified":"2014-06-27T08:01:22","modified_gmt":"2014-06-27T12:01:22","slug":"add-amazon-style-zooming-to-your-wordpress-post-images","status":"publish","type":"post","link":"https:\/\/wpmudev.com\/blog\/add-amazon-style-zooming-to-your-wordpress-post-images\/","title":{"rendered":"Add Amazon-style Zooming To Your WordPress Post Images"},"content":{"rendered":"<p>Would you like to add a zooming feature to your WordPress images, just like Amazon has on its product pages?<\/p>\n<p>Are you looking for the ability to pan around a thumbnail and see a magnified version or to zoom in and out using just the mouse scroll wheel?<\/p>\n<p>Then you need to integrate the elevateZoom jQuery plugin into your WordPress site. Here&#8217;s how.<\/p>\n<p><!--more--><\/p>\n<figure id=\"attachment_130310\" class=\"wp-caption aligncenter\" data-caption=\"true\"><a rel=\"lightbox[130303]\" class=\"blog-thumbnail\" href=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2014\/06\/Screen-Shot-2014-06-27-at-9.25.10-PM.png\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" class=\"size-ratio-large wp-image-130310\" src=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2014\/06\/Screen-Shot-2014-06-27-at-9.25.10-PM-700x237.png\" alt=\"Image with elevateZoom lens activated and larger image showing\" width=\"700\" height=\"237\" \/><\/a><figcaption class=\"wp-caption-text\">Adding Amazon-style image zooming and panning is easy with elevateZoom<\/figcaption><\/figure>\n\n<p><a title=\"Read more about the elevateZoom jQuery plugin\" href=\"http:\/\/www.elevateweb.co.uk\/image-zoom\/\" rel=\"noopener\" target=\"_blank\">elevateZoom<\/a> from Elevate Web in the UK is a very cool jQuery plugin indeed.<\/p>\n<p>Highly configurable it provides all the options required to replicate the Amazon imaging zooming experience and more as <a title=\"Have a play with the elevateZoom examples\" href=\"http:\/\/www.elevateweb.co.uk\/image-zoom\/examples\" rel=\"noopener\" target=\"_blank\">these examples illustrate<\/a>.<\/p>\n<p>There are a couple of existing plugins that add elevateZoom to a WordPress site but they either use shortcodes to wrap around the image or are\u00a0for galleries. I wanted something a bit more flexible, something that you can have work on an existing site without needing to update your content.<\/p>\n<p>So, here&#8217;s a new elevateZoom plugin.<\/p>\n<h2>Using The elevateZoom Plugin<\/h2>\n<p>As far as possible, I&#8217;ve tried to make the plugin work with standard WordPress output. It uses a combination of server-side and client-side processing which is explained in more detail later.<\/p>\n<p>The plugin won&#8217;t do anything out-of-the-box, so\u00a0<a title=\"Download the elevateZoom plugin\" href=\"https:\/\/github.com\/pommiegranit\/elevatezoom\/archive\/master.zip\" rel=\"noopener\" target=\"_blank\">download it<\/a>\u00a0and we&#8217;ll step through setting it up.<\/p>\n<h3>Configuring The Settings<\/h3>\n<p>Installing and activating the plugin will add a new options page, <em>elevateZoom<\/em> to the <em>Settings<\/em>\u00a0menu in your site&#8217;s admin interface.<\/p>\n<figure id=\"attachment_130312\" class=\"wp-caption aligncenter\" data-caption=\"true\"><a rel=\"lightbox[130303]\" class=\"blog-thumbnail\" href=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2014\/06\/Screen-Shot-2014-06-27-at-9.51.09-PM.png\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-130312\" src=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2014\/06\/Screen-Shot-2014-06-27-at-9.51.09-PM.png\" alt=\"Screengrab of the admin settings page for elevateZoom\" width=\"700\" height=\"680\" \/><\/a><figcaption class=\"wp-caption-text\">The global settings can be overidden on the image itself<\/figcaption><\/figure>\n<p>There are half-a-dozen settings there that will let you change such options as the zoom type, the lens size and shape, the zoom window position and the height and width of the zoom window. To find out more about the settings values and what they do, have a look at the <a title=\"View the configuration settings\" href=\"http:\/\/www.elevateweb.co.uk\/image-zoom\/configuration\" rel=\"noopener\" target=\"_blank\">configuration details<\/a>.<\/p>\n<p>You&#8217;ll notice that there are many more options than appear on the settings page. I&#8217;ve only added the most common so\u00a0if you want to add more you&#8217;ll need to edit the plugin script.<\/p>\n<p>The most important setting is the <em>class<\/em>. When a page is loaded, the elevateZoom functionality will be attached to all images that have the specified class. You can manually add the class to an image if you want to control exactly which images can be zoomed, or you can define a more generic class to include a number of images.<\/p>\n<p>In fact, this setting can take multiple classes, just separate them with a space and don&#8217;t worry about the &#8220;.&#8221;. This means that you can add the functionality to any embedded image on most WordPress sites by specifying <em>alignright alignleft aligncenter alignnone<\/em> as the classes.<\/p>\n<p>If you have a WooCommerce or MarketPress site then you can attach the functionality to your product images by specifying a common class. For example, WooCommerce&#8217;s Mystile theme adds the class <em>attachment-shop_single<\/em>\u00a0to the image on a product page, so using this class will automatically attach the elevateZoom function to the image on each product page.<\/p>\n<p>You must click on <strong>Update<\/strong> at least once to save the settings, even if you don&#8217;t change them.<\/p>\n<h3>Overriding The Settings In The Content<\/h3>\n<p>The settings page is good for configuring global behavior but what if you want to change the behavior for certain images?<\/p>\n<p>The plugin allows you to override the global settings by adding the settings as attributes on the &lt;<em>img&gt;<\/em> tag. The name of the attributes is slightly different to their Javascript equivalents due to attributes being converted to all lower case. It gets a bit convoluted but\u00a0attribute names need to be split into their individual words, separated by a hyphen and prefixed with <em>ez-<\/em>.<\/p>\n<p>For example, to specify a <em>zoomWindowHeight<\/em> of <em>200<\/em> add <em>ez-zoom-window-height=&#8221;200&#8243;<\/em> to the &lt;<em>img&gt;<\/em> tag.<\/p>\n<p>The <em>ez-<\/em> prefix is so that the plugin knows this is an <em>elevateZoom<\/em> setting.<\/p>\n<h3>Determining The Full-Size Image To Use For Zooming<\/h3>\n<p>The only attribute that the plugin actually needs is <em>data-zoom-image<\/em> which is set to the URL of the full-size image.<\/p>\n<p>The plugin will automatically use the URL from a\u00a0parent &lt;<em>a&gt;<\/em> tag &#8211;\u00a0the default output for WordPress if you chose to link the image to the media file when inserting into a post.<\/p>\n<p><code><\/code><\/p>\n<p>&lt;a href=&#8221;http:\/\/www.test.dev\/wp-content\/uploads\/2013\/03\/the-dark-knight-rises.jpg&#8221;&gt;&lt;img class=&#8221;elevateZoom alignleft wp-image-1042 size-medium&#8221; src=&#8221;http:\/\/www.test.dev\/wp-content\/uploads\/2013\/03\/the-dark-knight-rises-198&#215;300.jpg&#8221; alt=&#8221;The Dark Knight Rises&#8221; width=&#8221;198&#8243; height=&#8221;300&#8243; \/&gt;&lt;\/a&gt;<\/p>\n<p>&nbsp;<\/p>\n<p>Otherwise, you can manually add the <em>data-zoom-image<\/em> attribute to the <em>&lt;img&gt;<\/em> tag.<\/p>\n\n<h2>Delving A Little Deeper<\/h2>\n<p>For those of you who are interested in how the plugin works, here&#8217;s a brief walkthrough.<\/p>\n<p>The actually zooming functionality is provide by a jQuery plugin, which is obviously client-side, so the WordPress plugin is mainly concerned with inserting the necessary javascript into the pages and providing the Settings page.<\/p>\n<p>The Settings page uses the standard Settings API. I saved a lot of time by using the <a title=\"Visit the WP Settings API tool\" href=\"http:\/\/wpsettingsapi.jeroensormani.com\/\" rel=\"noopener\" target=\"_blank\">WP Settings API Generator tool<\/a> to provide the basic coding and just added the checks for an existing value for each option and the assigning of a default value.<\/p>\n<p>Those settings are required by the client-side javascript and so are inserted into the page via the <a title=\"Read more about this function on the WordPress Codex\" href=\"http:\/\/codex.wordpress.org\/Function_Reference\/wp_localize_script\" rel=\"noopener\" target=\"_blank\">wp_localize_script<\/a> function. As the Generator code automatically sets up all the options as an array, this makes providing the settings to the wp_localize_script as easy as grabbing the option via the get_option function.<\/p>\n<p>This is what the output looks like in the HTML:<\/p>\n<p><code><\/code><\/p>\n<p>\/* &lt;![CDATA[ *\/<br \/>\nvar ezSettings = {&#8220;ez_class&#8221;:&#8221;elevateZoom&#8221;,&#8221;zoomType&#8221;:&#8221;window&#8221;,&#8221;lensShape&#8221;:&#8221;square&#8221;,&#8221;lensSize&#8221;:&#8221;200&#8243;,&#8221;zoomWindowPosition&#8221;:&#8221;1&#8243;,&#8221;zoomWindowHeight&#8221;:&#8221;400&#8243;,&#8221;zoomWindowWidth&#8221;:&#8221;400&#8243;};<br \/>\n\/* ]]&gt; *\/<\/p>\n<p>&nbsp;<\/p>\n<p>The client-size scripts themselves are added to the output via the <a title=\"Read more about this function on the WordPress Codex\" href=\"http:\/\/codex.wordpress.org\/Function_Reference\/wp_enqueue_script\" rel=\"noopener\" target=\"_blank\">wp_enqueue_script<\/a> function. As a jQuery plugin, the elevateZoom plugin is made dependent on jQuery loading; the custom script\u00a0that\u00a0adds the elevateZoom functionality is made dependent on the plugin.<\/p>\n<p>The custom script (elevateZoomInit.js) is a smallish function that is attached to jQuery&#8217;s <em>document.ready<\/em>. It runs any elements that have the class specified in the elevateZoom settings in the admin interface and performs the following tasks:<\/p>\n<ol>\n<li>Sets up the <em>data-zoom-image<\/em> attribute using the parent <em>&lt;a&gt;<\/em> <em>href<\/em> (if available and no <em>data-zoom-image<\/em> specified on the &lt;img&gt;)<\/li>\n<li>Loops through all the attributes on the image converting any that start with <em>ez-<\/em> and adding (or replacing) the value\u00a0in the settings<\/li>\n<li>Attaches the <em>elevateZoom<\/em> function to the image with the updated settings<\/li>\n<\/ol>\n<p>All this just helps to set up the elevateZoom functionality: all the real magic, of course, is provided by the jQuery plugin itself!<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Would you like to add a zooming feature to your WordPress images, just like Amazon has on its product pages? Then you need to integrate the elevateZoom jQuery plugin into your WordPress site. Here&#8217;s how.<\/p>\n","protected":false},"author":262394,"featured_media":130310,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"blog_reading_time":"","wds_primary_category":0,"wds_primary_tutorials_categories":0,"footnotes":""},"categories":[263],"tags":[16],"tutorials_categories":[],"class_list":["post-130303","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","tag-images"],"_links":{"self":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/130303","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/users\/262394"}],"replies":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/comments?post=130303"}],"version-history":[{"count":1,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/130303\/revisions"}],"predecessor-version":[{"id":215825,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/130303\/revisions\/215825"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/media\/130310"}],"wp:attachment":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/media?parent=130303"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/categories?post=130303"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/tags?post=130303"},{"taxonomy":"tutorials_categories","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/tutorials_categories?post=130303"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}