Back to Programming Articles

Simplify your Template Conditionals
by kall 06 Dec 2004

If you want to be able to show/hide things in your templates based on Usergroups, you can either use something like:

Code:
<if condition="$bbuserinfo['usergroupid'] == '9' || $bbuserinfo['usergroupid'] == '10' || $bbuserinfo['usergroupid'] == '11'">
or

Code:
<if condition="in_array($bbuserinfo[usergroupid], array(9,10,11))">
...but if those usergroup ids ever change, or you want to add more groups to the array, it could get time consuming, especially if you have multiple styles.

Wouldn't it be easier to be able to use:

Code:
<if condition="is_paid_member()">
?

Well now you can!

In includes/functions.php, find:
PHP Code:
// ###################### Start build datastore #######################
function build_datastore($title ''$data '')
{
    global 
$DB_site;

    if (
$title != '')
    {
        
$DB_site->query("
            REPLACE INTO " 
TABLE_PREFIX "datastore
                (title, data)
            VALUES
                ('" 
addslashes(trim($title)) . "', '" addslashes(trim($data)) . "')
        "
);
    }

After, add
PHP Code:
function is_paid_member($usergroupid 0// replace is_paid_member with your new function/condition
{
    global 
$bbuserinfo;

    if (
$usergroupid == 0)
    {
        
$usergroupid $bbuserinfo['usergroupid'];
    }
    return 
in_array($usergroupid, array(91011));  // replace with the Usergroup ids you want

In adminfunctions_template.php find:

PHP Code:
// vBulletin-defined functions
            
'can_moderate',          // obvious one
            
'can_moderate_calendar'// another obvious one
            
'exec_switch_bg',        // harmless function that we use sometimes
            
'is_browser',            // function to detect browser and versions
            
'is_member_of',          // function to check if $user is member of $usergroupid 
under, add:

PHP Code:
'is_paid_member',          // function to check if $user is member of paid member groups 

Save and upload includes/functions.php and adminfunctions_template.php and you are done.

You can now wrap
Code:
<if condition="is_paid_member()"></if>
around whatever you want to show to people in Usergroups 9, 10 or 11...and if you need to add usergroup 12 to the list, all you need to do is add it in the function.

Hoorah!

vblts.ru supports vBulletin®, 2022-2024