We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Trying to create an extension… HELP!!! :-)

Development and Programming

Mark Bowen's avatar
Mark Bowen
12,637 posts
17 years ago
Mark Bowen's avatar Mark Bowen

Hi there,

I have read a lot of posts from people wanting a way to redirect users to a configurable URL upon logging out of the EE system. I thought I would give a go at writing an extension to handle this.

I now admit that I have absolutely no idea of what I am doing at all. I tried the code below for an extension but every time I go to the Extensions Manager I just get a completely blank page and there is no source to the page.

<?php

//------------------------------------
//   Logout Redirect Extension
//   using 'member_member_logout' hook
//   author: Mark Bowen
//------------------------------------

if ( ! defined('EXT'))
{
    exit('Invalid file request');
}

class Logout_redirect
{
    var $settings = array();
    var $name = 'Logout Redirect';
    var $classname = 'Logout_redirect';
    var $version = '1.0';
    var $description = 'Provide a simple setting to redirect the user to a URL upon logging out of the system.';
    var $settings_exist = 'y';
    var $docs_url = '';
    
    //------------------------------------
    //   Constructor - Settings
    //------------------------------------
    
    function Logout_redirect($settings='')
    {
        $this->settings = $settings;
    }
    // END
    
    //------------------------------------
    //  Activate Extension
    //------------------------------------
    
    function activate_extension()
    {
        global $DB;
                
        $DB->query($DB->insert_string('exp_extensions',
                array(
                'extension_id'    => '',
                'class'            => $this->classname,
                'method'        => "logout_redirect",
                'hook'            => "member_member_logout",
                'settings'        => $default_settings,
                'priority'        => 10,
                'version'        => $this->version,
                'enabled'        => "y"
                )
            )
        );
    }
    // END
    
    //------------------------------------
    //  Update Extension
    //------------------------------------
    
    function update_extension($current='')
    {
        global $DB;
        
        if ($current == '' OR $current == $this->version)
        {
            return FALSE;
        }
        
        if ($current > '1.0')
        {
            // Update to next version
        }
        
        $DB->query("UPDATE exp_extensions 
                    SET version = '".$DB->escape_str($this->version)."' 
                    WHERE class = '$this->classname'");
    }
    // END
        
    //------------------------------------
    //  Extension Settings
    //------------------------------------
    
    function settings()
    {
        $settings = array();
        
        $settings['redirect_url']    = "/contact/";
    
        return $settings;
    }
    // END
    
    //------------------------------------
    //   Logout Redirect Function
    //------------------------------------
    
    function logout_redirect()
    {
         $FNS->redirect("http://www.yahoo.com");
    }
    // END
    
}
?>

I tried commenting out the last part of the extension where it has my function :

//------------------------------------
    //   Logout Redirect Function
    //------------------------------------
    
//    function logout_redirect()
//    {
//         $FNS->redirect("http://www.yahoo.com");
//    }
    // END

and this allows the extension to be enabled and the setting to be set but if the function is un-commented then it all goes hay-wire!!

I was just wondering if anyone could possibly give me a push in the right direction to get this working? Also how would I go about using a language file to hold the redirect_url setting instead of having it directly inside the extension file?

One last question. I was also wondering if it is possible to have a plug-in that somehow references this so that when the {path=LOGOUT} link is used in a template it could perhaps send the redirect_url setting to it instead of using the setting in the extension so that if you want you can have different redirects depending upon which logout link on which page you click.

If anyone has any idea as to what I am babbling on about and could possibly lend me a push or shove in the right direction to get this all up and working then I would really appreciate it. I would rather not have the answer given to me as I would love to say that I managed to make this myself but any help would be massively appreciated.

Thanks in advance for any ideas.

Best wishes,

Mark

       
Brian M.'s avatar
Brian M.
529 posts
17 years ago
Brian M.'s avatar Brian M.

If there’s no source at all I’d assume you’re throwing a PHP error - is error display turned on in the system prefs?

It looks like you’re declaring the logout_redirect function twice - one is capitalized and one isn’t. I would guess that even though one is caps and one isn’t that’s a problem? Try renaming that last function to something else and putting that as your method in the activate_extension function?

       
Greg Salt's avatar
Greg Salt
3,988 posts
17 years ago
Greg Salt's avatar Greg Salt

Hi Mark,

In addition Brian’s comments one other thing looks wrong to me. In your function logout_redirect (the last one that you are going to rename) you must provide access to the redirect method by using ‘global $FNS’.

Cheers

Dry

       
Mark Bowen's avatar
Mark Bowen
12,637 posts
17 years ago
Mark Bowen's avatar Mark Bowen

Hiya,

Thanks for looking into this for me.

I have now changed the code to the code below :

<?php

//------------------------------------
//   Logout Redirect Extension
//   using 'member_member_logout' hook
//   author: Mark Bowen
//------------------------------------

if ( ! defined('EXT'))
{
    exit('Invalid file request');
}

class Logout_redirect
{
    var $settings = array();
    var $name = 'Logout Redirect';
    var $classname = 'Logout_redirect';
    var $version = '1.0';
    var $description = 'Provide a simple setting to redirect the user to a URL upon logging out of the system.';
    var $settings_exist = 'y';
    var $docs_url = '';
    
