How to Create a Custom Post Type for Affiliate Links

Depending on the type of web site that you are creating, and the desired content structure, it’s sometimes very powerful to create custom post types with custom fields.

Some of the time, you may want 100% control over the fields for your custom post type.

At a minimum, you may want to keep the post title, although you actually don’t have to.

Keeping the title is handy though for when viewing the content within the Admin and for generating a default post slug.

The title doesn’t actually have to be used on the front end if it’s not needed.

Let’s explore creating a custom post type for affiliate links, which can be expanded on at anytime.

Today, you will simply create an interface to store the affiliate URL, give it a name for easy reference, and to keep track of how many times the affiliate URL was visited.

Upon visiting the domain-branded & friendlier custom post URL, as opposed to the ugly affiliate URL, the counter will increment by one, and the browser will redirect to the affiliate URL.

Future upgrades for this application can involve creating country specific versions for each affiliate link, and redirecting to the appropriate URL based on the country of the visitor. This feature is handy if you are an Amazon affiliate.

Also, it can be expanded upon to pass data to the Custom Post, which can then pass the data along dynamically within the affiliate URL for tracking purposes.

Beyond that, for cleaner admin management, you can use the Admin Columns plugin so that you can display the Custom Post URL and Click Count in the Custom Posts list.

That way, it’s quicker to grab the Custom Post’s URL whenever you want to use the affiliate link, as it will be available at a glance instead of having to go into the Editor for the custom post, or having to get it from the View Link.

How to create a custom post type for affiliate links.How to add custom fields to the custom post type for the affiliate URL, and for a click counter.How to create a custom plugin to handle the incrementing of the click counter, and for redirecting the visitor to the affiliate URL.

Let’s create the custom post type now.

From within your WordPress Dashboard, click on Plugins, then Add New.Do a search for ‘Custom Post Type UI’.Click Install Now, then Activate.Under the newly created CPT UI menu, click on Add Post Type.Enter affiliate_links for the Post Type Slug.Enter Affiliate Links for the Plural Label.Enter Affiliate Link for the Singular Label.Scroll down to Settings, and change Public to False.Change Show in Nav Menus to False.Change Exclude From Search to True.Remove Editor and Featured Image from the Supports box.Finally, click the Add Post Type button.

You will now see on the left side, a new menu option called Affiliate Links.

If you have a peek inside the Add New screen for Affiliate Links, you will see that Title, Slug, Status, Visibility, and Publish Date are the only editable settings.

Let’s now add a field for the URL, and another for the counter.

Follow along below to add custom fields to the custom post type.

From within your WordPress Dashboard, click on Plugins, then Add New.Do a search for ‘Advanced Custom Fields’.Click Install Now, then Activate.Click on the newly created Custom Fields menu.Click the Add New button.Give it a Title, such as: Affiliate Link Custom Fields.In the Location box, choose Post Type is equal to: affiliate_linksIn the Options box, choose Standard for Style.Go back up to the top, and click the Add Field button.In the Field Label box, type: Affiliate URL.Change the Field Name to: my_custom_affiliate_url or something similar, to keep it unique from custom fields used in other plugins and tools that may find their way into your WordPress site down the line. Please make note of the name you use because we will be referencing it within the plugin.Leave the Field Type as Text, as we lack the URL type in the Free version of Advanced Custom Fields.Change Required to Yes.Click the Close Field button.Click the Add Field button again.For Field Label put: Click Counter.Change the Field Name to: my_custom_click_counter or something similar, to keep it unique. Again, please make note of the name you use because we will be referencing it within the plugin.Change the Field Type to Number.Change the Default Value to the number zero.Click the Publish button on the right side of the screen.

Now, let’s create a new Affiliate Link that we can use for testing purposes after we create the plugin.

From within your WordPress Dashboard, click on Affiliate Links, then Add New.Give the link a name. For testing, just enter: Amazon Home PageIn the Affiliate URL box, enter: https://www.amazon.com/Click the Publish button.Click on Screen Options, put a check next to Slug if not already selected, and click Update on the post. That way, you can modify the URL from the default that was created based on the title, if desired.

Now, if you view this Affiliate Link Post right now, you will see that it simply displays a page on your site with the Title.

That’s not the desired effect however.

We want it to redirect to the affiliate URL, but not before silently incrementing the hit counter.

Let’s create a quick plugin to accomplish these tasks.

To follow along, please be sure that you have the Pluginception plugin installed, as that is the approach we will be using to create a new plugin.

From within your WordPress Dashboard, go to Plugins, then Create a New Plugin.Give it a name such as: My Affiliate Link Redirect Plugin.Click the Create a Blank Plugin button.Copy in the code shown in the video or below these steps if you are following along within the written tutorial. Please make certain that you use the appropriate field names if you didn’t use the names I suggested in the previous steps.Click the Update File button.

To test your work, follow along with these quick steps:

From within your WordPress Dashboard, click on Affiliate Links. On a side note, as you can see in this screen, using a tool like Admin Columns could be handy because it would allow you to add your custom fields and other fields such as the Post URL in the columns. Either way, you can right click the View link to get the Affiliate Link or Post URL to use whenever you want to display your affiliate link somewhere.Right click the View link, and click Open in a New Tab.Visit the Tab and you will see that it redirected to the Amazon.com home page as expected.Now, head back to your Affiliate Links list.Click on the Edit link for your Amazon test link, and see that the Click Counter is now a 1.

A few extra things worth mentioning here.

One, be sure to always use your Custom Post Type’s URL when wanting to link to your affiliate link. If you use your actual affiliate link, this whole process will have been for nothing. The Custom Post Type URL is more attractive, has your domain branded in it, and keeps track of clicks. And perhaps in the future, it can also geo-target and do other wonderful things.Secondly, since these are affiliate links, be certain to nofollow them whenever used.Also, keep in mind that these links technically can be used on other sites, not just the site you are managing them with. Do so only with permission of course.Lastly, there is a rule that you must be transparent about the use of affiliate links. Be sure to look up the rules and requirements governing the use of af