If server performance is important, you do not want to use AJAX through WordPress. Each click will load a full version of WordPress, and it’s very likely your server resource usage will skyrocket.
How much text are we talking about here? If it’s less than a couple thousand bytes, I’d format the entries into a JSON-encoded string and add it to the page as a javascript variable. You could then use JSON.parse() on the string to create a structure that can be used when a user clicks a button. This requires no AJAX whatsoever, and your users would need to look at the page source to see a (hard-to-read) list of entries.
How much text are we talking about here?
At least 120 separate lines of 1 to 3 small words, more to be added later on.
I wouldn’t mind having it as a variable encoded. It’s simply to avoid people cheating and seeing the entire list by seeing source code.
You dont need the WordPress core to use AJAX. 🙂 At leaat on the PHP side.
For something that’s as simple as your need, this file woudl be more than enough:
<?php
$phrases = array (
'Bus',
'Plane',
'Car',
'Flying Spaghetti Monster'
);
$index = mt_rand (0, count ($phrases) - 1);
echo $phrases [$index];
die ();
You can add as many entires into the $phrases
array as you need and it will still work. The only reason for using the WordPress back-end for this would be if you need any more functions or authentication (there’s no security issues with this).
But, if you want the least overhead, do what DionDesigns suggested and have them all in the page. Only someone that really wants to find out will go looking for them, and if your example is what you’re looking for I can’t see how someone knowing this sort of list would be an issue.
Another option would be to store the strings as a file on the filesystem, and then use AJAX to load the file when required. The entries would no longer be in the page source, this type of AJAX doen’t require a PHP file (which means it would have very little impact on server performance), and it gives you more flexibility with adding/deleting entries since they would be external to your WordPress installation.
If I were doing this for myself or a client, I would write a plugin that controls the addition/deletion of entries in the file. If you don’t know how to write plugins, then the best solution would be to store the entries in a text file in a pre-defined format, then use javascript to parse the text file into the structure you need.
Thanks for the replies. The current setup right now is some random plugin in a frame where I refresh the frames content onclick. I’ll test out the various ways tomorrow where I have time to. Again, thanks for taking time to help me out 🙂
Right. Being really new and all with this php and coding in general, I’m still “cheating” a bit with plugins.
catacaustic, I went with your solution, that array one, and stuffed it all into a shortcode. Posting php directly on a page didn’t seem to please WordPress.
Thanks alot for the help, both of you. 🙂