Thord Daniel Hedengren - Smashing WordPress_ Beyond the Blog-Wiley (2014)

(avery) #1

196 PART III • Using Plugins with WordPress


What happens when a plugin relying on shortcodes gets uninstalled? Shortcodes are snippets
of code that output something within the post content. The most common shortcode that
WordPress uses is the [gallery] shortcode, which you’ll see when you include a gallery in
a post and switch to HTML view in the Add New Post screen.

So what happens to shortcodes used by a plugin when that plugin is uninstalled? The short-
code won’t be parsed; hence, it is outputted as regular text. Your [myshortcode] shortcode
will end up printed just like that, in the middle of your post.

It won’t look good.

So although the plugin may not be active, or even present anymore, you still need to
maintain some sort of backward compatibility to it. This means that you should offer some
sort of solution for the user to make sure the posts aren’t littered with unparsed shortcode.
One way would be to run a SQL query and simply remove all the shortcode automatically, but
that may be a bit drastic, and what happens if something breaks during this process? It may
destroy the database. Or, more likely, how do you know that there isn’t a human error within
the post that for some reason causes the removed shortcode query to cut too much?

How you choose to handle redundant shortcode depends on the kind of usage you have
introduced in your plugin. This is covered more in depth in Chapter 8.

Obviously, not all types of plugins will need this treatment. For example, plugins utilizing
widgets will just stop displaying the widget when removed.

BUILDING THE ADDAFF PLUGIN


Why not put what you’ve learned so far to the test by building a plugin? The AddAff plugin is
a simple little thing for sites where affiliate links are of interest. I use it for projects that have
only one product per post, so I’ll stick with that functionality for now.

This is what the AddAff plugin will do when you’re done with it:

◾ Each post will get an affiliate link box, where the product URL and a suitable title for the
link will be added.
◾ There will be a settings page where it will be possible to add a string that gets included
before the product link, and after it as well, as a lot of affiliate providers work with this
sort of setup.
◾ The affiliate link will be added after the_content(), but only if both the affiliate link
title and URL are filled in.

Let’s get to it then, starting with the settings page.

THE ADDAFF SETTINGS PAGE
The AddAff Settings page isn’t a complicated affair. The only thing you need to do is save two
strings, which you will use to build the affiliate link when you’re outputting the link.
Free download pdf