v PHP code in Joomla content ~ WebsiteSupport

June 17, 2009

PHP code in Joomla content

Joomla is great for building all sorts of websites, but Websitesupport find that the real power comes from being able to mix and match PHP/MySQL code with the rest of the Joomla code.

There are numerous ways to do this:

  1. If you want to do it the right way then you can always write a custom module or component. A module is not that difficult, but a component is a bit of a different story. I am not going to go into how to write a component here, since the focus of this entry is rather on doing a quick and dirty!

    I've seen this Extension on the Joomla site:
    It is a Developer Toolkit for Joomla that, among other things, "creates boiler plate components" - will check it out one of these days and write up some feedback.

  2. You can use the Static Files component. I used this quite extensively on some of my sites, and it works quite well.

    You can download it from http://www.joomlaxt.com/ and the author has now written up a quick guide that should help you get going with it. You can find it on joomlaxt.com.

    Update : 26 Jan 2008: I do not use this at all any more, but rather KL_PHP, as described below.

  3. But here is the clincher! The BEST way to do 'quick-and-dirty' PHP programming inside Joomla is to use this mambot: KL_PHP. This mambot is not publically available any more but you can download here KL_PHP Mambot.
    Also, you cannot use this bot directly in modules, for that you require the module: mod_kl_evalphp.zip
    Now, I do realise that there are other options available by now, as also commented on below, however, this has worked very well for me up to now so I am not really interested at this stage to try something else!
  4. Here are my tips to get the most out of it:
    • Firstly, don't even try and use it if you have your WYSIWYG editor on. The WYSIWYG editor will destroy some of your code and you can never be sure that the code that you entered so painstakingly will still be there after you've saved it. So be sure to turn your WYSIWYG OFF when you use it in any page.
    • Then make sure that the Mambot is actually published!

You can then create a 'Static Page'. Make the name and title both 'some page' and link it to a menu item called 'Some Page'. I normally use the Othermenu or Usermenu to publish these types of pages for testing purposes. Your 'Some Page' page with your kl_php mambot will then look like this:

{ kl_php}

$inv_num = $_POST['invoice_id'];

and so on ...ending with

{ /kl_php }

Here are a couple of power tips:

  • You can use multiple { kl_php } ... { /kl_php } combinations through your page, but if you want to refer to $inv_num, as in the example above, later on in your page inside a different { kl_php } ... {/kl_php} block, you have to declare it as global first in the other blocks else it it won't have the value you originally assigned to it, it will be seen as a new variable.
  • Apart from that, you can also include other PHP files inside your { kl_php }... { /kl_php} block. This is very powerful since if you don't want to be bothered with writing your PHP code inside your Joomla content, you can write it as external PHP files and just include the PHP files. For maintenance purposes this might be better since if anything happens to your Joomla installation your PHP code will be gone too!
  • PLEASE NOTE: FOR SECURITY PURPOSES ALWAYS INCLUDE A CHECK AT THE TOP OF YOUR FILE TO MAKE SURE THAT THE INCLUDED FILE IS CALLED FROM THE JOOMLA ENVIRONMENT. For example, make sure that you include the following line at the top of your included PHP code:

    defined( '_VALID_MOS' ) or die( 'Restricted access' );
  • You can print output directly to the front end by using 'echo' and 'print' - at least in all the cases that I've used it. In some of the older forum entries it is specified that you should use 'Return' to output any values to the front end, however, I have not found that to be the case any more.
  • In my most recent usage of this mambot, I have even found that it is possible to sessions. It is therefore possible to code quite complex applications directly into your Joomla content by passing parameters through forms as well as sessions.

This is just a quick write-up of the basic functionality of this great extension - any more questions, just write a comment or drop me an email and I will see if I can elaborate.

0 Comments Here:

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | coupon codes

HTML Hit Counter