LOGIC Library

This site is the Logic involvement in sharing expertise and skills acquired in daily work. The goal is to create a solid knowledge base and share best practices in software development and systems management.

More info about us can be found on logicsistemi.it.

A redirect plugin for Joomla! 2.5

In this post we'll take a look to content plugins and we'll write a very simple plugin that allows to create a redirect page in our Joomla! Site.

By doing this we will discover how to use a plugin to modify article test or add Javascript code to it.

Our goal

Our site lists external resources, such has blog posts, videos and pictures. When we want to add a new resource we create an article with a link to that resource. In this way we can keep track of user choices.

To improve user experience we decide to add a way to automatically redirect users on the desidered destination. This will be done with a Joomla! plugin that performs the redirection.

Joomla! content plugins

A Joomla! plugin is a a set of code snippets executed when a particular event occurs. Every plugin is built creating a PHP class with the right methods.

Content plugins respond to events triggered during the preparation of a page and we can use them to alter the page content or perform actions based on the article content or parameters.

A full explanation of plugins development is out of the scope of this article: if you need more information about plugins development you can take a look at this series of post.

In this post we'll focus on the code necessary to accomplish our task.

Creating the plugin structure

The mimimal set of files needed to build a plugin is composed by:

  • A manifest file: this is required for the plugin installation in your site

  • The PHP plugin definition

  • An empty index.html file: to hide the folder content to web users

Let's start by creating a folder called plg_redirect containing an empty index.html file and a redirect.xml file with the following content:

<?xml version="1.0" encoding="utf-8"?> 
<extension version="2.5" type="plugin" group="content" method="upgrade"> 
    <name>Content - Redirect</name> 
    <author>Edy Incoletti</author> 
    <creationDate>May 2013</creationDate> 
    <license>GNU General Public License</license> 
    <authorEmail>edy.incoletti@logicsistemi.it</authorEmail> 
    <authorUrl>eincoletti.wordpress.com</authorUrl> 
    <version>1.0.0</version> 
    <description>A simple redirect plugin</description> 
    <files> 
        <filename plugin="redirect">redirect.php</filename> 
        <filename>index.html</filename> 
    </files> 
</extension>

This content should be clear: we are defining a content plugin with 2 files.

Then we can complete the folder with a redirect.php file with this base content:

<?php 
defined( '_JEXEC' ) or die( 'Restricted access' ); 
// Import library dependencies 
jimport('joomla.event.plugin'); 
class plgContentRedirect extends JPlugin 
{ 
}

This is the base structure of our plugin. The only important thing, at the moment, is to check the name of our class: the last part must match the plugin name specified in the manifest file.

The folder, once zipped, can be installed on Joomla using the extension manager and activated in the plugin manager.

The plugin code

To complete our work we need now to choose the right event, so let's a look to the action we need to accomplish to choose it.

1. We search in the article content the string:
{redirect}URL|TIMEOUT{/redirect}

Where URL is the redirection and timeout is the number of seconds to wait before the redirection appens (ie to display an advertisement).

2. If timeout is not present or is 0, we perform a PHP redirection using the header function

3. If timeout is present the redirection is performed, after the specified timeout, using Javascript

This actions needs to parse the article content and modify it before it's displayed, so we need to put our code in the first event managed by the plugin and this is the onContentPrepare event.

This is the full code:

<?php 
defined( '_JEXEC' ) or die( 'Restricted access' ); 
// Import library dependencies 
jimport('joomla.event.plugin'); 
class plgContentRedirect extends JPlugin 
{ 
	public function onContentPrepare($context, &$row, &$params, $limitstart)
	{
		if (preg_match_all("#{redirect}(.*?){/redirect}#s", $row->text, $matches, PREG_PATTERN_ORDER) > 0)
		{
			// Delete plugin string: should never be displayed
			$row->text = preg_replace("#{redirect}(.*?){/redirect}#s", "", $row->text);
			
			if ($context == "com_content.article") 
			{
				// Do redirection
				$match = $matches[1][0];				
				$plg_params = split("\|", $match);
 				if ($plg_params[1]) 
				{
					// JS redirection with time
					$document = & JFactory::getDocument();
					$document->addScriptDeclaration("
						window.addEvent('load', function() {
							setTimeout(function(){
									document.location.href = '" . $plg_params[0] ."';
								}," . ($plg_params[1] * 1000) . ");
						});");             	
				} 
				else 
				{
					// PHP redirection
					header("Location: " . $plg_params[0]);
					die();
				}
			}
		}
	}    
}

The preg_match_all search, in the page text, the redirect pattern and delete it to avoid display.

If this pattern is found, we check if we are in an article or in a list (ie category blog) by checking the context variable: redirection is performed only in article pages.

After that we put some PHP code that read parameters and add a piece of Javascript to the page or performs the redirection.

That's all. If you want to can download the complete plugin package.

Further improvements

The creation of a page for every resource can be a time consuming task. As an exercice, try to use URL parameters to specify the redirection settings and use a single article to perform it.

Have a nice try!

Comments   

 
#1 Bruck Wondaferaw 2014-06-04 07:13
i have accidentally activated the "system redirect" plugin and now i can't login to my own page on both fron end and back end.

When trying to log in, It gives me the error

"Could Not Redirect! You did not specify a valid URL in the fileld Redirect URL of plugin setting!"

I couldn't find any useful suggestion online...

Please, give me directions to change the codes to deactivate the joomla 2.5 redirect plugin from the code it self from my FTP.

Mind that i am not a programmer!

Thnx in advance it would mean a lot,
Quote
 
 
#2 Edy Incoletti 2014-06-04 09:09
Delete the file /plugins/content/redirect/redirect.php
Log to the administration
Disable the plugin
Restore the deleted file.

In this way the plugin is installed, but disabled, so you can uninstall it or modify the settings to make it work.
Quote
 
 
#3 Narciso Benedicto 2014-06-11 13:39
It's working great for us in Joomla 3.3.0. I haven't had to change a single character.

Thank's for the plugin.
Quote
 

Add comment


Security code
Refresh