By default disabling the lightbox will also remove the hover effect on each post. This will preserve that effect and make the post link back to Instagram on click.
Would you like your Instagram posts to open on Instagram and still keep the hover effect?
In this guide, we’ll add a short JavaScript snippet with WPCode that changes where a post link goes, without turning the lightbox off.
Before you get started, make sure Instagram Feed Pro is installed and activated on your WordPress site.
- Linking Posts to Instagram and Keeping the Hover Effect
- Adding the Snippet in WPCode
- Frequently Asked Questions
Linking Posts to Instagram and Keeping the Hover Effect
When you turn the lightbox off in the feed editor, Instagram Feed Pro also hides the hover layer. That layer holds the hover color, username, date, caption and icons. So, you lose the hover effect along with the lightbox.
To keep the hover effect, leave the lightbox on. Then, use the snippet below to change where each post link goes.
To turn the lightbox on or off for a feed, see Setting Up the Instagram Feed Lightbox.
Important! Do not turn the lightbox off for the feed. In the feed editor, the Enable switch in the Lightbox section of the Customize tab stays on.
Here is the snippet.
var sbi_custom_js = function() {
var $ = jQuery;
$('#sb_instagram .sbi_item').each(function(){
var $sbi_link_area = $(this).find('.sbi_link_area');
$sbi_link_area.attr({
'href': $sbi_link_area.attr('data-url'),
'target': '_blank'
}).removeAttr('data-lightbox-sbi');
});
};
The snippet goes through every post in your feeds and does three things.
- It sets each post’s link to the post’s address on Instagram.
- It sets
'target': '_blank', which makes the link open in a new tab. - It removes the attribute that opens the lightbox, so a click no longer opens it.
To open the link in the same tab instead, delete the 'target': '_blank' line from the snippet. Also delete the comma at the end of the line above it.
The plugin runs the sbi_custom_js function when the feed finishes loading, and again after a visitor clicks the Load More button. So, the posts that load later get the same change. The line var $ = jQuery; comes first in the function because WordPress loads jQuery without the $ shortcut.
Next, we’ll add the snippet to your site.
Adding the Snippet in WPCode
We’ll use WPCode, a free plugin that keeps your custom code in one place. The Custom JS box in the plugin settings is deprecated, so don’t use it for new code.
- Install and activate the WPCode plugin. Then, go to Code Snippets » Add Snippet.
- Hover over the box named Add Your Custom Code (New Snippet). Then, click + Add Custom Snippet.
- In the window that opens, choose JavaScript Snippet.
- Next, type a title for your snippet, such as Open Instagram posts on Instagram. Then, paste the snippet into the code box. You don’t need to add any script tags, because WPCode adds them for you.
- Then, find the Location field and click it. Then, choose Site Wide Footer.
- At the top of the page, click the toggle to switch the snippet from Inactive to Active.
- Finally, click Save Snippet.
A site can only have one function named sbi_custom_js. If another snippet on your site already defines it, such as the Instagram Feed Custom JS snippet that Instagram Feed’s one-click migration creates from the old Custom JS box, don’t add a second one. Instead, paste everything between the first line and the last line of the snippet at the top of that function, right after its first line.
Now, open a page that shows your feed and hover over a post. The hover effect appears as before. When you click the post, it opens on Instagram in a new tab instead of in the lightbox.
That’s it! Your posts now open on Instagram, and your feed keeps its hover effect.
Next, see how the WPCode integration works to browse ready-made snippets and move your old custom code into WPCode.
Frequently Asked Questions
Below, we’ve answered some of the most common questions about this snippet.
Why does the hover effect disappear when I turn the lightbox off?
When the lightbox is off, the plugin gives the hover layer a style that hides it. That is why this guide keeps the lightbox on and changes the post link with code instead.
Can I link posts to Instagram with a setting instead of code?
Partly. In the feed editor, go to Customize » Posts » Hover State. Under Information to display, you can show the Instagram Icon. That icon opens the post on Instagram in a new tab. The rest of the post still opens the lightbox, so use the snippet if you want the whole post to link out.
Will the snippet work on posts that load after I click Load More?
Yes. The plugin runs the sbi_custom_js function again after each Load More click, so the new posts get the same change.