Support » Plugin: WP-United : phpBB WordPress Integration » Using $user and $db on integrated website

  • Resolved Vikestart

    (@vikestart)


    I wish to use $user and $db and other various phpBB info to check for conditions, such as if the visitor is logged in or not, or their custom profile field settings, etc.

    if($user->data['is_registered']):

    Unfortunately this doesn’t seem to work with WP-United. The information is not accessible and if I try to include common.php, I get fatal errors because it has already been included by WP-United despite the information not being accessible.

    How can this be solved?

    https://wordpress.org/plugins/wp-united/

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author jhong

    (@jhong)

    Sure, have a look at the documentation on the context switcher at the bottom of wp-united.php

    In particular, this part:

    * – You **CAN NOT, MUST NOT EVER** make calls to phpBB code from WordPress without wrapping your code in calls
    * to the WP-United context switcher, asking it to change state. Note also that most common phpBB functions are already provided
    * as an API in WP-United’s $phpbbForum object. Use them! There are plenty of examples.
    * – If you need some custom phpBB code, then use the WP-United context switcher as follows:
    * global $phpbbForum;
    * $phpbbForum->foreground();
    * // I can call phpBB code in here, e.g. use the global $user, $db and $auth objects
    * // I can’t use WordPress code in here — e.g. $wpdb->do_something() will fail
    * $phpbbForum->background();
    * // Now I can call WordPress code, but not phpBB code — $user, $db, $config, $auth, $template, and a bunch of others, are unavailable now
    *
    * – Using the context switcher as in the above example is all well and good, but it is a bad example. It assumes that you
    * know the current state when you call foreground(). In reality, you should **NEVER** assume the current state –
    * your function could be called from anywhere, and tracking the state throughout a large application like this is impossible.
    * Rather than having to figure out the state yourself, you can use the context switcher in the following way:
    * $changedToken = $phpbbForum->foreground();
    * // my phpBB code here
    * $phpbbForum->restore_state($changedToken);
    *
    * Now you can go ahead and write mutiple functions, even nested, and not worry about the phpBB state! Nice, right?
    *
    * However, bear in mind the following:
    * – Context switching is computationally expensive. Group your phpBB calls together if you can.
    * – It is *your* responsibility to leave the context in the state you found it! That means you can’t just return a
    * result from your function without restoring the state first. A calling function could be royally f***ed up if it calls your function,
    * and you return after having pulled the current state out from the caller’s feet.
    */

    Thread Starter Vikestart

    (@vikestart)

    That seems nice, I’ll try that you 🙂

    Is it not possible to translate the phpBB global variables $db, $auth, $user etc. into variables with new names so that I don’t have to constantly use the context switcher?

    Translate them into $phpbb_db, $phpbb_auth and $phpbb_user?

    Plugin Author jhong

    (@jhong)

    That wouldn’t work as plugins / mods (and indeed some core code) expect the global vars to exist in the normal locations. So we have to switch them around using the context switcher anyway, and make sure they never tread on each other.

    There’s a bit more to it than just the variable name collisions though. For example, depending on people’s setup, the mysql database connection might be pointing to the wrong database. Or, depending on the object you call, it might spawn a globally namespaced function that collides between phpBB and WordPress. Being very clear about which context we are in helps to solve these problems.

    Don’t worry too much about the performance cost — just worth bearing in mind if you use the context switcher a lot.

    Thread Starter Vikestart

    (@vikestart)

    Thanks, I got it working! 🙂

    This plugin is fantastic, I hope you’ll continue to update it 🙂 Thanks for all your hard work!

    Thread Starter Vikestart

    (@vikestart)

    Forgot to mark as resolved.

    Plugin Author jhong

    (@jhong)

    Great, glad it is working OK for you.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Using $user and $db on integrated website’ is closed to new replies.