Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2006-05-16 13:55:56

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,462
Website GitHub

smd_random_banner: Random banner image for advertising

Bit of a niche plugin this, probably appealing more to people who design ‘corporate’ sites. It allows you to offer up random banners (images and now files/movies) from particular categories that are sensitive to the current document being viewed.

As long as you organise your site well and have a good naming convention for your banners, you’ll be able to use this plugin to advertise “other products and services” to customers without them seeing an advert for “Widget A” while they’re viewing a customer brochure about the same “Widget A”.

Conversely you can use it to pick from a group of ‘wanted’ images to display on a page, using the current article as the basis for determining which images to show.

The plugin also offers the option of having the banner clickable to take you to a page about that product/service, with referrer tracking showing up in your logs so you can see which banners are most effective.

Download smd_random_banner | Requires smd_lib

<<< IMPORTANT: this post is now out-of-date. See the post v0.4 for the new functionality >>>

[ Note: this first post was constantly updated to reflect the latest plugin version in the v0.1x range. The comments below this may seem a bit strange because they refer to features that look like they’ve been here all along! ]

Simple usage

Since it’s shamelessly based on dru_random_image (thanks!), in its base form the plugin functions exactly as dru_random_image. So:

<txp:smd_random_banner />

chooses a random image from all images uploaded via textpattern’s image interface.

<txp:smd_random_banner category="banners" />

chooses a random image from all images in the ‘banners’ category. If at any point, particular options result in matching no images, the plugin will return nothing. This is so you can use plugins such as Coke Harrington’s excellent chh_if_data to detect this and branch to a “default” image or something.

Alternatively, if you always want a picture to be displayed no matter what, specify the always_display="1" parameter. In the event of no matches being found, the plugin will try again just from the given category (if supplied) or from the whole pool of images otherwise.

If for some reason you want the image thumbnail to be displayed instead of the full image, add the thumb="1" option.

Adjusting randomness

Now comes the fun part. What if you only want images to appear that have particular words in their filename? (or Alt Text – see option use_alt below) For example, if you had a zoo website and only wanted pictures of lions to appear on the Lions page.

Or, conversely, what if you don’t want a banner to appear that advertises the product your customer is already looking at? It looks a bit strange to have an advert for “productA” on a page about “productA”! That’s where the next three options come in: match_text, avoid_text and look_in.

These compare the filename (or optionally alt text) of your banner image to some given text and if it matches, that particular image will either be displayed (match_text) or not displayed (avoid_text). Some examples:

<txp:smd_random_banner category="banners" match_text="penguin" />

That will choose a random image from all images in the ‘banners’ category that have the word “penguin” in their title.

<txp:smd_random_banner category="banners" avoid_text="product1" />

That will choose a random image from all images in the ‘banners’ category but ignores any image filenames that contain the text ‘product1’. This is useful if you had more than one banner per product like:

  • banner_product1_A.jpg
  • banner_product1_B.jpg
  • banner_product2_A.jpg

as it would exclude all of product1’s images. It can also take a comma-separated list of words to ignore, e.g. match_text="llama,penguin" or avoid_text="product1,product2,product8" Note: no spaces around the commas!

These options can be used in tandem to create some crazy brain-bending combinations.

What about alt text?

If you’ve already loaded all your images and the prospect of going through them to create a naming convention is daunting, try adding the use_alt="1" option (‘1’ is a convenience, it could be any text really). Then, instead of looking in the image filename, it will look in the image’s ‘Alt Text’ for words to match/exclude.

Getting wild

Now things get even more warped. Since you often don’t know which product page your customers will look at you can use the special wildcard “?” as an option in your list of words to match or avoid. What this does is look at the current section name of the page being displayed and use that as one of your ignore words. So if a person was on page www.megazoo.com/lions/the-humble-lion and the plugin was called thus:

<txp:smd_random_banner category="banners" match_text="?" />

any image name in the “banners” category that had the text “lions” in it would be fair game for appearing on the page. Again, you can do the reverse by using avoid_text:

<txp:smd_random_banner category="banners" avoid_text="?" />

This means that if you arrange your site such that each product has a separate section, you can then name your banner images (or alt text) in such a way that certain images won’t be displayed when a user visits a particular section.

You can use the “?” in addition to a list of words if you like, and mix both match_text and avoid_text until your brain explodes.

Looking around

By default, “?” means the plugin will look at the name of the current section for a match. If you want to change that, you have the choice of giving the look_in option. This forces the plugin to look for a match somewhere else in the URL.

