In my previous post I showed how to include Tippy calls directly from your theme file where the shortcode is unavailable. In that example, all I wanted was a regular Tippy link so we were able to make use of the tippy_formatLink() function. But let’s take the example one step farther. What if I don’t want a tooltip on a regular link but instead on an image?
For this example, I’m going to have a smaller image and a larger image. The theme file will automatically display the smaller image under the post title and if you hover over it, it will pop up with the larger image. I could do like last time and add a custom field such as post_tooltip_small_image and post_tooltip_large_image with the url or path to those images, but for this example I’ll keep it simple and code it in directly.
See the top of this post, just above the title, for the code in action. Here’s how to get it going:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | if (get_the_ID() == 301) { if (function_exists('tippy_formatLink')) { $small_image = "http://croberts.me/wp-content/uploads/giraffe_small.jpg"; $large_image = "http://croberts.me/wp-content/uploads/giraffe_large.jpg"; $tip_title = ""; $tip_content = '<img src="'. $large_image .'" />'; $tip_content = htmlentities($tip_content, ENT_QUOTES, 'UTF-8'); echo '<a id="tippy_tipSample0" class="tippy_link" title="'. $tip_title .'" href="http://croberts.me/wp-content/uploads/giraffe_large.jpg" onmouseover="Tippy.loadTipInfo(\''. $tip_content .'\', 375, 525, \'tippy_tipSample0\', event);" onmouseout="Tippy.fadeTippyOut();"><img border="0" src="'. $small_image .'" /></a>'; } } |
Lines 1 & 3: Like in the last example, I’m only doing this on a specific post, so check post id. Also, make sure Tippy is loaded.
Lines 5 & 6: Specify where your small and large images are stored.
Line 8: Specify your tooltip title, if desired.
Line 9: Specify what you want to display inside your tooltip. In this case, we’re wanting to display an image.
Line 11: In the last example, I said to run your content through tippy_format_text() instead of htmlentities(). In this case, however, we need to use htmlentities() (and in some cases may need both htmlentities() and tippy_format_text()!) since we’re bypassing tippy_formatLink. So before you pass your content string to the JavaScript function, make sure to run it through htmlentities() the way I do it above.
Line 13: Output the tooltip with your formatting options. If you don’t want this to be a link, just remove the anchor tag and put the id, onmouseover, and onmouseout calls on your img tag. For example:
1 | echo '<img id="tippy_tipSample0" class="tippy_link" onmouseover="Tippy.loadTipInfo(\''. $tip_content .'\', 375, 525, \'tippy_tipSample0\', event);" onmouseout="Tippy.fadeTippyOut();" border="0" src="'. $small_image .'" />'; |
Note that not all browsers (*cough*IE 6*cough) recognize onmouseover and onmouseout on image tags.
It is important to know how Tippy.loadTipInfo is declared. In Javascript, this function maps to domTip_loadTipInfo() and it is declared:
1 | function domTip_loadTipInfo(domTip_tipText, domTip_tipWidth, domTip_tipHeight, domTip_tipId, domTip_event, domTip_tipTitle) |
Whatever id you use for id=”" in the link or image tag, be sure to pass it to domTip_tipId
Set domTip_tipWidth and domTip_tipHeight to 0 if you want them to use default values.
You should always pass even for domTip_event
domTip_tipTitle can be empty, unless you want to show a header title on the tooltip.
That should about do it!




Recent Entries
Tech Talk
Projects
Archive
Tags