1

I want use onContentAfterSave in Joomla backend but it's not working.
My code this is:

class plgContentTest extends JPlugin
{

public function onContentAfterSave($context, &$article, $isNew)
{
    $option = JRequest::getVar('option');

    if($option=='com_content'){

        echo 'Test';

     }

   }

}

onContentAfterSave isn't working on backend?

2

If you are testing on save, then echo'ing out something may never be seen as almost certainly a redirect is triggered after the save. So try adding an exit; to double check that it really isn't firing :

class plgContentTest extends JPlugin
{
    /**
    * Loads the application object
    *
    * @var    JApplicationCms
    * @since  3.2
     */
    protected $app = null;

    public function onContentAfterSave($context, &$article, $isNew)
    {
        $option = $this->app->input->get('option');

        if ($option=='com_content')
    {
            echo 'Test';
            exit;
        }

    }

}

Note that JRequest is deprecated and you can now inject in the Joomla application object by declaring it as a protected property.

  • For me using 3.4.7, it only works when the $article is passed by value ($article) and not by reference (&$article) like mentioned in the below answer. – sifu Mar 21 '16 at 22:55
2

Check following things:

  • Plugin class name should be: plgContentTest
  • Plugin class should extend JPlugin class
  • Plugin filename should be: test.php
  • Plugin file should be in: /plugins/content/test
  • Plugin xml definition filename should be: test.xml
  • Plugin xml definition file should be in: /plugins/content/test
  • Plugin should be installed and enabled

Few other tips:

You should check context, not a direct request:

if ($context != 'com_content.form') {
    return true;
}

You can find sample content plugin here: /plugins/content/joomla

You should define article variable like this $article not this &$article.

Also it would be nice for you to provide directory structure of your plugin.

1

Most problems with Content plugins are caused by the plugin name. If it is not correctly named and installed, Joomla cannot find it and call onContentAfterSave.

Taking finder plugin as example:

  • It is installed in plugins/content/finder folder.
  • Plugin is named finder.php
  • The class is PlgContentFinder (extends JPlugin)
  • It has onContentAfterSave function

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.