Translations of this page?:

Action Plugin Tutorial

Action plugins are a way to modify many aspects of how DokuWiki behaves in certain cases independent of a page's syntax. To be able to modify a DokuWiki internal behavior it needs to trigger an event. Your action plugin can register as a handler for such an event and then work with the given event data.

To learn more about events, read the following pages:

Sample action plugin 1

Insert a javascript script link in all pages.

  • Register the TPL_METAHEADER_OUTPUT event, with a before EVENT_ADVISE.
  • Add javascript information to “script” meta headers as array type.
<?php
/**
 * Example Action Plugin:   Example Component.
 * 
 * @author     Samuele Tognini <samuele@cli.di.unipi.it>
 */
 
if(!defined('DOKU_INC')) die();
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once(DOKU_PLUGIN.'action.php');
 
class action_plugin_example extends DokuWiki_Action_Plugin {
 
  /**
   * return some info
   */
  function getInfo(){
    return array(
		 'author' => 'Me name',
		 'email'  => 'myname@example.org',
		 'date'   => '2006-12-17',
		 'name'   => 'Example (action plugin component)',
		 'desc'   => 'Example action functions.',
		 'url'    => 'http://www.example.org',
		 );
  }
 
  /**
   * Register its handlers with the dokuwiki's event controller
   */
  function register(&$controller) {
    $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE',  $this, '_hookjs');
  }
 
  /**
   * Hook js script into page headers.
   *
   * @author Samuele Tognini <samuele@cli.di.unipi.it>
   */
  function _hookjs(&$event, $param) {
	$event->data["script"][] = array ("type" => "text/javascript",
                                          "charset" => "utf-8",
					  "_data" => "",
					  "src" => DOKU_BASE."lib/plugins/example/example.js"
				          );
  }
}

Sample Action Plugin 2

Inserts a button into the editor toolbar:

  • registers as handler for the TOOLBAR_DEFINE event with an AFTER advise
  • adds a button definition to the event's data
<?php
/**
 * Example Action Plugin: Inserts a button into the toolbar
 *
 * @author Gina Haeussge <osd@foosel.net>
 */
 
if (!defined('DOKU_INC')) die();
if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');
require_once (DOKU_PLUGIN . 'action.php');
 
class action_plugin_actionexample extends DokuWiki_Action_Plugin {
 
    /**
     * Return some info
     */
    function getInfo() {
        return array (
            'author' => 'Some name',
            'email' => 'foo@bar.org',
            'date' => '2007-04-05',
            'name' => 'Toolbar Action Plugin',
            'desc' => 'Inserts a button into the toolbar',
            'url' => 'http://www.example.com/plugin/toolbar',
        );
    }
 
    /**
     * Register the eventhandlers
     */
    function register(&$controller) {
        $controller->register_hook('TOOLBAR_DEFINE', 'AFTER', $this, 'insert_button', array ());
    }
 
    /**
     * Inserts the toolbar button
     */
    function insert_button(& $event, $param) {
        global $lang;
        global $conf;
        include_once (dirname(__FILE__) . '/lang/en/lang.php');
        @ include_once (dirname(__FILE__) . '/lang/' . $conf['lang'] . '/lang.php');
 
        $event->data[] = array (
            'type' => 'format',
            'title' => $lang['qb_abutton'],
            'icon' => '../../plugins/actionexample/abutton.png',
            'open' => '<abutton>',
            'close' => '</abutton>',
        );
    }
 
}
 
wiki/plugins/action_tutorial.txt · Last modified: 2008/03/19 18:05 by chi
 
Imprint Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki
WikiForumIRCBugsTranslate