Greet Your WordPress Visitors With Tailored Messages
Would you like to provide bonus content to those visitors who clicked through from your e-newsletter? Perhaps you’d like to welcome visitors who’ve come from an affiliate site? Or maybe you just want to let subscribers know that you are running a special offer on membership?
There are 101 marketing and promotional opportunities for notifications that can be tailored according to a visitor’s referrer and role.
In this article, I’ll show you how to splice two existing plugins to add conditional global notifications to your WordPress site.
The WordPress Conditional Content plugin is an absolute gem. Through the use of a single shortcode it allows content to be displayed according to certain tests.
For example, this content will only be shown to logged in members with a role of subscriber:
[if role="subscriber"]Upgrade to a full month's membership now for the special price of $1![/if]
Fairly standard stuff and not uncommon.
But what about this?
[if referrer="www.google.com"]Searching for an article? Let us help you find what you need.[/if]
Getting interesting. How about?
[if qs="utm_medium:email"]We'd like to thank you for being a subscriber to our email newsletter. So, here's a free gift[/if]
[if qs="category_name:pyjamas"]Buy a pair of pyjamas now and get 50% off a pair of slippers![/if]
Did I also mention that you can nest those if statements?
[if gs="category_name:pyjamas"] [if2 role="member"] Hooray! Members get 20% off all pyjamas this month! [/if2] [/if]
No doubt now your brain is racing with numerous scenarios where you could put this functionality to use. But there’s a catch.
The plugin assumes that you are going to put the shortcodes into the actual page or post content. This seems a little short-sighted: surely it would be a more powerful feature if it could be applied as a message that appears across the site?
Enter the Global Notification Plugin
This plugin does a simple job very well. It provides a Settings page that allows an administrator to enter a message that will be displayed on all posts and pages and choose the message styling (notify, alert, warning).
Unfortunately, although it allows HTML code to be included in the message, it doesn’t support shortcodes. It also has the CSS embedded in the plugin, which is not optimal.
So, simply installing the two plugins isn’t going to provide the ultimate solution, especially as I’d also like to add a page check to the conditions in the Conditional Content plugin, so I can also control which pages content appears on. For example:
[if page="home"]This will only show on the home page[/if] [if page="post"]This will only show on posts[/if]
Rather than hacking both plugins, I’ve spliced them together to create a new Global Conditional Notifications plugin with the following additional functionality:
- Notification text can include any shortcode including the Conditional Content shortcodes (achieved by applying the the_content filter to the message).
- CSS is specified in a new option on the Settings page (also hooked into the wp_head action to output the CSS)
- New page test added to the [if] shortcode (e.g. page=”home”, page=”post”, also allows for page=”home,post”)
An Interesting Gotcha
When I initially started testing the plugin, I found that the [if] shortcodes were getting altered with extra ‘\’ being placed before any quotes. This is due to the update_options function santizing the option content before storing it in the database.
The fix is remove the ‘\’ when retrieving the message for outputting to a page or for when displaying in the Settings form.
Setting Up An Example Notification
I’m not going to walk through the code, you can download it from its Gist and step through it if you are interested.
What I am going to do is walk through an example notification so that you can see how it works.
First, install the plugin. When it’s installed, click on the new Global Notify option in the Admin interface and you’ll be presented with this form:
We’ll use a nested message so that you can see how flexible the solution is. Enter the following in the notification content:
[if page="home,post"] [if2 role="administrator"]This is a message for admins <strong>only</strong>[/if2] Just testing global post notifications. Here's some <em>HTML</em>? [/if]
This contains two tests: the first is checking to see if the page being displayed is the home page or a post; the nested check (notice it is using if2) only outputs the content for administrators.
You can change the notification type as this really just changes the colour. The CSS will have defaulted to the original styles, again you can leave that as is.
Ensure that Enabled is checked and click on Save.
With the Settings saved, go to the home page of your site. If you are logged in as an administrator (which you probably are) you should see this:
Click on a post and you should still see the same message, but click on a page and the message should disappear.
Pop open a different browser (or log out of the site) and return to the home page and the message should still appear but should not include the message for administrators.
And don’t forget, the conditional content shortcodes will also still work in page and post content.
Combining two plugins has created a new plugin that is arguably greater than the sum of its parts. This was only possible because the base plugins are so well written and perhaps there’s a useful lesson there for GPL licensed code: free isn’t enough on its own to encourage forking.
Have you combined plugins to produce a better end product? Let us know in the comments.