4

I need to create a plugin that displays a popup when an user makes a log in on my Joomla 3.6.4 website. I know that I need to create a Plugin and trigger the onUserAfterLogin event, but I don't know how to create HTML and Javascript from inside a Plugin. So, my popuplogin.xml is:

    <?xml version="1.0" encoding="utf-8"?>
<extension version="3.6" type="plugin" group="user" method="upgrade">
        <name>Popuplogin</name>
        <author>XX</author>
        <creationDate>08/03/2018</creationDate>
        <copyright>2018</copyright>
        <license>GNU General Public License</license>
        <authorEmail>XXX</authorEmail>
        <authorUrl>XXX</authorUrl>
        <version>1.0</version>
        <description>Plugin to display a modal after UserLogin</description>
        <files>
                <filename plugin="popuplogin">popuplogin.php</filename>
                <filename>index.html</filename>
        </files>
</extension>

And my popuplogin.php is:

    <?php

// no direct access
defined('_JEXEC') or die;


class PlgUserPopuplogin extends JPlugin
{
    public function onUserAfterLogin($options)
        {
              return 'I think I need to place Javascript/HTML here';
 }
}

?>

With these files, nothing happens, even if I place a simple <p>I am the login popup</p> inside return. Can you please explain to me how can I properly display a message using a Plugin in Joomla?

  • Silly question, but have you enabled the plugin? – Lodder Mar 8 '18 at 12:21
  • 1
    silly question, but why are you still on J 3.6 when updating to current 3.8 is so easy and painless? – Simba Mar 8 '18 at 12:26
4

The return value of onUserAfterLogin() should be a boolean -- ie true or false. The idea is that you can use this event handler to do additional validation on the login, so if you return false, it indicates that the login should be rejected.

What you want to do is quite different; you're trying to add content to the page after login. There's several ways of doing this.

  • Option 1: If you're not worried about a modal, you could just use Joomla's message system to tell the user something:

    JError::raiseNotice(100, "Put whatever message you want to tell the user here.");
    
  • Option 2: Use an extension like Regular Labs Modals, so that you can pop up a modal alert with minimum code. You'd probably not need much more than a single line in your existing plugin to call the modal with the message you want to show.

  • Option 3: Write the modal yourself using JavaScript, and inject it into the page using addScript() or addScriptDeclaration(), as described in the Joomla documentation here: https://docs.joomla.org/Adding_JavaScript

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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