How to create a module with DirectPHP plugin (for Joomla 1.0.x)
Written by kksou   
Friday, 28 March 2008

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:

Note: the steps below works for Joomla 1.0.x only. For Joomla 1.5.x, please refer to How to create a module with DirectPHP plugin (for Joomla 1.5.x).

Here are the steps to create a Joomla module

  1. Go to Module Manager by selecting "Modules - Site Modules" from the top menu.
  2. Click New to create a new module.
  3. Give it a title. Let's name it "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 Published.
  4. Scroll all the way to the bottom. You will see a field called Custom Output as shown below:
  5. Now copy and paste the following code into the content area:
  6. <?php
    global $database;
    $query = "SELECT * FROM #__content ORDER BY hits DESC limit 0,10";
    $database->setQuery( $query, 0, $count );
    $rows = $database->loadObjectList();
    
    echo '<ul>';
    foreach($rows as $row) {
        echo "<li>$row->title ($row->hits views)</li>";
    }
    echo '</li>';
    ?>
    
  7. 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":
  8. Now click Save or Apply.
  9. For this example, we're using database query. So you need to allow DirectPHP to execute those commands. Go to "Mambots - Site Mabots", click on "DirectPHP", and then on the right hand side where it says Enable PHP Command Block, change it from 'Yes' to 'No'.
  10. 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.

  11. 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:

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.


 
< Prev   Next >

Blog - Forum - Privacy Policy - Contact Us
Copyright © 2006-2012. kksou.com. All Rights Reserved