    //------------------------------------
    //   Constructor - Settings
    //------------------------------------
    
    function Logout_redirect($settings='')
    {
        $this->settings = $settings;
    }
    // END
    
    //------------------------------------
    //  Activate Extension
    //------------------------------------
    
    function activate_extension()
    {
        global $DB;
                
        $DB->query($DB->insert_string('exp_extensions',
                array(
                'extension_id'    => '',
                'class'            => $this->classname,
                'method'        => "redirect_upon_logout",
                'hook'            => "member_member_logout",
                'settings'        => $default_settings,
                'priority'        => 10,
                'version'        => $this->version,
                'enabled'        => "y"
                )
            )
        );
    }
    // END
    
    //------------------------------------
    //  Update Extension
    //------------------------------------
    
    function update_extension($current='')
    {
        global $DB;
        
        if ($current == '' OR $current == $this->version)
        {
            return FALSE;
        }
        
        if ($current > '1.0')
        {
            // Update to next version
        }
        
        $DB->query("UPDATE exp_extensions 
                    SET version = '".$DB->escape_str($this->version)."' 
                    WHERE class = '$this->classname'");
    }
    // END
        
    //------------------------------------
    //  Extension Settings
    //------------------------------------
    
    function settings()
    {
        $settings = array();
        
        $settings['redirect_url']    = "/contact/";
    
        return $settings;
    }
    // END
    
    //------------------------------------
    //   Logout Redirect Function
    //------------------------------------
    
    function redirect_upon_logout()
    {
        global $FNS;
         $FNS->redirect("http://www.yahoo.com");
    }
    // END
    
}
?>

This now allows the extension to be enabled but when I click on a standard {path=LOGOUT} link nothing special happens. The system just logs me out as expected but I never get re-directed or anything?

Any more ideas?

Thanks for all the help on this.

Best wishes,

Mark

       
Brian M.'s avatar
Brian M.
529 posts
17 years ago
Brian M.'s avatar Brian M.

You should probably look at the hook itself to be sure, but from the docs, member_member_logout happens after the member is logged out. Sounds like it happens after they’ve already been redirected? Just a thought.

       
Brian M.'s avatar
Brian M.
529 posts
17 years ago
Brian M.'s avatar Brian M.

What happens if you change the function to this?

function redirect_upon_logout()
    {
        global $FNS, $EXT;
       $EXT->end_script = true;
         $FNS->redirect("http://www.yahoo.com");
    }

EDIT to add - it actually happens before the output message is created so that’s not the issue - what happens with the above?

       
Mark Bowen's avatar
Mark Bowen
12,637 posts
17 years ago
Mark Bowen's avatar Mark Bowen

Hi Brian,

Still the same unfortunately. Just logs out as per normal. Just out of interest if this isn’t meant to work as the hook happens after they have logged out then any ideas what use the hook is?

Thanks.

Best wishes,

Mark

       
Brian M.'s avatar
Brian M.
529 posts
17 years ago
Brian M.'s avatar Brian M.

It does actually happen before the logout page happens - so you should be able to use this for what you’re trying to do. I seem to remember having problems redirecting someone when I was building an extension if I’m not mistaken - I’ll take a quick look through some of the extensions I’ve built to see if I can job my memory.

       
Brian M.'s avatar
Brian M.
529 posts
17 years ago
Brian M.'s avatar Brian M.

Just out of curiosity - does this do anything?

function redirect_upon_logout()
    {
         global $FNS, $EXT;
         $FNS->redirect("http://www.yahoo.com");
         $EXT->end_script;
    }

Setting $EXT->end_script = true should work. Check out what it says in the dev docs:

$EXT->end_script Many extension hooks exist for the express purpose of totally controlling a page or script in the Control Panel. They are meant for redesigning the appearance of a form or perhaps usurping a script for processing form data. In those instances you want your extension to be the last thing called for that extension hook so that nothing else is processed after that point. The $EXT->end_script exists solely for that purpose. If you set this value to TRUE, then once your extension is done being processed the execution of the hook is finished, as is the script that the extension hook is contained within.

The hook is before the place where the logout output is generated and displayed - by setting it to true you shouldn’t ever see that interim page.

EDIT to add - it shouldn’t make a difference, but what if you use all caps for TRUE? $EXT->end_script = TRUE;

EDIT again - try doing something else besides redirecting - can you get $EXT->end_script to work at all? It could potentially be a bug?

       
Mark Bowen's avatar
Mark Bowen
12,637 posts
17 years ago
Mark Bowen's avatar Mark Bowen

Hi Brian,

Have now changed it to :

<?php

//------------------------------------
//   Logout Redirect Extension
//   using 'member_member_logout' hook
//   author: Mark Bowen
//------------------------------------

if ( ! defined('EXT'))
{
    exit('Invalid file request');
}

class Logout_redirect
{
    var $settings = array();
    var $name = 'Logout Redirect';
    var $classname = 'Logout_redirect';
    var $version = '1.0';
    var $description = 'Provide a simple setting to redirect the user to a URL upon logging out of the system.';
    var $settings_exist = 'y';
    var $docs_url = '';
    
