DirectPHP plugin

download button
faq button

This plugin allows direct embedding of PHP commands right inside Joomla content page for dynamic contents.

Available for all Joomla versions: Joomla 1.0.x, native 1.5.x, 1.6.x, 1.7.x,2.5.x and 3.0.x!

Very Important: If you're using Joomla 1.6 or Joomla 1.7, don't forget to set the Filter Type to "No Filtering" so that you can enter PHP codes in a Joomla 1.6 / 1.7 articles. Details here (works for both Joomla 1.6 and 1.7).

If you're using Joomla 1.5, please refer to the article:
How to enter HTML tags, javascript and PHP codes in Joomla 1.5.8?
Otherwise you might find that all your HTML tags, Javascript and PHP scripts will be removed the moment you click the Save or Apply button.


Key Features

  • Include PHP commands right inside your Jommla content page using the standard php construct:
  • <?php php_commands... ?>
  • Freely intermix static contents with php commands:
  • Now is: <?php echo date('Y-m-d H:i:s');?>
  • Leverage on all your PHP libraries right inside the content page:
  • <?php 
    require_once('fullpath/your_lib.php');
    your_php_func();
    ?>
  • You can edit the php code right inside the default TinyMCE WYSIWYG Editor!
  • Ability to block potentially "risky" commands, e.g. system, fopen, unlink, rmdir, etc. You can add and edit this list of commands-to-block yourself.

Latest Version

v3.0 (for Joomla 3.0)
Released March 21, 2013

Native version of DirectPHP for Joomla 3.0!

Don't forget to set the Filter Type to "No Filtering" so that you can enter PHP codes in a Joomla 3.0 article using the default WYSIWYG editor. Details here.

Background

I've recently redesigned this website using Joomla. As you can see, there are more than 450 PHP-GTK sample codes. It's just too much a hassle to include each sample code into every article. Isn't it nice if I could do a simple

echo "<pre>".htmlentities('filename.php')."</pre>";

to automatically include the respective sample code?

I've tried two other plugins: Jumi and RunPHP .

  • Jumi allows you to call or include another .php file. However, it doesn't allow you to add php commands right inside the content. What this means is that if you want to display a sample code, say example1.php, you need to create an intermediate .php file. In that intermediate php file, you then load and display the sample code with echo "<pre>".htmlentities('filename.php')."</pre>". These intermediate files accumulate very fast and is becoming a bit cumbersome.
  • RunPHP allows you to embed php commands right inside the content page. However, using it is a bit troublesome. You need to use, for example, JCE Editor with Show HTML view. You cannot use the default TinyMCE WYSIWYG Editor.

So I decided to develop my own plugin, and the result is this plugin DirectPHP.

How to install (Joomla 1.0.x)

  1. Download the plugin and unzip it into a folder.
  2. From the Installers menu, select mambots. In the field "Install directory", enter the folder location (where you unzipped the file) and click Install.
  3. From the Mambots menu, select Site Mambots.
  4. Make sure the plugin "DirectPHP" is published. If you see a cross in the Published column, just click on it. It will turn into a green tick.
  5. To try if it works, go to Content Manager, and copy the following text into any static content page:
  6. Current date and time is: <?php echo date('Y-m-d H:i:s');?>
  7. Now load that content page. You should see the current date and time displayed.

To customize the block list (Joomla 1.0.x)

  • From the Mambots menu, select Site Mambots and then click on DirectPHP.
  • On the right-hand side, you will see the Plugin Parameters "PHP commands to block".
  • Edit this list and click Save.

How to install (Joomla 1.5.x)

  1. Download the plugin and unzip it into a folder.
  2. From the Extensions menu, select Install/Uninstall. In the field "Install directory", enter the folder location (where you unzipped the file) and click Install.
  3. From the Extensions menu, select Plugin Manager.
  4. Enable the plugin (DirectPHP) - make sure there is a green tick in the Enabled column.
  5. To try if it works, go to "Content - Article Manager", and copy the following text into any static content page:
  6. Current date and time is: <?php echo date('Y-m-d H:i:s');?>
  7. Very Important: If you're using Joomla 1.5.8 and above, please refer to the article:
    How to enter HTML tags, javascript and PHP codes in Joomla 1.5.8?. Otherwise you might find that all your HTML tags, Javascript and PHP scripts will be removed the moment you click the Save or Apply button.
  8. Now load that content page. You should see the current date and time displayed.

To customize the block list (Joomla 1.5.x)

  • From the Extensions menu, select Plugin Manager, and click on DirectPHP.
  • On the right-hand side, you will see the Plugin Parameters "PHP commands to block".
  • Edit this list and click Save.

Examples