The current options available are “category”, “section” and “title”; the title in this case is the name of the article itself. For example:

<txp:smd_random_banner category="banners" match_text="?" look_in="title" />

would look at just the article’s title and compare it to the image names. In our example URL above, if there was an image in the “banners” category which had the-humble-lion in its name, it would appear on any page that had a URL with a title containing the-humble-lion. If you instead used:

<txp:smd_random_banner category="banners" match_text="?" look_in="title,section" />

it would include any image names that had either “lions” or “the-humble-lion” in them. Note that if you specify a look_in parameter you must specify every place to look in; it only assumes “section” if you miss out the look_in parameter entirely.

Also be aware that if you specify “title” as a place to look_in and the person is surfing a page that contains a list of articles only, the title parameter will be ignored.

Linking the image

The final piece of the puzzle is to direct people to a web link when they click on the image. Since the image is going to be a banner and you’re unlikely to want to put a caption on it (I hope!), I figured the caption was a good place to store a web link. Yes, I know it’s a bit of a hack but there isn’t really anywhere else to put it right now. When you upload your image, put the desired destination URL in the ‘caption’ field and use the linkcaption="1" option to pull it from there and make your banner clickable.

<txp:smd_random_banner category="banners" match_text="?" avoid_text="test" look_in="section,title" linkcaption="1" />

If the URL begins with http://, it will be used verbatim. If not, it will be assumed to be a ‘local’ link and will have <code><txp:site_url /></code> prepended to it.

If you already have a tonne of captions stored and don’t want to overwrite them, you can use linkalt="1" option to allow the link be made from the alt text instead.

Further, if you always want your link to point to a particular section of your site or a specific external site, you can save yourself some typing. Instead of hard-coding the section or site name in the alt/caption field, you can specify the linkprefix option. Whatever text you put in that option will be prefixed to the alt/caption text. Note that if you have used a ‘full’ URL in the alt/caption field (i.e. one beginning with http://) the linkprefix option will be ignored for that image. Also note that if you specify both linkalt and linkcaption options, the linkcaption will take precedence.

Track it

If you use linkcaption="1" or linkalt="1", the plugin will automatically add a parameter to the link so you can track which banners were clicked in your textpattern logs. Since this is a bit like referrer tracking, the option to control it is named referer – and is mis-spelled exactly the same way as the HTTP spec was all those years ago :-) referer takes a text argument to use as a prefix. An example:

<txp:smd_random_banner category="banners" match_text="?,goldfish" referer="trackid" />

This will generate a url with ?trackid=image_number added to it, where image_number is the image id of the picture in question.

If you don’t specify the referer, all linkable images will have the code ?bid=image_number added (for ‘Banner ID’). If you wish to disable the option, add referer="".

And there’s more

Want more than one image sent out at once? Add the limit parameter. Give it a number and it’ll be happy. Defaults to 1 image. If you wish to add a class to each img tag, use the imgclass option.

Troubleshooting

There is a chance that, under txp v4.0.4, you will get the same image back each time. This could be because of your options (check match_text and category carefully) but may also be because the image is being cached. If the server thinks the current page request is identical to the last, it instructs the browser to show the same page as last time.

If this is causing a problem, try setting the value of “Send Last-Modified header” to “no” under admin->preferences->advanced. It may increase server load because every page and image will always be sent from the server, but at least you’ll get a random image each time :-)

Change log

Most old versions are available on my site if you want to rollback a version (shouldn’t ever have to but hey). Just change the rev number in the link below. Here’s the list of changes so you can see roughly what’s in each version:

  • v0.1 Initial public release
  • v0.11 Added PFX to query (thanks mercury)
  • v0.12 Fixed help to include trailing slashes (thanks vittorio)
  • v0.13 Unreleased. Added look_in=category and referer
  • v0.14 Added match_text and use_alt (thanks lukepermsn)
  • v0.15 Added always_display and limit (thanks lukepermsn)
  • v0.15a Bugfix for linkcaption (thanks mercury)
  • v0.16 Tightened image() params for txp v4.0.4 compliance
  • v0.17 Added thumb, linkprefix and linkalt options (plus a referer/limit bugfix)
  • v0.17a Bugfix for referer. Now url-aware and handles ?/& correctly (thanks Papuass)
  • v0.18 Added imgclass option (thanks DigitalRealm)
  • v0.2 Major overhaul. Added subcategory support (thanks lukepermsn and santheo). Uses smd_lib. Enhanced match_text/avoid_text so look_in is no longer needed; dropped use_alt and added compare_to; replaced linkcaption/linkalt with link_in; added id_prefix; linkprefix is now link_prefix
  • v0.21 Added MLP support
  • v0.22 Added raw_url option
  • v0.23 Against better judgement, added target option (thanks jstubbs, cdtavijit)
  • v0.3 Added support for files as well as images – finally, video/swf banners are a reality!
  • v0.3a Fixed empty categories defaulting to all images
  • v0.31 Extended always_display. Added nv_delim. Fixed: define() warning
  • v0.32 Added no_plug_text and global_params
  • v0.33 Uses sdm_lib v0.3. Added swfobject support in file mode, and overridable mime types
  • v0.34 Added static image support to files mode!
  • v0.4 Refactored code to take advantage of new smd_lib functionality (subcats, category and matching) ; added id and compare_to:id ; changed imgclass to class ; changed subcats to use levels ; added display_url (thanks Andrew Blevins) ; can now use image name as link_in ; fixed swfobject / Flashvars bugs in file mode ; removed id_prefix
  • v0.41 Fixed MLP string bug
  • v0.42 Added file title as a possible location ; added row_delim ; added onclick handler to file mode (thanks renobird) ; added SMD_UNIQUE (thanks Scott Girvan)

