Docs    Instagram    Customizations

Add Like and Comment counts to the Instagram lightbox

A snippet that shows like and comment counts in the lightbox. The icons need enabling in feed settings first.

Would you like visitors to see how many likes and comments a post has when they open it in the Instagram lightbox?

In this guide, we’ll add a short JavaScript snippet with WPCode that copies a post’s counts into its lightbox, and a few lines of CSS that style them.

Before you get started, make sure Instagram Feed Pro is installed and activated on your WordPress site. The lightbox is part of Instagram Feed Pro and needs an active license.

Checking Your Account and Settings

On its own, the lightbox shows a post’s caption and a share button, and it can show the post’s comments. The like and comment counts are not part of it, so this guide adds them. To show the comments themselves, turn on the Comments switch in the Lightbox section of the Customize tab. To learn how, see our guide on showing comments in the lightbox.

Likes and comments come only from a Business account. If your feed uses a Personal account, Instagram has stopped sharing them with the plugin, so there are no counts to copy. To change this, reconnect the account with Just my posts or My posts and more on the connection page. See how to reconnect a Personal source or how to connect through Facebook.

The snippet copies the counts from the post in your feed, so the feed has to show them first. To turn them on, follow these steps.

  1. Go to Instagram Feed » All Feeds. Then, click the name of your feed, or the pencil icon, to edit it.
  2. Next, click the Customize tab. Then, click Posts.
  3. Then, choose one of the two settings below.
  • To show the counts under each post, click Like and Comment Summary. Then, switch on Enable. For its other options, see our guide on changing the like and comment summary.
  • To show the counts when a visitor hovers over a post, click Hover State. Then, under Information to display, check Like/Comment Icons.

If you want the counts visible in your feed at all times, choose Like and Comment Summary. If you want them to appear only when a visitor hovers over a post, choose Hover State. Either way, the snippet copies the counts into the lightbox. If you turn on both settings, the snippet copies the counts from the hover state.

Next, we’ll add the JavaScript.

Adding the JavaScript With WPCode

Each time the lightbox shows a post, the plugin runs a function named sbiLightboxAction if your site has one. The snippet below defines that function. It reads the ID of the open post, finds that post in your feed, and copies its counts into the lightbox.

Here is the snippet.

window.sbiLightboxAction = function() {
  var postID = jQuery('#sbi_photo_id').text();
  jQuery('.sbi_lb-details .sbi_meta').remove();
  if ( ! postID ) {
    return;
  }
  var meta = jQuery('#' + postID + ' .sbi_meta').first();
  if ( meta.length ) {
    jQuery('.sbi_lb-details').append( '<div class="sbi_meta">' + meta.html() + '</div>' );
  }
};

The snippet removes the counts of the previous post before it adds the new ones, so they don’t pile up when a visitor moves to the next post. It writes jQuery in full, because WordPress loads jQuery without the $ shortcut. If the post shows no counts, the snippet adds nothing.

A site can only have one function named sbiLightboxAction. If another snippet on your site already defines it, add this code to that function instead of adding a second snippet.

We’ll use WPCode, a free plugin that keeps your custom code in one place. The plugin settings mark the Custom JS box as deprecated and point you to WPCode instead.

  1. Install and activate the WPCode plugin. Then, go to Code Snippets » Add Snippet.
  2. Next, hover over the box named Add Your Custom Code (New Snippet). Then, click + Add Custom Snippet. The WPCode Add Snippet page with an arrow pointing to the + Add Custom Snippet button on the Add Your Custom Code box
  3. In the window that opens, choose JavaScript Snippet. The WPCode code type window with a red box around the JavaScript Snippet option
  4. Next, type a title for your snippet, such as Lightbox like and comment counts. Then, paste the snippet into the code box. You don’t need to add any script tags, because WPCode adds them for you. The WPCode snippet editor with the title filled in and the code pasted into the code box
  5. Then, find the Location field and click it. After that, choose Site Wide Footer. The WPCode Location menu open, with an arrow pointing to the Site Wide Footer option
  6. At the top of the page, click the toggle to switch the snippet from Inactive to Active. The top bar of the WPCode snippet editor with an arrow pointing to the Active toggle
  7. Finally, click Save Snippet. The top bar of the WPCode snippet editor with the toggle set to Active and an arrow pointing to the Save Snippet button

Next, we’ll style the counts.

Adding the CSS

The CSS below styles the counts inside the lightbox. It makes the text dark gray at 14 pixels, floats the counts to the left with 10 pixels of space above them, and adds space between the like and comment counts.

Here is the CSS.

.sbi_lightbox .sbi_meta * { color: #333 !important; font-size: 14px; margin-right: 5px; }
.sbi_lightbox .sbi_meta{ float: left; margin: 10px 0 0 0; }
.sbi_lightbox .sbi_meta span { margin-right: 15px; }

To add it to your site, follow these steps.

  1. In your WordPress dashboard, go to Appearance » Customize.
  2. Next, click Additional CSS.
  3. Next, paste the CSS into the box. Then, click Publish.

You can also add the CSS in WPCode, as a CSS snippet instead of a JavaScript snippet.

Now, open a page that shows your feed and click a post. The like and comment counts appear in the details area of the lightbox. When you move to the next post, they change to match it.

That’s it! Your lightbox now shows the like and comment counts of each post.

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 don’t the counts show in my lightbox?

The snippet copies the counts from the post in your feed, so they have to show there first. Your feed has to use a Business account, Like and Comment Summary or Like/Comment Icons has to be on, and the WPCode snippet has to be Active.

Will the counts change when I move to the next post?

Yes. The plugin runs the sbiLightboxAction function every time the lightbox shows a post, so the counts update with each post.

support

Couldn’t find your answer in the docs?

Contact Support