1

I am new to Joomla and I am making a testing website to learn more.For the site I have created, I wish to create custom php scripts, that are to be called mainly from javascript elements inside the browser pages and return specific results according to user information.

So, for starters, I created a php script inside the Joomla site parent directory, and random prints worked. Then, I tried to make use of some of Joomla libraries' elements.

Despite my final aim,since I ran into problems, I tried to make something simple work, such as printing the site name:

<?php $config = JFactory::getConfig();
echo 'Site name is ' . $config->get( 'sitename' ); ?>  

This is the error I get:

Fatal error: Class 'JFactory' not found in /home/ubuntu/workspace/JP-27/testphp.php on line 1

Generally, whenever I try to use any element of Joomla libraries, the elements are not found. Any advice?

1 Answer 1

3

Custom scripts that aren't associated with a Joomla extension or the core require you to import the Joomla API.

You can do this by adding the following to the top of your custom PHP file:

define( '_JEXEC', 1 );
define('JPATH_BASE', '../');
require_once ( JPATH_BASE .'/includes/defines.php' );
require_once ( JPATH_BASE .'/includes/framework.php' );
$app = JFactory::getApplication('site');

Please note you may have to change define('JPATH_BASE', '../'); on the second line, depending on where your custom PHP is located in relation to the root of your Joomla site.

Once imported correctly, your won't get the error you mentioned.

Hope this helps

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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