Help talk:How To Embed a YouTube Video

Modification
This function depends on an added extension, the following code is saved as a php file named   in the extensions folder and   is added to LocalSettings.php. Note that this code is a slight modification of the original in that it allows the width to be specified.

 <?php /* * YouTubeTag.php - Provides youtube tag for embedding a YouTube video into a page. * @author Jim R. Wilson * @version 0.1 * @copyright Copyright (C) 2007 Jim R. Wilson * @license The MIT License - http://www.opensource.org/licenses/mit-license.php * --- * Description: *    This is a MediaWiki extension which adds an additional tag,, for embedding *    YouTube videos into wiki articles. * Requirements: *    MediaWiki 1.6.x, 1.8.x, 1.9.x or higher *    PHP 4.x, 5.x or higher * Installation: *    1. Drop this script (YouTubeTag.php) in $IP/extensions *        Note: $IP is your MediaWiki install dir. *    2. Enable the extension by adding this line to your LocalSettings.php: *           require_once('extensions/YouTubeTag.php'); * Usage: *    Once installed, you may utilize YouTubeTag by placing the tag in an *     article's text: *         * --- * Copyright (c) 2007 Jim R. Wilson * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights to * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of  * the Software, and to permit persons to whom the Software is furnished to do  * so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. * --- */ if (defined('MEDIAWIKI')) { $wgExtensionCredits['parserhook'][] = array(   'name'=>'YouTubeTag',    'author'=>'Jim Wilson (wilson.jim.r&lt;at&gt;gmail.com)',    'url'=>'http://jimbojw.com/wiki/index.php?title=YouTubeTag',    'description'=>'Provides youtube tag embedding videos into a page.',    'version'=>'0.1' );
 * 1) Confirm MW environment
 * 1) Credits

$wgExtensionFunctions[] = "wfYouTubeTagExtension"; function wfYouTubeTagExtension { global $wgParser, $wgMessageCache; $wgParser->setHook( "youtube", "renderYouTubeTag" ); $wgMessageCache->addMessage('youtubetag-bad-id', 'Invalid YouTube video ID supplied: [$1]'); } /** * Callback function for embedding video. * @param String $input Text between open and close tags - should always be empty or null. * @param Array $params Array of tag attributes. * @param Parser $parser Instance of Parser performing the parse. */ function renderYouTubeTag( $input, $params, &$parser ) {
 * 1) Register Extension initializer
 * 1) Extension initializer

# Check for 'v' parameter and ensure it has a valid value $v = htmlspecialchars($params['v']); if ($v==null || preg_match('%[^A-Za-z0-9_\\-]%',$v)) { return ' '.wfMsgForContent('youtubetag-bad-id', $v).' '; }

# Check for 'width' parameter and ensure it has a valid value $width = htmlspecialchars($params['width']); if ($width==null || preg_match('[0-9]',$width)) { $width=425; }   if ($width==0) { $width=425; }   if ($width>1024) { $width=1024; }   if ($width<100) { $width=100; }

$ratio = 425 / 350; $height = round($width / $ratio);

# Build URL and output embedded flash object $url = "http://www.youtube.com/v/$v"; return ''. ' '. ' '. ''. ' '; } } # Closing MW Environment wrapper