Just to learn about plugins, I’m taking some PHP code that used on my pMachine-based site and turing it into an EE plugin. The code reads a file containing quotes—one per line—and picks one at random, displaying it.
I want to pass the local path to the quotes file as a parameter. The pass seems to work, I can print out the parameter and it is, in fact, the path to the file. But, I can’t seem to get that path assigned and the file opened.
Here is the complete code (without the php tags):
$plugin_info = array(
'pi_name' => 'Random Quote',
'pi_version' => '1.0',
'pi_author' => 'Rick Robinson',
'pi_author_url' => 'http://blue.homeunix.net',
'pi_description' => 'Returns a random quote from a text file.',
'pi_usage' => random_quote::usage()
);
class random_quote
{
var $return_data = "";
function random_quote()
{
global $TMPL;
$parameter = $TMPL->fetch_param('path');
$delim = "\n";
$quotefile = "/Library/WebServer/Documents/blog/quotes.txt";
//$quotefile = $parameter;
$fp = fopen($quotefile, "r");
$contents = fread($fp, filesize($quotefile));
$quote_arr = explode($delim,$contents);
fclose($fp);
// generate random quote index
$quote_index = (mt_rand(1, sizeof($quote_arr)) - 1);
// get quote at $quote_index and return it
$thequote = $quote_arr[$quote_index];
$this->return_data = $thequote;
//$this->return_data= $parameter;
}
function usage()
{
ob_start();
?>
The random_quote plugin returns a random quote pulled from a text file named quotes.txt which is passed as the path parameter. The text file should have one quote per line.
{exp:random_quote path='<absolute path to quote file>'}
<?php
$buffer = ob_get_contents();
ob_end_clean();
return $buffer;
}
}
You can see that I’ve got two lines that assign a value to $quotefile as well as two lines that provide the return value. I’ve done this to debug only. This particular version “hard codes” the path to the file, and it works fine. However, if I assign the path to $quotefile via the $parameter variable, it does not work…
I know this is more a php question than plugin question, but maybe someone has a quick answer?
Rick R
