News:

Want to get involved in developing SMF, then why not lend a hand on our github!

Main Menu

Guests Only Block

Started by shaythong8, August 11, 2009, 02:38:54 AM

Previous topic - Next topic

shaythong8

Hi I want to manually create a block on my theme but I just don't know how to make it guest only. On MyBB I was able to use "<if $GLOBALS['mybb']->user['usergroup'] == 1 then></if>" with a plugin for it. Is there a similar syntax for SMF?

JBlaze

$context['user']['is_guest']

OR

$user_info['is_guest']
Jason Clemons
Former Team Member 2009 - 2012

shaythong8

Thank you JBlaze. The guest block is now working 100%. :D

JBlaze

I'll go ahead and mark this solved then ;D
Jason Clemons
Former Team Member 2009 - 2012

shaythong8

How do I make the code to display on all pages except the registration page? Is it possible?

Arantor

Where is the code and what have you used exactly?

shaythong8

This is what I'm using in the header of index.template.php:

if ( $context['user']['is_guest'] ) {
echo '<div class="categoryframe tborder" style="margin: 0 auto;">
<h3 class="catbg headerpadding">Welcome</h3>
<table cellspacing="1" class="bordercolor boardsframe">
   <tr>
      <td class="windowbg" style="padding: 5px;">
Join the community today and you will be able to post and have your own profile!
      </td>
   </tr>
</table></div>';
}

Arantor

Change the first line from:

if ( $context['user']['is_guest'] ) {

To:
if ( $context['user']['is_guest'] && (empty($context['current_action']) || $context['current_action'] != 'register' ) ) {

This says if we're a guest, and (we have no current page as we're on the front page, or we're on a subpage that isn't register), display the block.

shaythong8

Thank you it works. I'm not exactly sure what you mean by "we have no current page as we're on the front page".

Arantor

It works by examining the current action. If you're on the main page, there is no current action because the current page is just 'index.php', not 'index.php?action=something', thus in my offhand way 'we have no current page'.

That's what the check is doing - the page didn't specify a current action.

shaythong8

So that basically means that the code makes it so it displays on the index page or does this just set it so that it makes the next part of the code hide the block on the registration page?

Arantor

It doesn't set anything.

The first check is whether the user is a guest, if false, it just skips over the entire block.

Assuming the user is a guest, we then look at the condition.

The situation is thus: the user is either on: 1) the registration page, 2) another page where action=something, or 3) the front page.

So if the user is on the front page (no action has been defined because the URL is simply index.php) or they're on another page (index.php?action=something), we might be displaying the block. We just need to check that the page they're on isn't the registration page.

Here's the relevant code.
if ( $context['user']['is_guest'] && (empty($context['current_action']) || $context['current_action'] != 'register' ) ) {

To write that line of code out in full English:
If the user is a guest, and (either the user is on index.php, or they're on index.php?action=something but something isn't equal to 'register') do this bunch of code

Advertisement: