• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
NameHero® Blog

NameHero® Blog

Web Hosting Tips & Resources From NameHero

  • AI Agents
    • OpenClaw
    • Claude Code
    • n8n
    • Hermes Agent
    • Open WebUI
    • Docker
  • Hosting
    • Web Hosting
    • WordPress Hosting
    • WooCommerce Hosting
    • Enterprise Hosting
    • Email Hosting
    • HeroicGuard
    • GPU Hosting
    • Website Builder
  • VPS
    • Managed VPS
    • Unmanaged VPS
    • Flex VPS
  • Reseller
  • Gaming
  • Domains
  • Account
  • Blog Home
  • Categories
  • Authors

Add JavaScript To Open Links In A New Window In WordPress

Bhagwad Park

Published on: January 6, 2020

Categories: WordPress 0

One of the biggest benefits of the Gutenberg WordPress editor as I mentioned in my earlier post on one year after, is the “Custom HTML” block that allows you to insert arbitrary HTML, JavaScript and CSS code into a WordPress post. I’d previously talked about using it for JSON structured data, but you can also implement cool JavaScript functionality on specific posts, without needing to modify your functions.php file or add PHP code.

I’m finding this quite useful in a variety of ways. Often I’ll have special needs for a certain page that requires a unique piece of JavaScript. And while there are plugins that can do the same thing, it’s nice to have it in a native format with Gutenberg so it’s part of the post itself, and can be easily migrated to another installation without any issues.

One example of this JavaScript potential is the occasional need to open specific links in a new window on a certain post. It can be tiresome and unsustainable to manually add the target=”_blank” attribute to every single link that you want. It’s much easier to do it via a small JavaScript snippet.

Step 1: Get the Class Name of the Links

The first step to using JavaScript to open links in a new window, is to target the right ones. Presumably you don’t want to open every link in this way. Internal links for example, are almost always opened in the same window. For my website, I usually only want the links present in my pricing table to open in this way.

So the first step to targeting them properly, is to obtain their CSS classes in the most narrow way possible. We can do this using our browser developer tools. For example, my site’s links that I want to open in a new window look like this:

Get the CSS Class of the Links

As you can see in this screenshot, my links all have the class “wpsm-comptable” as their CSS class. This is provided by my “Table Maker” plugin.

So once we have the class name, we can create the JavaScript snippet.

Writing the JavaScript

Copy the following code:


jQuery(document).ready(function ($) {
$(document).ready(function(){
$('.wpsm-comptable a').attr('target', '_blank');
});
});

In the snippet above, replace the section in bold with the class name of your links.

Adding the Code to Gutenberg

To insert the above code into your post, simply open your post in Gutenberg, and select the “Custom HTML” block. Paste your code into it and voila, you’re done! Open your post in the browser and view the source code. You should see the target=”_blank” attribute added to the links you want.

A Note on Security

You might have noticed that the ability to add arbitrary JavaScript code to a post can post a security risk if not managed properly. In general, you don’t want to give writers on your team the power to simply add whatever JavaScript they want to a page. It’s very easy to slip in something malicious.

To deal with this, WordPress has the permission called “unfiltered_html”. This permission allows users to post whatever HTML they want in posts and pages, including JavaScript. By default, admins, and editors have access to it, and with multisite installation, only superadmins have it. It’s a carefully guarded privilege for good reason!

But if you’re the only user on your site and if you have admin permissions, then you can safely use it without consequences. Just be careful when giving someone else the role of “editor”, since you’re giving them the ability to insert any kind of JavaScript on your posts and pages!

Bhagwad Park Profile Picture
Bhagwad Park

I’m a NameHero team member, and an expert on WordPress and web hosting. I’ve been in this industry since 2008. I’ve also developed apps on Android and have written extensive tutorials on managing Linux servers. You can contact me on my website WP-Tweaks.com!

Related Posts

How To Back Up Your WordPress Site (And Why You Should Do It Today)

In this post, we’ll take a closer look at why backups matter. We’ll also show you different ways to back up your WordPress website.

Joomla vs. WordPress: Which One Is Better?

In this post, we’ll consider five differences between Joomla and WordPress and how you might go forward with setting up a WordPress website.

How To Start A Newsletter In WordPress

Check out our complete guide on why and how you should start your own newsletter in WordPress.

What Is Self-Hosted WordPress?

In this post, we’ll differentiate self-hosted WordPress from WordPress.com, and discuss the benefits of the former.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow & Subscribe

Exclusive promos, content and more!


Most Popular Posts

NameHero’s Recommended WordPress Plugin and Theme Setup

WordPress Hosting vs. Web Hosting – What’s The Difference?

How To Increase The InnoDB Buffer Pool Size

How To Fix A Stuck All-in-One WP Migration Import

How To Add A Subdomain In Cloudflare

Top Categories

  • WordPress
  • WordPress Tutorials
  • OpenClaw Hosting
  • Enterprise Hosting
  • WooCommerce
  • Web Hosting
  • Resellers
  • Website Security
  • Website Development
  • Website Performance
  • VPS Hosting
  • SEO Tips
  • Announcements
  • Domain Registration
NameHero

NameHero® proudly provides web hosting to over 40,000 customers with 99.9% uptime to over 750,000 websites.

  • Master Card
  • Visa
  • American Express
  • Discover
  • Paypal
Products
  • Web Hosting
  • Managed VPS Hosting
  • Unmanaged VPS Hosting
  • Flex VPS Hosting
  • WordPress Hosting
  • WooCommerce Hosting
  • Reseller Hosting
  • Enterprise Hosting
  • GPU Hosting
  • Email Hosting
  • HeroicGuard
  • Domains
  • Website Builder
  • AI Agent Hosting
Help & Support
  • NameHero Blog
  • NameHero Gaming Blog
  • Support
  • Help Center
  • Migrations
  • Affiliates
  • Gaming Affiliates
  • Call 1-855-984-6263
Company
  • About Us
  • Contact Sales
  • Reviews
  • Uptime
  • We're Hiring

Copyright © 2026 Name Hero, LLC. All rights reserved.
NameHero® is a registered trademark.

  • Privacy Policy
  • Terms of Use
  • Acceptable Use Policy
  • Payment Policy
  • DMCA