Search code examples
phpjoomla

How to use Joomla plugin inside PHP script outside Joomla Framework


I have a PHP script that is executed by accessing it directly (this is for AJAX output).

I am initializing Joomla Framework variables inside that script this way:

if ($JEXEC_defined==TRUE) {
    defined('_JEXEC') OR defined('_VALID_MOS') OR die( 'Restricted access' ); //security reason
    $direct_script_access=FALSE;
}

if ($JEXEC_defined==FALSE) {
    define( '_JEXEC', 1 );
    define( 'DS', DIRECTORY_SEPARATOR );
    define('JPATH_BASE', dirname(__FILE__) );
    require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
    require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
    $direct_script_access=TRUE;

    // initialize the application 
    $mainframe =& JFactory::getApplication('site');
    $mainframe->initialise();
}


if ($user->username!="") if ($direct_script_access==TRUE) {
    //PHP code when script is accessed directly
}

As an output of the script when accessed directly I need to display a Joomla Plugin, for ex:

{valsimplecalendar SRQCMPDT1 }

But instead of displaying the content of the plugin, I get a flat text "{valsimplecalendar SRQCMPDT1 }".

My question: how to initialize plugin system when calling PHP directly?

Edit

I searched the web and found that I need to import the Joomla Plugins:

JPluginHelper::importPlugin('content');
$dispatcher = &JDispatcher::getInstance();
$dispatcher->trigger('onBeforeDisplayContent', array ( & $category, &$params, $limitstart));

But anyway that doesn't make to display plugin content when directly calling PHP script.


Solution

  • This might not be what you are looking for, but I had the same problem with another Joomla plugin called FlashChart Plugin.

    The plugin call looks like this:

    {flashchart data="10,20,15,30|40,50,12,14"}Samplechart{/flashchart}

    So I thought I could run a PHP script using a PHP plugin, calculate the data and just echo out the above string. Wrong! The curly bracket thing is preprocessed by Joomla and when you run PHP you bypass the preprocessor.

    My solution was to use the same code that the plugin used to create the charts I wanted. I basically bypassed the plugin preprocessor and did the same thing that the plugin would have done.... which was to output the necessary code to create one of the plugin's flash charts.

    I was lucky though because FlashChart Plugin is very object oriented and creating the charts and outputting the swfobject code was easy.

    In short, you may have to look in the plugin code and see what it's doing and do the same thing in your PHP code.

    Good luck.