→ Back to Plugin Development index
Some random tips and tricks for developing plugins
Inside the doSkinvar function:
global $blog; $currentBlogid = $blog->getID();
will give you the actual blog that is parsed inside the skin. Helpfull for sites with multiple blogs and you only need items / comments from 1 blog
Instead of using $_POST of $HTTP_POST_VARS to get request variables, it's suggested to use one of the following methods, making sure you get the same data under different PHP configurations:
$var = getVar('name'); // $_GET $var = postVar('name'); // $_POST $var = cookieVar('name'); // $_COOKIES $var = serverVar('name'); // $_SERVER $var = requestVar('name'); // $_REQUEST (=GET/POST or COOKIE) $var = intPostVar('name'); // same as postVar, but parses data into an integer (intval) $var = intGetVar('name'); // =intval(getVar(..)) $var = intRequestVar('name'); $var = intCookieVar('name');
All of these methods make sure that no magic quotes are present (the magic_quotes_gpc PHP option automagically replaces ' by \', when enabled), and work on all PHP versions on which Nucleus runs (4.0.6 - …)
When you need to generate URLs for certain pages, you can create an URL using one of the following global functions:
createItemLink($itemid, $params); createMemberLink($memberid, $params); createCategoryLink($catid, $params); createArchiveListLink($blogid, $params); createArchiveLink($blogid, $archive, params); createBlogLink($blogurl, $params); createBlogidLink($blogid, $params);
This way, the URL mode (normal/fancy) will be respected.
The $params attribute is optional, and is an associative array:
$params = array('catid' => 5, 'highlight' => 'foo'); echo createItemLink($itemid, $params);
results in: fancyurl:
http://www.example.org/item/1234/catid/5/highlight/foo
query string (fancyurl off)
http://www.example.org/index.php?itemid=1234&catid=5&highlight=foo
Instead of
$blog = new BLOG($blogid);
try to use
global $manager; $blog =& $manager->getBlog($blogid);
or
$blog =& $manager->getBlog(getBlogIDFromName('secondBlog'));
This way, only one BLOG object will be created during the request, and the info for the weblog will be read out of the database only one single time.
Instead of
$item = ITEM::getItem(1234);
use
$item =& $manager->getItem(1234);
Same reasoning as with getBlog After that you have your item in an array. So you can get the item title by calling $item['title'] instead of $item→title;
global $CONF; $CONF['IndexURL'] . 'action.php.....'
global $currentSkinName; contains the name of the skin used.
:: plugins ::
$catid = $blog->getSelectedCategory();
In function install() create this option:
$this->createOption('cleardb', 'Clear database on uninstall?', 'yesno', 'no');
Also in function install() if you create any tables with sql, instead of just CREATE TABLE use CREATE TABLE IF NOT EXISTS so that kept tables won't cause errors or be overwritten.
Now, in function uninstall() place this if statement:
if($this->getOption('cleardb') == "yes") { sql_query( 'DROP TABLE IF EXISTS ' . sql_table($your_table_name) ); }
When updating to a new version of an already known plugin, normaly you would loose the option values of already known options by the plugin.
With this trick, that will be history. There are 4 things you need to do:
function updateOption($option,$description,$type,$default) { //Update an option. Basicly it saves the value of the option to update, then deletes the options and then creates the option with the saved value. The option will then be at the bottem of the option list in the 'edit option' page of the plugin. if ($this->getOption($option)=="") { $default_val=$default; ACTIONLOG::ADD(INFO,'{NP_PHPList} option :'.$option.' created.'); } else { $default_val=$this->getOption($option); ACTIONLOG::ADD(INFO,'{NP_PHPList} option :'.$option.' updated.'); $this->deleteOption($option); } $this->createOption($option,$description,$type,$default_val); } #end of updateOption
$this-createOption(
with this:
$this-updateOption(
function init() { if (($this->getOption(UpdatePlugin)=="yes") || ($this->getOption(UpdatePlugin)=="")) { $this->install(); # run the install function. $this->setOption('UpdatePlugin','no'); # !! leave this line, so Update option is a toggle function } } # end of init
$this->updateOption('UpdatePlugin',"Update the plugin options on next call of this plugin. This option will reset it self when updated. !!When this option is set to 'yes' all new and redefined options within this version will be reset to default values!!.","yesno","no");