Plugin Development Tips and tricks

Back to Plugin Development index
Some random tips and tricks for developing plugins

Finding out currently selected blog

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

Request variables

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 - …)

URL generation

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

$blog = new BLOG($blogid)

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.

$item = ITEM::getitem($id)

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;

get URL to action.php

global $CONF;
$CONF['IndexURL'] . 'action.php.....' 

Globals that are available to the plugins

global $currentSkinName; contains the name of the skin used.

:: plugins ::

Getting catid in doSkinVar() function of a plugin

$catid = $blog->getSelectedCategory();

Keeping the database tables after uninstall

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)
	);
}

Keeping known plugin option values save when updating

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:

  • add the function updateOption to the plugin code. I added it before the install() function.
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
  • Edit the install() function. Replace the code:
$this-createOption(

with this:

$this-updateOption(
  • Call the install function in the init function like this.
    The if condition checks if updating is required. When the UpdatePlugin option isn't present, it will run the update. See also the next point.
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
  • Add the option 'UpdatePlugin' to the install() function
    $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");	
plugindev/tipsandtricks.txt · Last modified: 2007/06/22 15:28 (external edit)
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki