Quantcast
Channel: Tips and Tricks HQ
Viewing all articles
Browse latest Browse all 7

How to Create a Custom Post Type for Affiliate Links

$
0
0

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.

In this tutorial, you will be introduced to:

  • 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.

  1. From within your WordPress Dashboard, click on Plugins, then Add New.
  2. Do a search for ‘Custom Post Type UI’.
  3. Click Install Now, then Activate.
  4. Under the newly created CPT UI menu, click on Add Post Type.
  5. Enter affiliate_links for the Post Type Slug.
  6. Enter Affiliate Links for the Plural Label.
  7. Enter Affiliate Link for the Singular Label.
  8. Scroll down to Settings, and change Public to False.
  9. Change Show in Nav Menus to False.
  10. Change Exclude From Search to True.
  11. Remove Editor and Featured Image from the Supports box.
  12. 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.

  1. From within your WordPress Dashboard, click on Plugins, then Add New.
  2. Do a search for ‘Advanced Custom Fields’.
  3. Click Install Now, then Activate.
  4. Click on the newly created Custom Fields menu.
  5. Click the Add New button.
  6. Give it a Title, such as: Affiliate Link Custom Fields.
  7. In the Location box, choose Post Type is equal to: affiliate_links
  8. In the Options box, choose Standard for Style.
  9. Go back up to the top, and click the Add Field button.
  10. In the Field Label box, type: Affiliate URL.
  11. 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.
  12. Leave the Field Type as Text, as we lack the URL type in the Free version of Advanced Custom Fields.
  13. Change Required to Yes.
  14. Click the Close Field button.
  15. Click the Add Field button again.
  16. For Field Label put: Click Counter.
  17. 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.
  18. Change the Field Type to Number.
  19. Change the Default Value to the number zero.
  20. 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.

  1. From within your WordPress Dashboard, click on Affiliate Links, then Add New.
  2. Give the link a name. For testing, just enter: Amazon Home Page
  3. In the Affiliate URL box, enter: https://www.amazon.com/
  4. Click the Publish button.
  5. 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.

The video above shows how to set it up.

Finally, with Pluginception ready, follow these steps to create your custom plugin:

  1. From within your WordPress Dashboard, go to Plugins, then Create a New Plugin.
  2. Give it a name such as: My Affiliate Link Redirect Plugin.
  3. Click the Create a Blank Plugin button.
  4. 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.
  5. Click the Update File button.

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

  1. 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.
  2. Right click the View link, and click Open in a New Tab.
  3. Visit the Tab and you will see that it redirected to the Amazon.com home page as expected.
  4. Now, head back to your Affiliate Links list.
  5. 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 affiliate links. There are general rules, and probably specific rules for the merchant you are an affiliate for. Please do your homework before using this new tool on your Live site.

Below is the code used in this tutorial:

function my_affiliate_link_redirect() {
  if( is_singular( 'affiliate_links' ) ) {
    $count = (int) get_field( 'my_custom_click_counter', get_the_id() );
    $count++;
    update_field( 'my_custom_click_counter', $count, get_the_id() );
    wp_redirect( get_field( 'my_custom_affiliate_url', get_the_id() ) );
    exit();
  }
}
add_action( 'template_redirect', 'my_affiliate_link_redirect' );

The post How to Create a Custom Post Type for Affiliate Links appeared first on Tips and Tricks HQ.


Viewing all articles
Browse latest Browse all 7

Latest Images

Trending Articles





Latest Images