Quantcast
Channel: Tutorials Archives - Italicized Creative
Viewing all articles
Browse latest Browse all 35

How to Create a Shortcode to Quickly Add Disclosures to Posts

$
0
0

How to Create a Shortcode to Quickly Add Disclosures to Posts

Did you know that if you use affiliate links on your blog you need to include a disclosure? And if you’re putting this disclosure at the very bottom of your blog post, or in your site’s footer/sidebar, you’re doing it wrong. Affiliate links need to be disclosed visibly and before anyone can get a chance to click on them. I’m not going to go into details, but you can read more about it here.

Specifics aside, you might have noticed that on my (recent) posts that include affiliate links I include the same exact disclaimer. Here it is again if you haven’t seen it:

This post contains affiliate links. For more information, visit my disclosure page.

Now do you think I sit there and type up the same disclaimer each and every time? Nope. Who has time for that? And while I could copy and paste it, what if I want to change it in the future? Do you think I want to go back through all my posts to update it?

Instead of typing it (or copying and pasting it), I just use a shortcode! All I do is type [affiliate_disclosure] into my posts, and boom, the disclaimer is added. And then when I want to update it, I just have to update it in one spot and it’ll be updated everywhere.

Want to create your own affiliate disclosure shortcode? Here’s how:

Step 1 | Create a Custom Plugin

You might be wondering what plugins have to do with shortcodes. Let me explain. First of all, you could definitely insert your code into your functions.php file. But what happens when you switch themes down the line? You’ll be left with shortcodes in your blog posts that do nothing. Of course you could try and remember to copy and paste the shortcode to your new theme, but will you honestly remember?

That’s where a custom plugin comes in handy. By putting your shortcode code in a plugin, you can change themes all you want and the shortcode will keep on working.

So let’s create our custom plugin. First, create a PHP file called allyssa-barnes-custom.php (of course use your own your own name, initials, etc). Then add the following bit of code to the blank file:

<?php
/**
* Plugin Name: Allyssa Barnes Custom
* Description: A custom functionality plugin for allyssabarnes.com
* Version: 1.0
* Author: Allyssa Barnes
* Author URI: http://allyssabarnes.com
*/

And of course, change the text in the above code block to fit your name and site.

If you got this far, congratulate yourself, you just created a plugin!

Of course since it’s just the heading portion of the plugin, it’s not going to do anything. Let’s change that.

Step 2 | Create a Custom Shortcode

Now it’s time to create a custom shortcode. To do so, add the following bit of code below the code from step 1:

function ab_affiliate_shortcode( $atts ) {
     $disclaimer = '<p class="disclosure">YOUR AFFILIATE DISCLOSURE HERE</p>';
     return $disclaimer;
}
add_shortcode( 'affiliate_disclosure', 'ab_affiliate_shortcode' );

Now what does the above code do? Simply put, it allows you to type [affiliate_disclosure] into any of your posts and the text you specify will be displayed instead.

If you were to put the above code in your functions.php file, you’d have a working shortcode. But since we’ve created a plugin, we’re not quite there yet.

If you’re creating a shortcode for something else, or would rather use another bit of text as your shortcode, substitute affiliate_disclosure in the above code with whatever shortcode you want to use.

So for example, changing affiliate_disclosure to my_easy_to_remember_shortcode would create a shortcode of [my_easy_to_remember_shortcode] for you to enter into your posts.

Here’s the code again, but with my_easy_to_remember_shortcode inserted. Notice how the code is the same, except that affiliate_disclosure was changed to my_easy_to_remember_shortcode.

function ab_affiliate_shortcode( $atts ) {
     $disclaimer = '<p class="disclosure">YOUR AFFILIATE DISCLOSURE HERE</p>';
     return $disclaimer;
}
/* notice the change in the line below */
add_shortcode( 'my_easy_to_remember_shortcode', 'ab_affiliate_shortcode' );

Step 3 | Install Your Custom Plugin

Your plugin is fully functional at this point, so it’s time to upload it to your site. To keep things organized, we’re going to create a folder to put our PHP file into. So create a folder (I called mine allyssabarnescustom) and put your PHP file inside.

Now if you know how to use FTP, you can go ahead and upload the folder to your plugins folder. If not, go ahead and compress the folder you just created to create a zip file.

Go to your WordPress dashboard, click Plugins > Add New, then Upload Plugin. Select your zip file and upload! Once it’s uploaded, click on activate. Your shortcode is now ready to be used!

Step 4 | Use Your Shortcode

To use your shortcode, simply type in the shortcode ([affiliate_disclosure]) into the visual or text editor of a post/page. Just remember to start and end with square brackets and spell the shortcode exactly as created. If you mistype the shortcode, you’ll just end up seeing the brackets and mistyped shortcode rather than your affiliate disclosure.

Step 5 | Style Your Shortcode

Okay, so your shortcode works. But let’s add some styling! When we created the shortcode, you might have noticed that the disclosure text was surrounded with a paragraph with a class of disclosure. The reason for doing so was so that we can easily target your disclosure and style it however you want.

I kept my disclaimer pretty simple and just italicized it, but you can change its color, add a background, or even change the font. There are limitless possibilities.

Go to your theme’s stylesheet (or your theme’s custom CSS field) and paste the following:

.disclosure {
     font-style: italic;
     // Add any other properties here, such as color, background-color, etc
}

Step 6 | Changing Your Affiliate Disclosure

Let’s say down the road the rules for disclosing your links change. Or maybe you want to update the text, add a link, etc. In order to do so, go to Plugins and find your custom plugin. Click on edit below it. Then just edit the text between the paragraph tags and save. Now wherever you used the shortcode, the text will be changed.

And that’s it! You’ve not only created an easy way to insert an affiliate disclosure into your posts, but you’ve created a shortcode and a plugin!


Quickly add an affiliate disclosure to your posts by creating a shortcode. Here’s how:
Click To Tweet


How to Create a Shortcode to Quickly Add Disclosures to Posts was originally posted on Allyssa Barnes.


Viewing all articles
Browse latest Browse all 35

Trending Articles