Published by Mijingo

movie icon image

ExpressionEngine How-to Articles

Topics range from beginner to advanced, but are all born out of real world, professional, day-to-day use of ExpressionEngine. Need more information? Get training videos and ebooks on ExpressionEngine from Mijingo.

Extending Default Member Templates

ExpressionEngine is often used to manage membership sites, something it excels at. To help you manage member functions on the front end EE includes a comprehensive set of member templates that can be themed. You can of course choose to not use them at all and there are at least a couple of 3rd party member function options.

I want to show you how you can extend the functionality of the membership templates and/or change their default behaviour. We’re going to do that with a very simple extension. This type of extension will work in both EE 1.5.2+ and EE 2.x. The examples will use EE 1.x and the default site but you can find extensions for both EE versions at the end of this article.

Disclaimer: I don’t claim that this is the most useful extension you’ll ever see or that the code I’m going to use is the most efficient. I want to introduce an extension hook that sees little love and maybe provide a couple of ideas for ways to utilise the member templates.

What we’re gonna do

We are going to disallow the viewing of member profiles by guests. If they try and view a profile or the member list we’ll show them a standard template instead. This way we can explain why they can’t see them and offer the chance to register.

So, instead of a guest seeing this:

Not authorized to view profile

Default message template when not authorized to view the member profile.

they will see a nice template instead:

Not allowed to view member profile

Custom message template when not authorized to view the member profile.

First steps

You’ll need to create a template to show them instead of the notification. I am using a separate template group that I’ve named “_member” for my member templates. Inside that group I’m creating a template called ‘not_allowed_to_view’ and for this example I’m just copying the default template code and editing it to show a simple message that guests are not allowed to view member profiles. Now we just need to write a simple extension.

The extension

This isn’t a primer on extensions so I’ll just quote from the EE docs:

Within ExpressionEngine are what is known as ‘hooks’; little snippets of code in over 100 strategic places that allow the calling of third-party scripts. Extensions can do things like modify an entire Control Panel page, add/remove functionality, and modify the appearance of certain page elements. Extensions enable third party developers to modify aspects of ExpressionEngine without hacking the backend scripts.

There is one hook called member_manager and it’s description is “Seize control over any Member Module user side request”. The additional notes for it are “Be careful with this hook. It is simply put God mode for the member module”. Ace. That’s what we like ‘God mode.’

Here is the meat of the extension and it is fairly straightforward. The rest of the extension is fully commented in the download files.

function modify_member_manager($obj)
{
    
global $EXT$IN$SESS$TMPL;

    
$EXT->end_script FALSE;

    
$allowed = array(
        
'login',
        
'forgot_password',
        
'register'
    
);

    if ( ! 
in_array($IN->QSTR$allowed))
    
{
        
if ( ! class_exists('Template') AND ! is_object($TMPL))
        
{
            
require PATH_CORE.'core.template'.EXT;
            
$TMPL = new Template();
        
}

        
switch($SESS->userdata['group_id'])
        
{
            
case '3' :    $EXT->end_script TRUE;
                    
$TMPL->run_template_engine('_member''not_allowed_to_view');
                    echo 
$TMPL->final_template;
                    exit;
                    break;
        
}
    }

Note: I haven’t checked for a previous extension using this hook because we are exiting the script completely and not using of the Member Module after this.

Let’s look at what this does:

global $EXT$IN$SESS$TMPL;

$EXT->end_script FALSE

We need access to a few of the EE globals and also we’re telling EE that it is to continue processing the Member Module code after this. We’ll change that if we detect a visitor in the guest member group.

$allowed = array(
    
'login',
    
'forgot_password',
    
'register'
);

if ( ! 
in_array($IN->QSTR$allowed))

We check that the last segment in the URI is not one of the allowed pages. We still need to allow visitors to access the login, forgot password and registration pages.

if ( ! class_exists('Template') AND ! is_object($TMPL))
{
    
require PATH_CORE.'core.template'.EXT;
    
$TMPL = new Template();

This makes sure that we have access to the Template class. This class would not normally be used for member requests so we have to make sure we can access it.

switch($SESS->userdata['group_id'])
{
    
case '3' :    $EXT->end_script TRUE;
            
$TMPL->run_template_engine('_member''not_allowed_to_view');
            echo 
$TMPL->final_template;
            exit;
            break;

This is the important bit. It checks for the member group of the site user and runs code based upon that member group ID. By default the guest member group is 3. So, if the site visitor’s member group is 3 then this tells the template engine to grab the template we created earlier and output that instead of the default member page. We are also telling EE that it is not going to need to do anything else. In fact the exit statement at the end does that job for us. Remember that the Member Module in EE 1.x uses a different templating engine than the rest of the site and we are going to render a completely normal template so we don’t want anything else to interfere.

That’s all there is to it. You can of course change the member group that this applies to or indeed add others just by replicating the case statement. So, for example, let’s say that we’d created another member group and that group ID was 7 then we could show those users another template by adding this to the switch statement:

case '7'    :    $TMPL->run_template_engine('_member''my_other_template');
        echo 
$TMPL->final_template;
        exit;
        break; 
What else can you do?

Well, how about serving a different profile theme to specific member groups? You can do this easily with this hook. We could replace the code with something like this:

$theme 'default';
switch(
$SESS->userdata['group_id'])
{
    
case '1'    :    $theme 'super_admin';
        break;
    case 
'3'    :    $theme 'guest';
        break;
    case 
'5'    :    $theme 'member';
        break;
    default    : 
$theme 'default';
        break;
}
$obj
->theme_path PATH_MBR_THEMES.$theme.'/profile_theme.php'

What this will do is show a theme called super_admin for Super Admins, a theme called guest for those in the Guest member group and a theme called member for those in the Member group. For all others it will show the default theme. This assumes that you have separate folders called super_admin, guest and member in your themes/profile_themes folder.

What else do you think would be good to do with this hook? Let us know in the comments. Now grab the extensions for EE 1.x and EE 2.x and have a play!

ExpressionEngine 1.6 and 2 Downloads

member_management-2.x.zip

member_management-1.x.zip

Posted on May 04, 2010

Filed Under: How-To, Control Panel, ExpressionEngine Development,

Greg Salt
About Greg Salt

Greg runs Purple Dogfish with @RachaelWyatt, occasionally assisted by Skateboard. He also works part-time for EllisLab as a TSS. You can sometimes find him @drylouvre.