MediaWiki Developer's Handbook/Add Button

From Wikibooks, open books for an open world
Jump to navigation Jump to search
if( !defined( 'MEDIAWIKI' ) ) {
  die( "This file is part of MediaWiki and is not a valid entry point\n" );
  }
$myRedirectPng = "extensions/myRedirect/myRedirect.png";

$wgHooks['EditPage::showEditForm:initial'][] = 'myRedirectButton';
$wgExtensionCredits['myRedirectButton'][]= array(
  'name'         => 'my Redirect Button Extension', 
  'version'      => '1.0.0', 
  'author'       => 'Your Name', 
  'url'          => 'http://www.your.com/myredirectbutton/', 
  'description'  => 'This extension is for demonstration only.');

// Add a button to the internal editor
function myRedirectButton ($editPage) { 
  global $wgOut, $wgScriptPath, $myRedirectPng;
  
  // Insert javascript script that hooks up to create button.
  $wgOut->addScript("<script type=\"text/javascript\">\n".
       "function myAddButton(){\n".
       "  addButton('$myRedirectPng','Redirect','#REDIRECT [[',']]','Insert text');".
       "  }\n".
       "addOnloadHook(myAddButton);\n".
       "</script>");
        return true;
  }

To install

  • Create a directory mediawiki/extensions/myRedirect
  • Save above script as myRedirect.php
  • Save the following picture to the directory as myRedirect.png. .
  • Append the following line to LocalSettings.php
require_once("$IP/extensions/myRedirect/myRedirect.php");


Add Button that calls a custom javascript function[edit | edit source]

<?php

// to install, upload this file as /extensions/myEditbarButton/myEditbarButton.php
// then, copy this button image as /extensions/myEditbarButton/myButtonCard.png
//
// then add the following line to end of LocalSettings.php
//
//       require_once("$IP/extensions/myEditbarButton/myEditbarButton.php");
//
// This is a modification of 
// https://en.wikibooks.org/wiki/MediaWiki_Developer%27s_Handbook/Add_Button
//
// Sorry that extension can only add a new button before the other buttons
// of the edit bar, instead of after them. I am also not sure if this will work
// on other installations of mediawiki ( mine is MediaWiki: 1.16.5, PHP: 5.2.17
// MySQL: 5.1.69 )

if( !defined( 'MEDIAWIKI' ) ) {
  die( "This file is part of MediaWiki and is not a valid entry point\n" );
  }
$myButtonPng = "extensions/myEditbarButton/myButtonCard.png";

$wgHooks['EditPage::showEditForm:initial'][] = 'myEditbarButton';

// for $wgExtensionsCredits[$type][]
//       $type must be api, media, parserhook, skin
//                     specialpage, variable, other

$wgExtensionCredits['other'][] = array(
        'name'         => 'my Editbar Button', 
        'version'      => '1.0.0', 
        'author'       => 'Your Name', 
        'url'          => 'http://www.your.com/myredirectbutton/', 
        'description'  => 'Adds a new button to the old edit bar that calls a custom javascript function.'
        );

// Add a button to the internal editor
function myEditbarButton ($editPage) { 
        global $wgOut, $wgScriptPath, $myButtonPng;
  
        // Insert javascript script that hooks up to create button.
        $wgOut->addScript("<script type=\"text/javascript\">\n".
                "function myAddButton(){ \n".
                        "var toolbar = document.getElementById( 'toolbar' ); \n".
                        "var image = document.createElement( 'img' ); \n".
                        "image.className = 'mw-toolbar-editbutton'; \n".
                        "image.src = '$myButtonPng'; \n".
                        "image.border = 0; \n".
                        "image.alt = 'speedTip'; \n".
                        "image.title = 'speedTip'; \n".
                        "image.style.cursor = 'pointer'; \n".
                        "image.onclick = function() { \n".
                                "window.alert('HelloWorld'); \n".
                        "} \n".
                        "toolbar.appendChild( image ); \n".
                "} \n".

                "addOnloadHook(myAddButton); \n".
        "</script>");
        return true;
}

?>

Links[edit | edit source]

http://commons.wikimedia.org/wiki/Category:Button_link