    //------------------------------------
    //   Constructor - Settings
    //------------------------------------
    
    function Logout_redirect($settings='')
    {
        $this->settings = $settings;
    }
    // END
    
    //------------------------------------
    //  Activate Extension
    //------------------------------------
    
    function activate_extension()
    {
        global $DB;
                
        $DB->query($DB->insert_string('exp_extensions',
                array(
                'extension_id'    => '',
                'class'            => $this->classname,
                'method'        => "redirect_upon_logout",
                'hook'            => "member_member_logout",
                'settings'        => $default_settings,
                'priority'        => 10,
                'version'        => $this->version,
                'enabled'        => "y"
                )
            )
        );
    }
    // END
    
    //------------------------------------
    //  Update Extension
    //------------------------------------
    
    function update_extension($current='')
    {
        global $DB;
        
        if ($current == '' OR $current == $this->version)
        {
            return FALSE;
        }
        
        if ($current > '1.0')
        {
            // Update to next version
        }
        
        $DB->query("UPDATE exp_extensions 
                    SET version = '".$DB->escape_str($this->version)."' 
                    WHERE class = '$this->classname'");
    }
    // END
        
    // --------------------------------
//  Settings
// --------------------------------  

function settings()
{
    $settings = array();
    
    $settings['redirect_url']    = "";
    
    // Complex:
    // [variable_name] => array(type, values, default value)
    // variable_name => short name for setting and used as the key for language file variable
    // type:  t - textarea, r - radio buttons, s - select, ms - multiselect, f - function calls
    // values:  can be array (r, s, ms), string (t), function name (f)
    // default:  name of array member, string, nothing
    //
    // Simple:
    // [variable_name] => 'Butter'
    // Text input, with 'Butter' as the default.
    
    return $settings;
}
// END


    //------------------------------------
    //   Logout Redirect Function
    //------------------------------------
    
   function redirect_upon_logout()
    {
         global $FNS, $EXT;
         $FNS->redirect("http://www.yahoo.com");
         $EXT->end_script = TRUE;
    }
   // END
    
}
?>

and it does actually work!! 😊

All I need to figure out now is how I go about using the setting that is in the settings panel as the $FNS->redirect(); part instead.

I have tried loads of things now but just can’t seem to get the URL that is in the settings part of the extension in the EE admin to be used instead of my hard-coding it in to the extension.

Any more help would be totally appreciated.

This has turned into a real learning experience for me!!

Best wishes,

Mark

       
Mark Bowen's avatar
Mark Bowen
12,637 posts
17 years ago
Mark Bowen's avatar Mark Bowen

Oops never-mind!!

Have it working now!! 😊

I changed the function to this :

//------------------------------------
    //   Logout Redirect Function
    //------------------------------------
    
   function redirect_upon_logout()
    {
         global $FNS, $EXT;
         $FNS->redirect($this->settings['redirect_url']);
         $EXT->end_script = TRUE;
    }
   // END

and it works now!!

Will probably post this into the extensions forum and ask everyone if there are any glaring coding problems and whether or not it adheres to the EE coding guidelines. Hopefully it does?!!

Thanks again for all your help on this you will definitely be getting the credit on this one!!

Best wishes,

Mark

       
Mark Bowen's avatar
Mark Bowen
12,637 posts
17 years ago
Mark Bowen's avatar Mark Bowen

Hiya,

Just to let you know that I have now placed this into the Extensions forum over here.

Thanks again for all the help.

Best wishes,

Mark

       
Brian M.'s avatar
Brian M.
529 posts
17 years ago
Brian M.'s avatar Brian M.

Hey Mark - glad you got it working. I’ve got a feature request for your new extension, but I’ll add that to the other thread 😉

       
Jamie Poitra's avatar
Jamie Poitra
409 posts
17 years ago
Jamie Poitra's avatar Jamie Poitra

Gosh we have nice helpful people here. Sorry I seemed to have missed this thread while going through the forum yesterday. Glad you got it all taken care of.

Jamie

       
Mark Bowen's avatar
Mark Bowen
12,637 posts
17 years ago
Mark Bowen's avatar Mark Bowen

Hi Jamie,

No problem with missing the post as your ideas in the other post have pushed my brain into gear. I thought of something a bit like what you are saying and I am pretty sure that I can get this working.

Will keep you all up to date.

Best wishes,

Mark

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.