Comments and enhancements/bugs/whatever always welcome. No doubt this can be twisted into many different applications, none of which I’ve yet considered, but hopefully it won’t fall over when shoehorned into an application other than the one for which it was designed.

Last edited by Bloke (2010-07-25 14:59:22)


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#2 2006-05-16 22:19:37

soulship
Member
From: Always Sunny Charleston
Registered: 2004-04-30
Posts: 669
Website

Re: smd_random_banner: Random banner image for advertising

Looks like it has some other possible applications as well. Thanks!

Offline

#3 2006-05-20 01:38:32

mercury
Member
Registered: 2006-05-12
Posts: 26

Re: smd_random_banner: Random banner image for advertising

very good! thanks!

suggest changing <code>txp_image</code> to <code>”.PFX.“txp_image</code>

Offline

#4 2006-05-20 09:03:03

-P-
Member
From: Finland
Registered: 2005-09-10
Posts: 211

Re: smd_random_banner: Random banner image for advertising

Does this plugin support usage of .swf files also? That would make it even more useful!

Offline

#5 2006-05-24 07:14:35

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,462
Website GitHub

Re: smd_random_banner: Random banner image for advertising

P wrote:

Does this plugin support usage of .swf files also? That would make it even more useful!

My guess is no. I’m pretty sure .swf files need to be inserted into HTML using the <object> tag and the textpattern images use the <img> tag. Of course if someone could coerce textpattern’s image handler into detecting any type of object thrown at it…

@mercury: thanks for the tip about PFX. A dumbass noob mistake on my part. Fixed in v0.11. See initial post for download.


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#6 2006-08-31 10:12:53

vittorio
Member
From: Roma caput mundi
Registered: 2006-02-01
Posts: 122
Website

Re: smd_random_banner: Random banner image for advertising

Hi Bloke,
sorry, but i can’t get this plugin working. When i put the tag in page, it makes the layout go fuzzy or simply doesn’t show anything. And the linkcaption attribute doesn’t work too, as the banners – when they appear – are never clickable. Don’t know what to do, any suggestions?

Thanks a lot

edit: Problem solved. Didn’t notice that the example tags provided in the help miss the ending “/”

Last edited by vittorio (2006-09-04 08:13:53)


Happily served with Txp: Strampelli.net, Brain Essence, NicolaTranfaglia.com

Offline

#7 2006-09-08 21:29:24

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,462
Website GitHub

Re: smd_random_banner: Random banner image for advertising

vittorio wrote:

edit: Problem solved. Didn’t notice that the example tags provided in the help miss the ending “/”

D’oh! Sorry I didn’t reply sooner – been on holiday.

What a chump I am :$ I’ve fixed the original post and the help file to correct this. Thanks for pointing it out and sorry it caused you a headache for a bit.


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#8 2006-09-17 19:50:50

lukepermsn
Member
From: london
Registered: 2004-07-15
Posts: 57
Website

Re: smd_random_banner: Random banner image for advertising

Nice plug-in! a couple of thoughts:

It will be a pain for me to change the titles of all the images in my site database (about 500!) – therefore it would be great if ‘look_in’ worked for text in the ‘Alt text’ part of the image options…

It would also be nice if this worked from parent categories – i.e. randomly display images in child categories of a selected parent…

L.

Last edited by lukepermsn (2006-09-17 21:55:50)


