whb-logo8whb-logo8whb-logo8whb-logo8
  • Home
  • About
    • About
    • Contact
  • Blog
  • Methodology
  • Affiliate Disclosure
    • Disclaimer
    • Privacy Policy
✕

WooCommerce Part 3 – How to Open Specific Product Links in a New Tab or Window

Buckle up—it’s time for part 3 of the WooCommerce coding tips series! In this post, we’re going to look at how to open specific product links in a new tab or window when a user clicks on them—sounds simple, right?

The truth is that it’s actually pretty simple, it just requires a little bit of custom PHP coding to make it work with your WooCommerce installation. Let’s jump in and take a look at the problem!

The Problem: Opening Specific Product Links in a New Tab While Leaving Others to Open in the Current Window

Let’s take a look at an example! For instance, let’s say that you run a t-shirt store using WooCommerce, and you’ve got a variety of t-shirts.

Let’s also pretend that you’ve got a specific line of t-shirts that have dinosaurs on them, and for whatever reason, you want all of the single dinosaur t-shirt product pages to open in a new tab or window when clicked so that users don’t leave the main t-shirt store page to view the dinosaur t-shirts.

The Solution: A Custom PHP Function and a Little JQuery Magic

In order to do this, you’ll need to create some custom PHP combined with a little JQuery magic to select the div that the dinosaur t-shirt links are in.

Here’s the full custom function:

/* Add target="_blank" to WooCommerce Product Links on Specific Page */


add_action('wp_footer',function(){


if ( is_page( 'page-id-goes-here' ) ) {


echo '';


}


});

Let’s jump in an take a look at each part of the code to see what it’s doing:

/* Add target="_blank" to WooCommerce Product Links on Specific Page */

add_action('wp_footer',function(){

In this section, we’re simply calling a custom function in the footer to make sure that the entire page is loaded first (including all of the product links).

if ( is_page( 'page-id-goes-here' ) ) {


echo '[BEGIN SCRIPT TAG GOES HERE]

Next, we’re telling PHP to check the page that the user is on in order to: 1) ensure it’s a page and 2) ensure it’s a specific page. WordPress uses numerical page IDs to refer to specific pages, and in this case, you simply replace ‘page-id-goes-here’ with your specific page ID, such as ‘543’ or whatever your page ID happens to be.

Side note: if you aren’t sure how to get the page ID it’s easy—just go into your WordPress admin panel, navigate to the pages section and hover your mouse over the page title (the link you click to edit the page). In the lower-left-hand corner of the screen, you’ll see the page ID displayed.

After checking the page ID, the function then calls a script (JQuery wrapped in the <script> tags) based on that conditional logic.

//select all links in product div

jQuery("div.div-name-goes-here a").attr("target","_blank");

[END SCRIPT TAG GOES HERE]';

}

});

Then, this JQuery checks the page for a specific div name and then applies a target_blank (open in new window/tab) attribute to that div.

For this to work properly, you have to wrap the product link that you want to open in a new tab/window in a div with a specific name. So, if you had a product called T-Rex Dinosaur Tee, you’d need to make sure that it’s wrapped in a div with an ID that can be referenced later, such as:

T Rex Dinosaur Tee Product Page

Then, in the function listed above, you replace “div-name-goes-here” with the name of your t-rex div ID (t-rex-tee). So instead of the line “div.div-name-goes-here” you would have “div.t-rex-tee” etc.

And that’s it! After you follow these steps and clear your website cache at your hosting company etc., you should see your specific links opening in new tabs/windows.

Hopefully this helps anyone looking for a quick coding trick to open specific WooCommerce in a new tab/window! Stay tuned for part 4 of the WooCommerce coding series!

P.S. if you’re looking for the WooCommerce codex you can find that here: https://docs.woocommerce.com/documentation/plugins/woocommerce/woocommerce-codex/

Related posts

July 27, 2020

WooCommerce Part 5 – How to Exclude Products in a Specific Category from WooCommerce Search


Read more

Frustrated with Your WooCommerce Hosting Company?

Annoyed with your slow, buggy WooCommerce host? Upgrade to one of these rockstars!

Visit WP Engine

Visit SiteGround

A quick note: if you purchase hosting from one of these affiliate links, we might get a small commission.

It helps keep the content on this site free and fresh, and is much appreciated!

Guides & Reviews

  • The Best WordPress Hosting – An Expert Guide
  • The Best Web Hosting for Small Businesses
  • A Guide to Web Hosting for Nonprofits
  • The Best Domain Name Registrars
  • The Ultimate Guide to Web Hosting Alternatives
  • Web Hosting Glossary
  • Web Hosting How-Tos & Tutorials
  • Best Web Hosting & Software

Comparisons

  • The Fastest Web Hosting - Find Out Who Wins!
  • A Big List of Web Hosting Companies
  • Bluehost vs Hostgator
  • Dreamhost vs Hostgator
  • Bluehost vs Godaddy
  • Siteground vs Hostgator
  • Bluehost vs Dreamhost
  • The 5 Best GoDaddy Alternatives
  • Top 12 JMeter Alternatives
  • 20 Best Load Testing Tools
  • Hosts We Don't Recommend

Deals & Savings

  • Bluehost Coupon Codes & Discounts
  • Hostinger Coupon Codes & Deals
  • SiteGround Coupon Codes & Deals
  • DreamHost Coupon Codes & Discounts
  • HostGator Coupon Codes & Discounts
  • WPEngine Coupon Codes & Promos
  • NameSilo Coupon Codes & Deals
  • Web Hosting Coupons & Deals
  • Cheap Web Hosting
  • Cheap Email Hosting
✕
Follow @WebHostBuddy
Copyright 2025 WebHostingBuddy.com. All rights reserved.