Let's say I need to create a simple page and for my links I want to use JRoute::_() in order to make them SEF or display ID of the current user.

What would be the best way to achieve this? Joomla by default doesn't allow you to put any PHP code inside articles or custom modules. So the Joomla's "right" way seems to be to create installable modules which I can then call inside the article using {loadmodule mymodule}, but what if I want to call JRoute::_() in 10 different links. It seems that I would need to create 10 different modules and call each inside <a href="{loadmodule mymodule!}">link</a> or for simplicity put all content of the article inside that installable module.

Both solutions are bad and that is why I think there are extensions like Sourcer that allow you to put directly PHP code inside articles and modules.

So is using 3rd party extensions the best practice? What are cons and pros? Why Joomla doesn't offer putting directly PHP code natively?


For that, I use NoNumbers Sourcerer: https://www.regularlabs.com/extensions/sourcerer

| improve this answer | |

You can definitely put PHP code in custom modules (this example uses a helper.php file, but you can get as complicated as you need) - are you actually building your own custom module, or are you just overriding a core module's layout from your template's 'html' folder?

If it's the latter, you can still use PHP code by inserting it under defined('_JEXEC') or die; at the start of your html override file.

| improve this answer | |
  • I was not asking if it's possible to put PHP code in custom modules, those you create manually and which you install using extension manager. I was more referring to those custom modules you create in Extensions > Modules > New Module (type: custom module) which doesn't let you put PHP code. – Adam M. Aug 12 '16 at 14:22
  • In that case, yes - it's impossible. Those 'custom modules' are simply for allowing very basic HTML using tinyMCE/whatever editor you're using, which strips out all code beyond safeHTML (you can actually stop them doing this, but you wouldn't want to); they're not designed for making full-fat modules. To achieve what you want you're either going to have to make your own proper module, or use a layout override in your template. – chrBrd Aug 12 '16 at 14:32
  • I know that, but the question is still: What is the best way of using PHP code in Joomla's articles or modules? :) – Adam M. Aug 12 '16 at 14:52
  • I've answered you twice - build a proper module yourself or override the layout files in your template. You cannot do it using the WYSIWYG editor. – chrBrd Aug 12 '16 at 14:56
  • 1
    Because that 'custom modules' section is for people who know maybe a little bit of HTML (or even none at all) to insert a very basic module containing some set links, maybe an image or two, or some text. It isn't designed to be used for actual coding. That's exactly as it should be, for many reasons, which is why it's not even a Joomla thing; stripping out PHP & HTML tags is default behaviour in most WYSIWYG editors (which is all Sourcerer works around). You're right that using Sourcer isn't the best solution, the best solution is using a text editor or IDE and doing things properly. – chrBrd Aug 12 '16 at 15:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.