Skip to content

Making_A_Plugin_Searchable

Cameron edited this page Jan 28, 2017 · 1 revision

title: Making A Plugin Searchable permalink: /Making_A_Plugin_Searchable/

Making A Plugin Searchable

This guide describes how to add your plugin to the core e107 search functionality.

It also shows you how you can add a search text box to your plugins page(s).

e_search.php

Create a file called e_search.php in your plugin folder. It must be called this as it is a well known file name that is looked for in each plugin folder.

  • sfile is the file containing the search code to be run that returns results
  • qtype is the name of your plugin or similar - this is the text that will apear in the Search page drop down list.
  • refpage is where you go to if a user clicks on a search match for your plugin
  • id a unique id for your plugin, use the plugin folder name
  • advanced is the file containing the advanced search code to be run that returns results, optional

search.php

Create a file called search.php (or whatever you called the file in the 'sfile' array element in your e_search.php file). The following example is adapted from the Election plugin.

Note: you must define all these variables, even if they are blank, to prevent contamination of bad values from other plugins' search modules.

search.php (old style)

This is an older way of defining a search results file. However, it is still valid code and can be used in preference to the newer method (above).

Create a file called search.php (or whatever you called the file in the 'sfile' array element in your e_search.php file). The following example is adapted from the Userjournals plugin.

Here's a rundown of what's happening in this file.

  • First, we call a function that returns our search results. Not strictly neccessary but helps prevent corrupting global variables. The search results text must be appended to the variable $text.
  • $nothingfound is just a flag so you can put out a nice message if nothing found. Not really needed here but useful if you have mulitple SQL queries for which you want to return search results for (e.g. search in name, decription and category).
  • $linkprefix is just to save typing it in a lot of places (potentially). It's set to the HTML that represents the first part of the anchor that will be used to link the search result(s) back to your plugins page.
  • $search_info[$key]['qtype'] is the title for your section of the search results page. You must assign it to this array element.
  • In the SQL query the value of $query is the text the user is looking for. Use REGEXP to search for this value in your columns.
  • $short and $long - the two parts to the search result, the first is the title/link that can be clicked to go to the appropriate page, the 2nd is the longer textual description with the search term highlighted.
  • $result is appended with the result of each match, note the formatting (i.e. breaks). Also note that $linkprefix is appended with the specific URL parameters so that the appropriate page is displayed if the link is selected.

Note that the English text should really be localized and put in a language file.

advanced_search.php

Not sure of all the options for this file yet. This is just the Content Manager plugin advanced search file for now.

Adding A Search Text Box

You can add a search text box - so users can search directly from your plugins page(s) by adding some simple HTML (but see notes after sample code).

This will give you a text box where the user can type in their search term and press Enter/Return to start the search.

Note: In practice this does not work too well. This is because the hidden field t actually represents the plugin as a number, and this will vary from site to site depending on what plugins are installed. An alternate approach here is to use the plugin folder name.

Also, as mentioned, the English text should really come from a language file.

Note: You must create a plugin.php file and install your plugin through the e107 Plugin Manager. This allows e107 to register your plugin in the e107_core/search_prefs table so that e107_search will include it in it's searchable modules.

Searching Comments in your Plugin

Many third party plugins allow comments to be posted on records in the plugin. To be able to search these comments you need to create a file called search_comments.php. Following the e107 convention of putting all search related scripts into a sub directory called search this example assumes that file path is used.The search_comments.php file should be found on plugin installation or with a tools-database-scan plugin directories.

Category:API Category:PluginWriting

Clone this wiki locally