Here are some examples:

  1. You can display the current date and time with:
  2.  Now is: <?php echo date('l j D, Y H:i:s');?> 


  3. You can use for or foreach loop:
  4. <?php
    for ($i=0; $i<10; ++$i) {
        echo "$i: ".$i*$i,"<br>"; 
    } 
    ?>

  5. You can include your own PHP library and call the functions within that library:
  6. <?php
    require_once('/fullpath/your_php_library.php');
    display_image('image123.jpg');
    ?>
  7. You can sprinkle as many PHP statements you want within your content page. Note that these PHP statements will be processed in sequential order, and it respects all scoping of variables. So if you have set a variable earlier, you can use it in your PHP statements later.
  8. <?php global $a;
    $a = 10;?>
    This is a test. variable $a = <?php echo $a;?>
    
    <?php
    function f1() {
        global $a;
        return $a*2;
    }
    $b = f1($a);
    ?>
    
    You can access variable $a from within the function with global.
    variable $b = <?php echo $b;?>
    
  9. You can add any HTML statements through PHP:
  10. <?php
    echo "<table border=4$gt;";
    echo "<tr><td>a11</td><td>a12</td></tr>";
    echo "<tr><td>a11</td><td>a12</td></tr>";
    echo "</table>";
    ?>
  11. You can easily serve different contents for registered and non-registered members:
  12. For Joomla 1.0:

    <?php
    global $my;
    if ($my->id==0) {
        // non-registered members
        print "Please register or login.";
    } else {
        // registered members
        print "Contents for registered members.";
    }
    ?>

    For Joomla 1.5:

    <?php
    $user =& JFactory::getUser();
    if ($user->id==0) {
        // non-registered members
        print "Please register or login.";
    } else {
        // registered members
        print "Contents for registered members.";
    }
    ?>

Important Note: Blocking of PHP commands

By default, I've blocked some of the potentially "risky" commands, e.g. system, fopen, unlink, rmdir, etc.

You can view the entire list of PHP commands that are blocked in the plugin's property page. In there, you can also add, edit or delete this list of commands to suit your needs.

If you need to use any of these commands (e.g. mkdir) from within the content page, put them in one of the library files on your server, do a include() and then call that function. This is a much safer method.

If you insist, you may choose to turn off the blocking in the property page. However, I would strongly advise that you do so only if you're the only one maintaining your Joomla site. If you're opening up the adding and editing of content pages to the public, DO NOT turn off the blocking. It's too dangerous.

Disclaimer

DirectPHP is a simple, yet flexible and powerful plugin. I'm using it on this website. I'm sharing with you here because I think it will be very useful to some of you.

Your use of this plugin is at your own risk. kksou.com makes no warranties or representations, express or implied, as to the functionality or usefulness of this plugin. kksou.com disclaims all warranties, express or implied, including without limitation warranties of merchantability and fitness for a particular purpose. kksou.com disclaims liability for any direct, indirect, incidental, consequential, special, exemplary, punitive or other damages, or lost profits, that may result, directly or indirectly, from your use of this plugin, including without limitation any damage to computer systems, hardware or software, loss of data, or any other performance failures, or any errors, bugs, viruses or other defects that result from or are associated with use of this plugin.

Download


Note: To upgrade to the newer version, simply uninstall the old version and install the new version of the plugin.

Version History

  1. Version 1.00: released March 24, 2008.
  2. Version 1.01: released March 27, 2008.
  3. In TinyMCE Editor, when you press Enter, internally it's stored as a <p>. If you press shift-Enter, internally it's stored as <br>. In v1.00, if the user presses Enter after "<?php", the php script will not be captured. I only preg_matched for <br>. Overlooked the case for <p>. Bug has been fixed.

  4. Version 1.02: released April 6, 2008.
  5. In some of the editors, tabs is internally stored as &160;. If you're using tabs for code indentation, please download this update.

  6. Version 1.03: released May 12, 2008.
  7. Encapsulate all functions used by DirectPHP within a class so that you can use DirectPHP with some other plugins such as the "DirectPHP for Category Description" plugin.

  8. Version 1.04: released June 21, 2008.
  9. Now supports both PHP5 and PHP4.

  10. Version 1.05: October 11, 2008
  11. Thanks to Salvatore who informed me about some "mysterious" double-byte characters that get inserted into the DirectPHP code by the content editor of some of the non-English Joomla. These double-byte characters will cause error when processing the PHP commands with eval(). Have fixed this in this version.

  12. Version 1.56: April 8, 2009
  13. Added one more parameter "Using No Editor". If you're not using any editor when editing content articles, select 'yes' for this parameter. This will allow you to freely use any HTML tags (such as <br /> or <p>).

  14. v1.6 (for Joomla 1.6)
    Released Feb 21, 2011
  15. Native version of DirectPHP for Joomla 1.6!

    Don't forget to set the Filter Type to "No Filtering" so that you can enter PHP codes in a Joomla 1.6 articles. Details here.

  16. v1.7 (for Joomla 1.7)
    Released October 10, 2011
  17. Native version of DirectPHP for Joomla 1.7!

    Don't forget to set the Filter Type to "No Filtering" so that you can enter PHP codes in a Joomla 1.7 article using the default WYSIWYG editor. Details here (works for both Joomla 1.6 and 1.7).

  18. v2.5 (for Joomla 2.5)
    Released January 25, 2012
  19. Native version of DirectPHP for Joomla 2.5!

    Don't forget to set the Filter Type to "No Filtering" so that you can enter PHP codes in a Joomla 2.5 article using the default WYSIWYG editor. Details here.