简体   繁体   中英

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.

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.

Plugin events should be triggered somehow. Usually they are triggered during routing and dispatching of the app. You can trigger any plugin methods manually using this code:

JPluginHelper::importPlugin('system'); // Load your plug-in group
$dispatcher = JDispatcher::getInstance(); // USe JEventDispatcher for 3.x
$results = $dispatcher->trigger('onAfterInitialise'); // Trigger your custom event

More information: https://docs.joomla.org/Supporting_plugins_in_your_component

Based on your comment I think your question is really "How do I get an AJAX response without surrounding Joomla template HTML".

In this case you may want to read:

For 1.5

For 1.6+

Just add at the end of your URL in your ajax call: &format=raw

Content will only be loaded without the rest!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM