Joomla PHP Module

Joomla PHP Module is a Joomla module that allows you to embed PHP, HTML, JavaScript and CSS in a Joomla module position. It is based on the Joomla HTML Module (mod_html) which allows embedding HTML, JavaScript and CSS in a module position, but with the added benefit of allowing PHP code.

Joomla PHP Module Compatibility
There are two versions of Joomla PHP module, one for Joomla1.0 and the other for Joomla1.5.

Download Joomla PHP Module

Joomla PHP Module Installation and Configuration

  • Download the latest version of Joomla PHP Module from our Joomla PHP Extensions Download.
  • In your Joomla Administration Panel, go to Installers -> Modules and upload the Joomla PHP Module Installation package you downloaded.
  • Go to the Mambot Administration Panel: Modules -> Site Modules, choose the "Joomla PHP Module" module from the list and choose "Edit" to edit the parameters.
  • Add the PHP you need to the PHP parameter.
  • Publish the Module.

Frequently Asked Questions

Do I have to wrap the PHP code in special tags? No, you can use the regular PHP tags directly. eg:

<?php phpinfo(); ?>
will display the PHP information.

Can I use predefined Joomla variables? Yes, but you first have to declare them as globals. eg:

global $mainframe;

Can I include PHP files? Yes, but make sure you use the full path to the PHP files. It's just easier.
Example:

global $mainframe; 
include($mainrame->getCfg('absolute_path').'/modules/mod_php/includes/my_php_file.php');

Can I use more than one Joomla PHP Module on a page? Yes, just copy the module using the built in Joomla Module Copy feature.

Joomla Tips

Joomla based websites and applications work better on Joomla hosting because it was specially designed for the Joomla! technology.

Documentation and Examples:

Here are some examples of PHP code you can use in the Joomla PHP Module.

Example 1: Welcome Message

Example Snippet: Show a welcome Message to users:
<?php

/**
* Custom code for mod_php. 
* copyright: www.fijiwebdesign.com
* Shows a welcome to the logged in user, or vistor. 
*/

global $my;

if ($my->id) {
	echo 'Hello, '.$my->username;
} else {
	echo 'Hello, visitor';
}

?>

Example 2: Flickr Photos

Example Snippet: Display Flickr Photos
<?php

/**
* Example of display flickr photos for a tags list
* Requires: allow_url_fopen enabled in php.ini
*/

/**
* Configurations
*/
// the tags to get photos for
$tags = 'fiji';
// number of pics to show
$total = 5;

// main template
$main_template = '
	<!-- photos for: '.$tags.' -->
	<div class="flickr_photos">
		{photo_template}
	</div>
';
// the html template for each photo item/row
$photo_template = '
		<div class="flickr_photo">
			<img src="{img_src}" width="{img_width}" height="{img_height}" alt="{img_alt}" />
			<h4>
				<a href="{url}" target="_blank">{title}</a>
			</h4>
		</div>
';
// css for our photos styling
$css = '
	<style type="text/css">
	
	.flickr_photo {
		background: #cfcfcf;
		text-align: center;
	}
	
	.flickr_photo img {
		border: 1px solid #000;
	}
	.flickr_photo h3 {
		font-size: 100%;
		font-weight: bold;
		margin: 0px;
		padding: 5px;
	}
	
	</style>
';

/**
* End Configurations
* Start Flickr Feed Request and display
*/

$flickr_feed_url = 'http://api.flickr.com/services/feeds/photos_public.gne?tags='.rawurlencode($tags).'&format=php';
include($flickr_feed_url);

$photos_html = '';
if (isset($feed['items']) && ($count = count($feed['items']))) {
	$n = $count < $total ? $count : $total;
	for($i = 0; $i < $n; $i++) {
		
		$item = $feed['items'][$i];
		$row_html = $photo_template;
		$repl = $item;
		
		// flickr image is in description, so we have to parse it out
		preg_match("/<img src=\"(.*?)\" width=\"(.*?)\" height=\"(.*?)\" alt=\"(.*?)\" (.*?)\/>/ui", $item['description'], $matches);
		if ($matches) {
			$repl['img_html'] = $matches[0];
			$repl['img_src'] = $matches[1];
			$repl['img_width'] = $matches[2];
			$repl['img_height'] = $matches[3];
			$repl['img_alt'] = $matches[4];
		}
		
		foreach($repl as $name=>$value) {
			$row_html = str_replace('{'.$name.'}', $value, $row_html);
		}
		
		$photos_html .= $row_html;
	}
} else {
	echo 'No photos found for, '.htmlentities($tags);
}

$html = str_replace('{photo_template}', $photos_html, $main_template);

// display
echo $css;
echo $html;


// debug
function dump($obj) {
	echo nl2br(str_replace(' ', ' ', htmlentities(print_r($obj, true))));
}
?>

 

Joomla Downloads

DownloadsDownload our free Joomla! Components, Modules and Plugins.