Creating a search plugin

From Joomla! Documentation

Revision as of 15:30, 19 January 2008 by Marieke92 (Talk | contribs)

Replacement filing cabinet.png
This Namespace has been archived - Please Do Not Edit or Create Pages in this namespace. Pages contain information for a Joomla! version which is no longer supported. It exists only as a historical reference, will not be improved and its content may be incomplete.
Quill icon.png
Page Actively Being Edited!

This j1.5 page is actively undergoing a major edit for a short while.
As a courtesy, please do not edit this page while this message is displayed. The user who added this notice will be listed in the page history. This message is intended to help reduce edit conflicts; please remove it between editing sessions to allow others to edit the page. If this page has not been edited for several hours, please remove this template, or replace it with {{underconstruction}} or {{incomplete}}.


XML file

The XML file is named the same as the PHP file, and is one of the two required files. Always start off with the XML tag and define that it is written in a UTF-8 format.

<?xml version="1.0" encoding="utf-8"?>
 <!DOCTYPE install PUBLIC "-//Joomla! 1.5//DTD plugin 1.0//EN" "">

To define that the plugin has to be a search plugin, add this line:

<install version="1.5" type="plugin" group="search">

The type will define it is a Plugin, the group defines the Plugin is in the group of Search Plugins.

After that, add some information about yourself and the Plugin, like this:

<name>Name of your Search Plugin</name>
<creationDate>Created Date</creationDate>
<author>Your name</author>
<authorEmail>Your e-mail address</authorEmail>
<authorUrl>Your website</authorUrl>
<license>License, for example GNU/GPL</license>
<version>Version of the plugin</version>
<description>Description of the Plugin; showed with installation and when editing the Plugin in the Plugin Manager</description>

And now include your PHP file to the Search Plugin. The name of this file should be the same as the name of this XML file. Put this name also behind the plugin="" part.

You could also add more files for your plugin, for example an image. Just add another row between <files> and </file>, and then place the file between <filename> tags.

   <filename plugin="nameofplugin">nameofplugin.php</filename>

For the internationalization, we will use language files. This is not required, but people from other countries will love it they can easily translate your plugin to their own language. The language tags can be found here: [1] (use the ISO 639-1 column) and here: [2]

   <language tag="en-GB">language/en-GB/en-GB.plg_search_nameofplugin.ini</language>

Optionally, you could add some parameters to the Plugin. These will look like this:

   <param name="paramname" type="typeofparameter" default="defaultsetting" label="title" description="description"/>
  • Param name: The name of the parameter. You will need this when creating the PHP file.
  • Param type: You could choose between several types of parameters. For example, "text" is used to display an input box.
  • Param default: The default setting for this parameter.
  • Param label: The name of this parameter displayed in the edit screen of this Plugin in the Plugin Manager.
  • Param description: The text which appears as a tool tip for this parameter.

And do not forget to end your XML file with the following tag:


PHP file

The PHP file of your Plugin is probably the most important file of the Plugin. First start with information about the Plugin and yourself. For example:

 * @version		$Id: nameofplugin.php versionnumber date author
 * @copyright	        Copyright
 * @license		License, for example GNU/GPL
 * All other information you would like to add

To prevent accessing the document directly, enter this code:

// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

Now define the registerEvent and the language file. Replace 'nameofplugin' by the name of your Plugin.

$mainframe->registerEvent( 'onSearch', 'plgSearchnameofplugin' );
$mainframe->registerEvent( 'onSearchAreas', 'plgSearchnameofpluginAreas' );
JPlugin::loadLanguage( 'plg_search_nameofplugin' );

Then define a function to return an array of search areas. Replace 'nameofplugin' by the name of your Plugin.

function &plgSearchnameofpluginAreas()
	static $areas = array(
		'nameofplugin' => 'Nameofplugin'
	return $areas;

Then the real function has to be created. The database connection should be made. The function will be closed with an } at the end of the file.

function plgSearchnameofplugin( $text, $phrase='', $ordering='', $areas=null )
	$db		=& JFactory::getDBO();
	$user	=& JFactory::getUser();

When the array is not correct, return it:

	if (is_array( $areas )) {
		if (!array_intersect( $areas, array_keys( plgSearchnameofpluginAreas() ) )) {
			return array();

And yet it is the time to define the parameters! First get the right plugin; 'search' (the group), 'nameofplugin'.

$plugin =& JPluginHelper::getPlugin('search', 'nameofplugin');

Then load the parameters of the Plugin..

$pluginParams = new JParameter( $plugin->params );

And define the parameters. For example like this..

$limit = $pluginParams->def( 'nameofparameter', defaultsetting );

Use the function trim to delete spaces in front of or at the back of the searching terms

$text = trim( $text );

Return when nothing was filled in

if ($text == '') {
		return array();

After this, you have to add the database part. This will be the most difficult part, because this changes per situation. In the coding examples later on you will find some of the examples used by Joomla! 1.5 core Search Plugins. Although, it will look something like this. Explanation is added within the PHP quotes.

$wheres = array();
	switch ($phrase) {
//search exact
		case 'exact':
			$text		= $db->Quote( '%'.$db->getEscaped( $text, true ).'%', false );
			$wheres2 	= array();
			$wheres2[] 	= 'LOWER( LIKE '.$text;
			$where 		= '(' . implode( ') OR (', $wheres2 ) . ')';
//search all or any
		case 'all':
		case 'any':
//set default
			$words 	= explode( ' ', $text );
			$wheres = array();
			foreach ($words as $word)
				$word		= $db->Quote( '%'.$db->getEscaped( $word, true ).'%', false );
				$wheres2 	= array();
				$wheres2[] 	= 'LOWER( LIKE '.$word;
				$wheres[] 	= implode( ' OR ', $wheres2 );
			$where = '(' . implode( ($phrase == 'all' ? ') AND (' : ') OR ('), $wheres ) . ')';
//ordering of the results
	switch ( $ordering ) {
//alphabetic, ascending
		case 'alpha':
			$order = ' ASC';
//oldest first
		case 'oldest':
//popular first
		case 'popular':
//newest first
		case 'newest':
//default setting: alphabetic, ascending
			$order = ' ASC';
//replace nameofplugin
	$searchnameofplugin = JText::_( 'Nameofplugin' );
//the database query; differs per situation! It will look something like this:
	$query = 'SELECT AS title'
	. ' CONCAT_WS( " / ", '. $db->Quote($searchNameofplugin) .', b.title )AS section,'
	. ' "1" AS browsernav'
	. ' FROM #__nameofplugin AS a'
	. ' INNER JOIN #__categories AS b ON = a.catid'
	. ' WHERE ( '. $where .' )'
	. ' AND a.published = 1'
	. ' AND b.access <= '. (int) $user->get( 'aid' )
	. ' ORDER BY '. $order
//Set query
	$db->setQuery( $query, 0, $limit );
	$rows = $db->loadObjectList();
//the 'output' of the displayed link
	foreach($rows as $key => $row) {
		$rows[$key]->href = 'index.php?option=com_newsfeeds&view=newsfeed&catid='.$row->catslug.'&id='.$row->slug;

And end your plugin with

return $rows;

INI file(s)

Coding examples

Points to watch