A phpBB 3 iPhone Style Theme With Option to Disable

phpBB iPhone theme style


phpBB iPhone theme styleA forum that I am an adminstrator for has been clamoring for an iPhone theme (style) for a long time now. In the past, I hadn’t seen any usable iPhone template for phpBB3, until now.

The theme is hosted on Google code and is named phpbb-iphone-style. It is downloadable here and was last updated June 18th, as of today.

The theme works wonderfully on the iPhone. However, the issue that I ran into is automatically displaying it for mobile browsers, such as the iPhone, Android and other platforms.

Fortunately there is a modification you can make to the phpbb code which is effectively a theme switcher for mobile browsers. The how to is located within the download file for the theme above.

What if a user wants to turn off the mobile style?

I modified the code to allow a user to set an option on their profile to permentently disable the theme when logged in on a mobile browser.

First, install the style as described above. As part of the switcher, you need to find out the theme ID. You can do so, after installing the style, by hovering over the ‘Detail’ tab in the ACP (Administrator Control Panel) and looking for the ‘id’ variable. In the example below it is ‘6’.

Once the theme is installed, move over to the “Users and Groups” tab, and then the “Custom Profile Fields” area. At the bottom of this page there is a box to add a new profile field. Type ‘disable_mobile’ as the name, and Boolean (Yes/No) as the type and click add.

For the options, I selected:

  • Publicly display profile field: No
  • Display in user control panel: Checked
  • Display on registration screen: Unchecked
  • Display on viewtopic screen: Unchecked
  • Required field: Unchecked
  • Hide profile field: Unchecked
  • Field name/title presented to the user: Disable Mobile Browser
  • Field description: When viewing on a mobile device (iPhone, Android, etc), clicking ‘yes’ will disable the mobile browser.
  • Entries: First option: Yes, Second option: No

On the second screen select the following items:

  • Field type: Radio Buttons
  • Default value: no

Save your custom profile field.

It will end up looking like this in the profile control panel of the end user:

phpbb iphone style disabler

Now the fun part, adding the code to your install to select which mobile browser to use automatically.

Back up, then open install dir/includes/session.php

Around line 1468 in the function setup

1468
1469
1470
1471
1472
        /**
        * Setup basic user-specific items (style, language, ...)
        */
        function setup($lang_set = false, $style = false)
        {

Replace

1
global $db, $template, $config, $auth, $phpEx, $phpbb_root_path, $cache;

with

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
global $db, $template, $config, $auth, $phpEx, $phpbb_root_path, $cache, $user;


//-----Begin phone detection & redirection code-----

                $user->get_profile_fields( $user->data['user_id'] );
                $user_fields = $user->profile_fields;

                // if (!($user_fields['pf_disable_mobile'] == 1)) {  
                // Thanks to Chris Dembek for this code fix
                if (isset($user_fields['pf_disable_mobile']) && !($user_fields['pf_disable_mobile'] == 1))

                        //id of the iphone/mobile theme - SELECT THIS FROM YOUR STYLES
                        $mobilestyleid = 6;

                        //Fetch the users browser
                        $user_browser = strtolower($this->browser);

                        //List of mobile user-agent keywords
                        $browsers_array = array('240x320', '320x240','blackberry', 'iemobile', 'minimobile', 'mobile', 'opera mini', 'pda', 'phone', 'pocket', 'psp', 'symbian', 't-shark', 'wireless');

                        //Check for the user-agent in the list of mobile user-agents
                        foreach ($browsers_array as $ua_match) {
                                if (strpos($user_browser, $ua_match) !== false) {  //a match
                                        $style = $mobilestyleid;
                                        $this->data['is_mobile'] = true;
                                        break;
                                }
                        }

                }

                //-----End phone detection/redirection code-----

Make sure to replace $mobilestyleid = 6 with your style id number!

Save your file. Test out the modifications on your phone, you should be presented with the mobile version on reload (make sure to purge any cache if you don’t see it right away).

Finally go into your profile and select the ‘disable mobile browser’ option and make sure it reverts back to your regular default theme.

Let me know if you have any problems implementing this but it has worked great for this phpbb forum!

  • Chris Dembek

    If this is not working for you change this

    if (!($user_fields[‘pf_disable_mobile’] == 1)) {

    to this…

    if (isset($user_fields[‘pf_disable_mobile’]) && !($user_fields[‘pf_disable_mobile’] == 1))

  • Thanks for the fix Chris. I fixed above and credited you. Have you successfully used this code to implement an iPhone theme?

  • Baraka3000

    How do we add an option to disable mobile theme from the main menu instead of having to go into the UCP/profile?

  • The best way to do this might be to add additional logic into the code to have it check for a cookie. If that cookie says to disable the mobile browser, then it displays the current one. It is not anything in the current source code but since it is all open source it wouldn’t be that hard to do as long as you know PHP.

  • Dave Townsend

    I tried this but had to make a few fixes … on line 7 you have a > instead of a >

    Also, on line 11 you need to finish with a {

    Apart from that, seems to work well

    Thanks!

  • Dave Townsend

    Actually, still didnt work. IF you can check and revise that would be good :)

  • Baraka3000

    It would be sweet if someone could add a PM button :)

  • hahgeh

    Tried to include and user_field integration works fine, but not the parsing of $user_fields = $user->profile_fields; which gives me parsing error due to unexpected ‘&’
    Any ideas?

  • Try this again – it was a typo due to WP conversion. The line should be $user_fields = $user->profile_fields; the line above had an & which is why it threw that error for you.

  • Cyrus_xi

    Parse error: syntax error, unexpected T_IF, expecting T_FUNCTION in /home/content/n/i/n/nineclicks/html/forum/includes/session.php on line 1562

    this is what im getting

  • No

     Yup… this is what I am getting too.  Shame, because this is exactly what I want.

  • Sjbats

    I uploaded the style and it seems to work fine.  My friend made one change:

    if (!isset($user_fields[‘pf_disable_mobile’]) || ($user_fields[‘pf_disable_mobile’] != 1)) 
    instead of:

    if (isset($user_fields[‘pf_disable_mobile’]) && !($user_fields[‘pf_disable_mobile’] == 1))

    What difference will this make?

  • The difference is essentially: 

    If the user field pf_disable_mobile is not set OR user field pf_disable_mobile is not equal to 1
    and
    If the user field pf_disable_mobile is set AND user field pf_disable_mobile does not equal 1.  

    I wrote pf_disable_mobile to allow end users to disable the mobile theme if they want. My line will only display the mobile theme if it does not equal 1, and if it is actually set; the one your friend wrote will display the theme if it is not set or if the field does not equal one. Therefore they are logically equivalent. 

  • Sjbats

    Thanks for the quick reply!

    My friend’s reasoning was: “I changed it to an OR and added some !’s so that if pf_disable_mobile was undefined it would still do the override. In the other configuration, if pf_disable_mobile was never set to anything it wouldn’t override the style.”

    Ultimately, it’s all the same end result.

  • Harkrishan

    hi i keeping getting this error please help

    HTTP Error 500 (Internal Server Error): An unexpected condition was encountered while the server was attempting to fulfil the request.

    all i have done is installed which was fine but then when i changed the code this happened

  • Sjbats

    This seems to be working great, but is there anyway to automatically disable this on the iPad?