How to Cloak Link in WordPress (Without Plugin)

Cloaking links in WordPress is a popular technique used by many website owners to enhance the appearance and functionality of their links.

This method not only helps in improving the user experience by providing clean and concise URLs but also aids in tracking and managing affiliate links more efficiently.

You can turn a long and hard to read link that look like this: https://www.ebay.com/itm/126494993536?itmmeta=01J4P3808HMH5NCBGVM5ZGP2HP&hash=item1d73b06c80:g:ejEAAOSwCyFmo7wr

To a professional and clean link like this: https://yourwebsite.com/go/?id=ebay

In this tutorial, we will show how you can maintain a professional look for your website while ensuring that your valuable links such as affiliate links are protected from unauthorized modifications by utilizing link cloaking without installing any plugin.

Cloaking Links in WordPress Without Plugin

Step 1: Create a New Folder in WordPress Directory

To begin cloaking your links without a plugin, you’ll need access to your WordPress root directory.

If you’re unfamiliar with this process, we have a separate article that guides you on how to access your WordPress main directory via SFTP.

Once you’re inside, create a new folder in your website’s root directory. The name of this folder will serve as the redirect prefix to cloak the original link.

Here are a few folder names you may want to consider:

  • go
  • out
  • recommends
  • etc

Step 2: Create the Main File

Navigate into the folder you just created and create a new file named index.php. This file will contain the core code for the cloaking process.

Next, copy the following code, which enables the cloaking functionality, and paste it into the index.php file.

<?php
 
$id  	= isset( $_GET['id'] ) ? rtrim( trim( $_GET['id'] ), '/' ) : 'default';
$f	= fopen( 'redirects.txt', 'r' );
$urls	= array();
 
// The file didn't open correctly.
if ( !$f ) {
	echo 'Make sure you create your redirects.txt file and that it\'s readable by the redirect script.';
	die;
}
 
// Read the input file and parse it into an array
while( $data = fgetcsv( $f ) ) {
	if ( !isset( $data[0] ) || !isset( $data[1] ) )
		continue;
	
	$key = trim( $data[0] );
	$val = trim( $data[1] );
	$urls[ $key ] = $val;
}
 
// Check if the given ID is set, if it is, set the URL to that, if not, default
$url = ( isset( $urls[ $id ] ) ) ? $urls[ $id ] : ( isset( $urls[ 'default' ] ) ? $urls[ 'default' ] : false );

if ( $url ) {
	header( "X-Robots-Tag: noindex, nofollow", true );
	header( "Location: " .  $url, 302 );
	die;	
} else {
	echo '<p>Make sure yor redirects.txt file contains a default value, syntax:</p>
	<pre>default,http://example.com</pre>
	<p>Where you should replace example.com with your domain.</p>';
}

Be sure to save and upload the file to activate the code. Credit for the code goes to the Yoast GitHub repository.

Step 3: Create the Redirect List File

Next, create a text file named redirects.txt. This file will contain the slugs for each link, along with the original URLs that need to be cloaked.

On the first line of the file, add default, https://yourwebsite.com/ to handle edge cases.

Then, create a list of the slugs and corresponding links you want to cloak, with each entry on a new line. Here’s the format for each link:

  • custom-slug, https://youraffiliate.com/therestofaffiliatelink

Ensure that your affiliates permit the cloaking of their affiliate links. For instance, Amazon does not allow the cloaking of their affiliate links. However, most other affiliates do permit link cloaking

Below is a screenshot showing the content of the redirects.txt file:

Finally, save and upload the file to make the changes live and continue to the next step.

Step 4: Block Crawlers

To prevent Google and other search engines from following your redirects, add the following rule to your site’s robots.txt file. This file is located in your WordPress root directory.

  • Disallow: /folder-name/

Replace folder-name with the name of the folder you created earlier. If your robots.txt file already has a Disallow rule, simply add /folder-name/ to the disallow section.

If you don’t have a robots.txt file yet, you can create one. Navigate to your WordPress root directory and create a new file named robots.txt.

Here is an example of a simple robots.txt file with the rule we use for our /go/ folder:

# START WPP BLOCK
# ---------------------------
User-agent: *
Disallow: /go/

Sitemap: https://www.divi.lmtrtutorials.com/sitemap_index.xml
# ---------------------------
# END WPP BLOCK


This robots.txt file tells all user agents not to crawl any URLs that start with https://www.divi.lmtrtutorials.com/go/.

Once all files are ready, continue to the next step to start adding cloaked links.

Step 5: Add Cloaked Links

Adding cloaked links is similar to adding regular links to a paragraph, button, image, etc.

The key difference is that instead of using the original link, you’ll use a cloaked link in the following format:

  • https://www.yourwebsite.com/go/?id=slug

In this format, go refers to the folder you created earlier, and slug is the custom slug you assigned for each link in the redirects.txt file.

For example, your cloaked link might look like this:

  • https://www.divi.lmtrtutorials.com/go/?id=ebay

Below is a preview of the cloaked link results. You’ll see that we use links in the format mentioned above. When these links are clicked, they will direct to the original destination on each respective website.

The Bottom Line

In this guide, you learned how to maintain a professional and clean appearance for your website’s URLs while efficiently managing and protecting your valuable links, such as affiliate links, from unauthorized modifications through link cloaking — all without the need for a plugin.

This page may contain affiliate links, which help support our project. Read our affiliate disclosure.
Picture of Hendri Risman

Hendri Risman

Hendri is a WordPress expert and a writer staff at WPPagebuilders. He writes solutions on how to get things fixed in WordPress a lot. Mostly without involving a plugin.
Want to start a profitable blog with WordPress? OF COURSE!

Leave a Comment

Hey 👋🏻
Got WordPress knowledge?
Why not turning it into profit? Click the button below to learn how.

Your popup content goes here

50%

Where should we send the template?

After entering your email address, you will be added to our newsletter subscribers. You can unsubscribe anytime.

Want to Build Passive Income Like the One on the Screenshot Below?

Click the button on the right side to learn how 👉🏻
5 easy steps to turn your WordPress knowledge into monthly recurring revenue.

Click the above button to close the popup.