Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
smd_random_text: Pull random items from the TXP environment
Since I’ve done it with images and other stuff, I figured I might as well tackle text as well.
Written with a cursory nod of appreciation to Drew McLellan and Manuel Ebert who have both made random text plugins before, this one takes it a step further. You can choose to grab your text from a pool given in a fixed string, a TXP field, a URL field, a TXP variable, a database or a file. And you’re not limited to just grabbing one item.
Each item you choose is optionally passed to a form/container where you can insert them into markup using the now-customary {replacement tag syntax}
. Check the examples in the help for some gentle prods and ideas. I’m sure there are all sorts of wacky things you can randomize, like pulling an entry or two out of a sed_pcf.
There’s plenty of scope for extensions to this one if anyone has any ideas. Possible things to do:
- Matching/excluding to pull out subsets of items
- Define more than one list in a single file: like an old skool .ini file with interior sections
- Fix the code if it proves that writing a plugin in 1 hour at 1:30am is a bad idea
- Anything else?
Here is the latest version for your comfort and enjoyment: download
Any ideas or problems, you know where I live.
Revision history
————————
All available versions and changes are listed here. Each entry indexes the relevant post(s) in this thread to learn about the features.
- 10 Oct 08 | v0.1 | Initial release
- 17 Mar 09 | v0.11 | Added escape and trim attributes
- 26 Jul 09 | v0.12 | Added item_num and shuffle and
{smd_rnd_txt_chosen_item}
- 28 Feb 11 | 0.13 | Permitted multiple database columns to be selected in one plugin call (thanks giampablo) ; added
var_prefix
- 03 May 11 | 0.14 | Added file_delim_type and permitted regexes in
file_delim
(thanks MyOtheHedgeFox)
Last edited by Bloke (2011-05-03 00:07: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
Re: smd_random_text: Pull random items from the TXP environment
Is anybody else having problems with this plugin on Txp 4.07. It stopped working after the upgrade and I don’t if it’s something else I did, or the upgrade itself.
Thank you.
Offline
Re: smd_random_text: Pull random items from the TXP environment
Ops, I’ve installed it on a 4.0.7 but haven’t had time to test it yet! Also would like to know if it works!
Offline
Re: smd_random_text: Pull random items from the TXP environment
Liiint wrote:
Is anybody else having problems with this plugin on Txp 4.07.
Congratulations on being the first person (besides myself) to use it!
But I wouldn’t have thought it was the plugin: it’s so dim and simple it should run on any TXP version! It is working for me under 4.0.7 but there are so many ways to use it, anything’s possible. Would you mind posting the tag you are using that is causing problems and any associated information, and I’ll see if I can work out if the plugin’s to blame. Also, when you say “stopped working”, are there any errors if you put your site in debugging mode?
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
Re: smd_random_text: Pull random items from the TXP environment
Okay. It is NOT the plugin. It’s working again. During our backup and restore process during said upgrade, a certain file went missing. It’s back now and the world is a better place.
Offline
#6 2009-01-27 04:44:56
- hamsteralliance
- Member
- Registered: 2007-12-13
- Posts: 10
Re: smd_random_text: Pull random items from the TXP environment
Make that two people. ;)
Here’s my newbish question:
I plan on adding a quote of the day type thing, but I’m not really grasping how to get the plugin to pull the quotes from a textpattern article, preferably one that’s not live and set to hidden or whatever other setting will stop the full list of quotes from appearing on my site. :P
Any help would be greatly appreciated!
Offline
Re: smd_random_text: Pull random items from the TXP environment
hamsteralliance wrote:
I plan on adding a quote of the day type thing, but I’m not really grasping how to get the plugin to pull the quotes from a textpattern article
Hi, if you want to use an article to hold the quotes there are a couple of ways to approach it. I believe if you “hide” the article TXP won’t be able to see it so you’ll need to make it visible. But we can hide it in other ways.
Method 1:
- Make a Section to hold your quotes article and set it to not be searched/seen on front page/etc
- Make an Article and put the quotes in, say, Excerpt for now like this:
"All that we see or seem is but a dream within a dream."
-- Edgar Allan Poe
|
"Assassination is the extreme form of censorship."
-- George Bernard Shaw
|
...
- Put your smd_random_text tag in the body so it will read the quotes from its own excerpt field:
<txp:smd_random_text type="field" source="excerpt" />
- Assign the Article to your new Section and save it
- To output your quote you can simply use
<txp:article_custom section="quotes" limit="1" />
(or perhaps useid="ID_of_article"
). You may have to specify a different form too — one that only outputs<txp:body />
, but that depends on the layout of your default Form
Method 2 (slightly easier):
If you are going to the trouble of creating a form you might as well do it this way instead:
- Create the same Section as above
- Create a Form called “quotes” and put this in it:
<txp:smd_random_text type="field" source="body" />
- Create your Article and put your quotes — in the same format as in Method 1 — in the Body field
- Assign it to your Section
- Under Advanced options, set Override form to
quotes
, and save the article - Wherever you want the quote to appear, use a
<txp:article_custom />
tag as above with the ID of the Quotes article
Either of those methods (or a combination of forms/containers depending on your preferred way of working in TXP) will do the job. As long as you never link to the “hidden” section, nobody will be any wiser that it exists :-)
Does that help, or at least give you some ideas about how to approach it?
Last edited by Bloke (2009-01-27 10:15:30)
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 2009-01-27 10:39:04
- hamsteralliance
- Member
- Registered: 2007-12-13
- Posts: 10
Re: smd_random_text: Pull random items from the TXP environment
Sounds good to me!
Now I just gotta figure out how to get it in the spot I want it in. Thanks! :D
Offline
#9 2009-01-27 11:17:03
- hamsteralliance
- Member
- Registered: 2007-12-13
- Posts: 10
Re: smd_random_text: Pull random items from the TXP environment
I’ve actually tried it out now and it’s not working as well as planned. :)
I created the section, the form and the article with a few lines of text in it.
Wherever I use the article_custom code you have up there it ends up posting the entire article and attaching the author name and timestamp to it too.
like this: http://www.hamsteralliance.com/upload/donotwant.gif
Any ideas? :D
Last edited by hamsteralliance (2009-01-27 11:17:20)
Offline
Re: smd_random_text: Pull random items from the TXP environment
hamsteralliance wrote:
Wherever I use the article_custom code you have up there it ends up posting the entire article and attaching the author name and timestamp to it too.
That’s because it’s rendering the article using the default
Form, which tells it to display a title, the body, the author, the timestamp, the categories, etc etc etc. What you need is a form that displays a single thing: the body. In fact, if you’re using TXP 4.0.7 or higher, you can take advantage of the fact that the article tags can be containers. Try this:
<txp:article_custom id="blaaah">
<txp:body />
</txp:article_custom>
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
Re: smd_random_text: Pull random items from the TXP environment
Slight update to add trim
and escape
attributes. 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
Re: smd_random_text: Pull random items from the TXP environment
Have good idea. I want to use this plugin to output different “read more” texts: “read more about this article”, “try to get this”, “some facts about”.. and so on..
But bad thing is that every time page loads this text will change. And i want to make this random text to be stable :) So, i offer to add some attr
, named, for example, unqizer
:) And the example of using:
<txp:smd_random_text uniqizer='<txp:article_id' />
In this case, every article will have uniq and stable random text :)
How to make this? May be you can get md5() of uniqizer and then convert it in number of strings to offset in array of random strings.
Providing help in hacking ATM! Come to courses and don’t forget to bring us notebook and hammer! What for notebook? What a kind of hacker you are without notebok?
Offline