PHP code in Joomla content
Written by Christine   
Tuesday, 04 July 2006

Joomla is great for building all sorts of websites, but I 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 this link.

    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!
    • I use the Joomla 'Static Pages' for almost all my PHP code. Since they don't belong to any categories, it makes it easy to just create a page and refer to it directly.
    • Since I use either s404SEF or OpenSEF, all my Static Pages have nice names like '/select-item.html', or '/display-pics.html'. You can now do the following directly in your pages:

<form action = http://www.yoursite.com/some-page.html action = post>

<input type = "text" size = "20" name = "invoice_id">

<input type = "submit" value= "submit">

</form>

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.

Trackback(0)
Comments (25)Add Comment
Thanks
written by Camnio Media, January 22, 2010
Building a church website and this will help thanks. http://www.camnio.com
Thanks
written by Greg33, January 18, 2010
Thanks for this post - I really like using these tools and hope yu get the most out of PHP with Joomla. I also like using the http://www.fastregistryfixes.com as well.
not getting it!
written by rob, January 17, 2010
I still don't get it. For example I want to place a calorie calculator php script in my Static Content article. Are you saying that i include the whole script, with tiny wysiwyg turned off between - { kl_php } ... { /kl_php } - doesn't work!
Fiji Webdesign module - comment
written by Christine, October 26, 2008
Andrew, please remember that this module is just that, a MODULE, so you cannot actually use this to add code to your content itself
Php Module
written by Andrew, October 26, 2008
Easy way to add some custom PHP code anywhere on your website would be to use the PHP module. Its very handy to have. http://www.fijiwebdesign.com/portfolio/joomla-php-module-mod_php.html
problem with joomla
written by sakakiro, July 31, 2008
Dear sir,

Now, I'm finding to write code php on joomla. why can't i write code php when add new article? Thank in advance for answer.
Jumi does the same and it's joomla 1.5 native better inerface IMHO
written by Fabienne, July 11, 2008
I found THIS:
http://jumi.vedeme.cz/index.php?option=com_remository&Itemid=53
or here:
http://extensions.joomla.org/component/option,com_mtree/task,viewlink/link_id,1023/Itemid,35/

And this works SO WELL!!!!!

Thank you for your article: it did help me!
I hope you will enjoy jumi as i do...
Turning dynamic links to static.
written by ermd, May 18, 2008
Hi everybody,
I have a Joomla website and I,d like to turn the links to static, is there any way I can do that just by logging as administrator or do I need to download Joomla? I did not build the website myself so I don't know what version of Joomla the designer used.
Thank You!
mix of php, sql and html output
written by david, May 14, 2008
hi

i have a little script which pulls things out of the db and displays them in html, is it possible to use this {kl_php} method to display it in content, i want to add it to the bottom of existing content, it's a display hotels near to the golf course on that content page
NIce..
written by Good, May 09, 2008
this is very nice...i have solved my problem from using this...


