How to create a module with DirectPHP plugin (for Joomla 1.5.x)

Posted: May 19, 2010 in Joomla, PHP
Tags: ,

You can easily create Joomla modules using the DirectPHP plugin.

In this article, I will show you how to create a module similar to the Most Popular module (mod_mostread) that comes with the standard Joomla installation.

Our version of “Most Popular” module has one additional enhancement: it will list the total number of hits for each listing as shown below:

codehunterbd


1.The main “problem” with Joomla 1.5.x is that the HTML module (was Custom module in 1.0.x) is no longer processed by the content plugins. So we cannot use the method as outlined in the article How to create a module with DirectPHP plugin (for Joomla 1.0.x).

2.To resolve this, we need to install a module called “CustomContent” by Ian MacLennan first. This is a module that allows you to publish a content article in a module position. Please go ahead and install this module first.

3.Next we create an article to hold the PHP codes for the module. From the Content Menu, select “Article Manager”, and click New.
Let’s give it a title “Most Popular v2″.
For the Section and Category, you can select Uncategorized. For Published, make sure it’s set to ‘Yes’.

codehunterbd

4.Now copy and paste the following code into the content area:


setQuery( $query, 0, $count );
$rows = $db->loadObjectList();

echo ‘

    ‘;
    foreach($rows as $row) {
    echo ”
  • $row->title ($row->hits views)
  • “;
    }
    echo ‘

    ‘;
    ?>

    5.If you’ve copied and pasted the code from this web page, most likely you will see the format set to “preformatted”. Highlight all the text and make sure you change it to “Paragraph”:

    codehunterbd

    6.Save the article.

    7.Now from the Extensions menu, select Module Manager. Look for the module named “Custom Content” and click on it.

    8.On the left-hand side which says “Details”, change the title to “Most Popular v2″.
    Select a Position. If you’re using the standard installation, you can leave it as ‘Left’.
    Also, make sure ‘Yes’ is selected for Enabled.

    codehunterbd

    9.On the right-hand side which says “Parameters”, click the “Select” button. You will be prompted with a screen to select an article. Choose the article named “Most Popular v2″ which you have created in Step 3 above.
    For “Module Class Suffix”, you may want to put “_menu” to give a frame to the module.

    codehunterbd

    10.Now click Save.

    11.For this example, we’re using database query. So you need to allow DirectPHP to execute those commands. Go to “Extensions – Plugin Manager”, click on “DirectPHP”, and then on the right hand side where it says Enable PHP Command Block, change it from ‘Yes’ to ‘No’.

    codehunterbd

    Note: Please make sure you’re only doing this on your development machine. It’s too dangerous to do this on a live machine, unless you’re the only one managing your website.

    12.You’re done creating the module! Try loading your Joomla home page. You should be able to see the new module “Most Popular v2″ appearing on the left hand side of the page as shown below:

    codehunterbd

    Of course the above code is extremely simplified. I just want to show you that creating a Joomla module using DirectPHP plugin is simple and straightforward.

    If you want, you can refer to mod_mostread.php and put in a more complex database query that checks access control, whether it’s published or not, and adds a link to the article, etc.

    Creating More Modules:

    To create more modules, you do not need to install more copies of “CustomContent”.

    Following from the above example, from the Module Manager, check the module “Most Popular v2″ , and then select “Copy” from the menu on the top-right as shown below (the third one from the left):

    codehunterbd

    You will see another module called “Copy of Most Popular v2″ appearing above or below “Most Popular v2″. Click on that module and then select another article containing a different PHP code. You have now a new module!

    About these ads
Comments
  1. After looking into a few of the blog articles on your site, I truly appreciate your way of blogging.
    I added it to my bookmark site list and will be checking back in the near future.
    Take a look at my website as well and let me know what you think.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s