“Absorb what is useful, Discard what is not, Add what is uniquely your own” – Bruce Lee

Offline

#9 2006-09-18 19:36:31

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,462
Website GitHub

Re: smd_random_banner: Random banner image for advertising

lukepermsn wrote:

It will be a pain for me to change the titles of all the images in my site database (about 500!) – therefore it would be great if ‘look_in’ worked for text in the ‘Alt text’ part of the image options…

Hmm, I think I get what you mean; what are you trying to use it for? I’m not sure I’ve explained it well enough up there, so any ideas on how to rename the options or change the concept to make it easier to understand greatly appreciated!

In the current v0.1x, look_in tells the plugin to “Look In the specified part(s) of the current page URL for words that contain text in avoid_text. If it finds such a word and it also matches part of a filename of an image, exclude that image from the banner rotation.”

[ Side note: since look_in simply tells the plugin where to look – either the ‘section’ or the ‘title’ of the URL – and those are the only two options available right now, it probably won’t work in messy URL mode: haven’t tested it though ]

What I guess you mean is that you don’t want to rename your image filenames to have a common naming strategy because with 500 that would take forever. So you’d like look_in to match the URL to words in your Alt Text instead? (btw, you have 500 potential banner ads? blimey…) I assume therefore that your alt text has some meaningful pattern to it that can be used to ignore certain images based on the page URL?

If I’ve misunderstood what you want to use it for, I apologise. Give me an example or something and I’ll see if i can get my head round it, then maybe I can make the plugin more useful for ya.

It would also be nice if this worked from parent categories – i.e. randomly display images in child categories of a selected parent…

Now that I like! Why didn’t I think of that? I’ll see if I can hack the plugin to handle subcats in some manner. That might make some of the more awkward options redundant; maybe if I added an option to somehow match look_in to subcats? I’ll see how it pans out – thanks for the idea.

P.S. nice site – and DHTML Lemmings is way cool. Takes me back to my student days of skipping lectures / staying up all night to play my Amiga, with the rest of my housemates figuring out strategies to beat a level :-)

Last edited by Bloke (2006-09-18 19:37:23)


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#10 2006-09-18 22:00:37

lukepermsn
Member
From: london
Registered: 2004-07-15
Posts: 57
Website

Re: smd_random_banner: Random banner image for advertising

Hi bloke

Thanks for the response.

Actually, I think you explained yourself pretty well, I just misunderstood slightly. I’m putting together a travel site, for which i have already uploaded approx 500 images. I want the images to cycle around the relevant sections, so for instance images under category ‘Africa’ to appear on pages that talked about ‘Africa’ – which I don’t think your plug-in will do for me now, as I now realise it works in the opposite of this!

Yes, what would be nice is if there was another attribute, “use_alt” for instance, to make “avoid_text” cross reference with keywords in the image’s ‘alt text’ field instead of the filename. i.e. I have a bunch of images that are labeled ‘Africa Rafting School’ in the alt text, and another with just ‘Africa Rafting’, and I want images with ‘School’ in the alt text to display on some articles, but not on others… so something like:

<code><txp:smd_random_banner use_alt=“y” avoid_text=“School” /></code>

Would make any random image appear from the entire library, except those with keyword ‘School’ in the alt text field.

I could then use:

<code><txp:smd_random_banner use_alt=“y” avoid_text=“School” look_in=“section” /></code>

To prevent images with ‘School’ in their alt text appear in the School section of the site, although I probably wouldn’t want to do that! I would most likely use the first example above with some conditional section tags to prevent random images from appearing in sections they don’t belong to.

However, if the plug-in could be twisted to match words in the URL instead of avoiding them, then an application such as:

<code><txp:smd_random_banner use_alt=“y” match_text=“School” look_in=“title, section” /></code>

Would enable any image to display from the library, that had the word ‘School’ in the alt text, to display in articles that also have ‘School’ in the section or title. Or for further cleverness:

<code><txp:smd_random_banner use_alt=“y” match_text=”?” look_in=“title, section, category” /></code>

Could potentially display images that have ‘School’, ‘Africa’ and/or ‘Rafting’ in the alt text, depending on what the article title, currently viewed section or category are. And there would be a really powerful functionality IMHO!

Otherwise a very smart plug-in and yes, the ability to use parent categories in ‘category=’ to have random images pulled from all the subcats also would be incredibly useful, i.e. ‘Africa’ is a parent category, which then has sub categories of ‘Kenya’, ‘Namibia’, ‘Ethiopia’, ‘Swaziland’ etc… so some of the time I may want to match for ‘Kenya’ – but other times any image from ‘Africa’ will do… from what I understand, putting a parent category in the tag attribute won’t pull images from all the subcats also…