Thanks
:(
written by Marius, January 29, 2008
After installing it all and some figuring I still have one issue, out of the blue there is after my included file 'Array' as text. I have no idea where this comes from, when i refer the included file directly from filesystem this doesn't show. Anyone any idea?
http://contargo.net and http://contargo.net/pegel.php
Jumi
written by justin, December 18, 2007
I recommend using Jumi instead of kl_php now
http://www.vedeme.cz/index.php?option=com_content&task=category&sectionid=10&id=41&Itemid=123
Search Engines and KL PHP
written by Christine, November 06, 2007
Theoretically the search engines should process your page that has an included page in it the same as a normal reader would, in other words, it is not supposed to spider it as 'include XYZ', but the actual results of the included page.

If you DO want to prevent the search engines from spidering that page to keep it out of the results you can always add that URL to your Robots.txt file (to not index the page)
Search Problem with kl_php
written by ljk, November 06, 2007
Hello,

I am using kl_php on a Joomla site and it works great. My content item is just an include of a file. When a user searches the site, what they see in the results is the "include('store/get-product.php');"

Have you found a way to either get the search results to process the include or not to show the include in the results?
No Confirmation mail
written by dee, May 01, 2007
Hi,
I am new to this site,Realy when compared to other site this site provided much information of Joomla CMS and its use.The information is clear and understandable.Hatsoff and awesome.

I have registered in this site .But I didn\'t get any confirmation mail.I am tring to login ,but failed.
Awesome tips
written by Flavio, March 20, 2007
Your tips are awesome. Just what I needed. Thanks for explaining everything.

Best,

Flavio
kl_php and joomla parse error
written by Donavon, March 08, 2007
I am trying to get the proper usage of kl_php. Here is what the code I am entering into the static content page. It get a joomla parse error:


Parse error: syntax error, unexpected '<' in /home/tm4ycoza/public_html/mambots/content/kl_php.php(65) : eval()'d code on line 1

I manage to use it with JCE Editor
written by Guppy, January 13, 2007
First click the \"show/hide\" on the editor to get to the \"no editor mod\". Add your code using the kl_php. Save.

When you get back to the editor in another session, it will start out in HTML mode. Then just click the \"show/hide\" and then \"QUIT\"...don\'t SAVE!!!
Then click the content item to get back to the editor, and it will show in the correct mode and you can continue adding code etc.
...
written by Edouardj, September 15, 2006
Hello,

I try to find documentation on kl_php and i find your site. On joomla\'s site there another bot: runPHP. It is like kl_php. It is just for information beacause i doesn\'t test then yet.Bye
It works for me....
written by Christine, August 09, 2006
Hi,

The includes do normally work for me.

I just notice that you have the closing kl-php with a \'dash\', and not as kl_php.

Is this just a typo and you really have it with kl_php?

Also try the include without the brackets - just include \"test.php\";

Christine
Include doesnt work
written by Guest, August 09, 2006
Hi Christine,

Good article!!

I use KL_PHP and it works great. I enclose all my PHP-code in the content pages. As you mentioned this is not the best solution. There is a lot of redundant code in the content pages.
Perhaps you can help me out here. If I include a PHP-file in {kl-php} it doesnt see the PHP-script.

example:
File: test.php
Content: echo \'Hello\';

Contentpage:
{kl_php}
include (\"test.php\");
{/kl-php}

What can be wrong here???
{kl_php}
written by Guest, August 09, 2006
Hi Christine,

Good article!!

I use KL_PHP and it works great. I enclose all my PHP-code in the content pages. As you mentioned this is not the best solution. There is a lot of redundant code in the content pages.
Perhaps you can help me out here. If I include a PHP-file in {kl-php} it doesnt see the PHP-script.

example:
File: test.php
Content: echo \'Hello\';

Contentpage:
{kl_php}
include (\"test.php\");
{/kl-php}

What can be wrong here???
Need Help
written by Guest, July 18, 2006
Sir I read this page, it is good.
I have category links in menu, i want that when a category is clicked the main body are should show products of that category, and dynamic links to be created on the product name or image when they are clicked a new main page should show the discription of that product. I want to page populated from a database. It is basically an ecommerce site in joomla.

If you can help me please email me at aliawais@gmail.com
Where to find kl_php
written by Christine, July 15, 2006
I saw that it disappeared from the Joomla extensions site, but you can still find it on the Mambo forum - the Mambo forum site was just down for a while.

I wrote an email to the developer to find out why it is not on the Joomla site any more - waiting for an answer

Christine
Nice.. but
written by Guest, July 14, 2006
Looks like all links to kl_php have suddenly disappeared. No longer on that forum nor on extentions.joomla.com.

Write comment

security code
Write the displayed characters


busy
 
Subscribe to TM4Y via RSS

OR

Subscribe via Email

Enter your email address:

Delivered by FeedBurner

Syndicate

Syndicate our blog - receive fresh content on your web page on a weekly basis

Twitter Updates

Latest Web Trends Blog Entries

  • Learn Web Development
    Ever wondered what is the difference between HTML and XHTML? If someone asks you to develop a site that is...
  • Joomla Training - Advanced
    We have finally completed the Joomla advanced course and will be running the first course on the 30th of June...
  • SEO Is Dead?
    A month or two ago Google published on their official blog that they have implemented customised search for everyone doing...