I hope you the above musings useful. Glad you like the site :-)

L.

Last edited by lukepermsn (2006-09-18 23:52:32)


“Absorb what is useful, Discard what is not, Add what is uniquely your own” – Bruce Lee

Offline

#11 2006-09-18 23:24:30

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,462
Website GitHub

Re: smd_random_banner: Random banner image for advertising

lukepermsn wrote:

I want the images to cycle around the relevant sections, so for instance images under category ‘Africa’ to appear on pages that talked about ‘Africa’

That is an excellent use of this sort of plugin and ties in neatly with another idea I’ve been harbouring for a while of some kind of related_images system. This plugin could be a halfway house (or maybe a 90%-of-the-way-there house) for this idea.

Just had a squint through my (not very well commented, cough) code and it looks like adding the ‘positive’ match_text attribute won’t strain too many of my synapses.

the ability to use parent categories in ‘category=’ and have random images pulled from all the subcats also would be incredibly useful

I concur.

from what I understand, putting a parent category in the tag attribute won’t pull images from all the subcats also…

Alas no. At least not yet. Think I’ve figured a relatively simple technique for picking up the subcats. Next problem is how to make that option available for use.

For example, do I assume that if a parent category is given, all matching/non-excluded images in all subcats are fair game? Or should it be an opt-in switch (use_subcats="1")? Or an opt-out switch (the slightly unintuitive imo ignore_subcats="1")?

Or perhaps the ability to specify subcats to include or exclude? E.g
category="Kenya, Swaziland"

would only pick from 2 subcats;

or
category="Africa, !Ethiopia"

would pick from all subcats of Africa except Ethiopia?

Or perhaps adding an option similar to look_in that matches/excludes subcats based on a portion of the URL? That would give the ability to keep your images logically organised: say, a Ford category might have subcats of ‘Mondeo’ and ‘Puma’. If category_in="section" was used, a page in the Puma section of the site would only have images from the Puma subcat available to it.

Not even sure if that’s possible with my (lack of) coding skills. Not really thought through the implications, it’s just off-the-top-of-the-head stuff. If you have any ideas about how this sort of thing should work, or ways to word the attributes so it doesn’t become a huge confuse-a-thon, I’m all, umm, eyes.

what would be nice is if there was another attribute, “use_alt” for instance, to make “avoid_text” cross reference with keywords in the image’s ‘alt text’ field instead of the filename… Or for further cleverness:
<code><txp:smd_random_banner use_alt=“y” match_text=”?” look_in=“title, section, category” /></code>

You’re right, that would be way cool. Shouldn’t be tough for me to add that (he says…) and I don’t see any reason why it couldn’t even combine match_text and avoid_text to make some really confusing constructs :-) I’ve already added the ability for look_in to use ‘category’ in my latest dev version, btw.

Thanks for your thoughts and ideas. The gauntlet has well and truly been thrown at my feet. Let me sleep on it and I’ll be back with v0.2 as soon as I can.


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#12 2006-09-18 23:38:11

lukepermsn
Member
From: london
Registered: 2004-07-15
Posts: 57
Website

Re: smd_random_banner: Random banner image for advertising

Bloke wrote:

For example, do I assume that if a parent category is given, all matching/non-excluded images in all subcats are fair game? Or should it be an opt-in switch (use_subcats="1")? Or an opt-out switch (the slightly unintuitive imo ignore_subcats="1")?

I see what you’re saying there. Potentially one might want to assign the parent category but not have images display from subcats.

< snip >

Wow – you went way above what I was thinking there. I was just thinking it would be nice to display a random image from all subcats by assigning the parent category. I see what you mean by certain ramifications though… i.e. the ability to exclude some subcats would be cool – i can see how that could begin to get complicated though.

Shouldn’t be tough for me to add that (he says…) and I don’t see any reason why it couldn’t even combine match_text and avoid_text to make some really confusing constructs :-) I’ve already added the ability for look_in to use ‘category’ in my latest dev version, btw.

haha – now that would be fun :D

Thanks for your thoughts and ideas. The gauntlet has well and truly been thrown at my feet. Let me sleep on it and I’ll be back with v0.2 as soon as I can.

you’re welcome. i look forward to seeing what you come up with!

L.

Last edited by lukepermsn (2006-09-19 00:01:55)


“Absorb what is useful, Discard what is not, Add what is uniquely your own” – Bruce Lee

Offline

Board footer

Powered by FluxBB