Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
+6 votes
12.4k views
in Q2A Core by
I would like to know if it is possible to  integrate this script  with wordpress -joomla users database  .
by
Even I would like to know how to integrate with Vbulletin forum.

2 Answers

+1 vote
by
You can do this with the single sign-on integration option, but it requires manual coding at this time. On the roadmap is a plan to provide built-in integrations with several content management systems, so please be patient!
+4 votes
by
edited by
It's actually quite easy to code the single sign-on for WordPress. Use the instructions at http://www.question2answer.org/advanced.php

A) Ignore step 3 - use the WordPress database settings from wp-config.php in step 5.

B) In step 7, change null to to 'BIGINT UNSIGNED' (including quotes).

C) Check all is OK down to step 10. You will not have any users until you change the file at step 11:

D) Near the top of qa-external-users.php (e.g line 2 - must be after <?php ), insert:
include_once('/home/server/public_html/yoursitedirectory/wp-load.php');

Change this to match your server. This also works if you put it in the qa-config file instead.

E) About line 118, change the qa_get_login_links function to:
        return array(
            'login' => $relative_url_prefix.'../wp-login.php?redirect='.urlencode('question2answer/'.$redirect_back_to_url),
            'register' => $relative_url_prefix.'../wp-login.php?action=register&redirect='.urlencode('question2answer/'.$redirect_back_to_url),
            'logout' => $relative_url_prefix.'../wp-login.php?action=logout&redirect='.urlencode('question2answer/'.$redirect_back_to_url)
        );

Make sure you change question2answer to the directory you installed Question2Answer!

F) About line 203, change qa_get_logged_in_user function to:
        global $current_user;
        get_currentuserinfo();
        if ($current_user->ID > 0)
            return array(
                'userid' => $current_user->ID,
                'publicusername' => $current_user->display_name,
                'email' => $current_user->user_email,
                'level' => ($current_user->user_level==10) ? QA_USER_LEVEL_ADMIN : QA_USER_LEVEL_BASIC
            );
        return null;

There is more scope with level if you have different user levels, but this works fine for most installs.

G) About line 297 change qa_get_user_email function to:
        global $user_info;
        get_userdata($userid);
        return $user_info->user_email;

[Note: edited re gidgreen comment below]

H) About line 343 change qa_get_userids_from_public function:
        $publictouserid=array();
           
        if (count($publicusernames)) {
            $escapedusernames=array();
            foreach ($publicusernames as $publicusername)
                $escapedusernames[]="'".mysql_real_escape_string($publicusername, $qa_db_connection)."'";
           
            $results=mysql_query(
                'SELECT display_name, ID FROM wp_users WHERE display_name IN ('.implode(',', $escapedusernames).')',
                $qa_db_connection
            );
   
            while ($result=mysql_fetch_assoc($results))
                $publictouserid[$result['display_name']]=$result['ID'];
        }
       
        return $publictouserid;

Note that you might need to change the users table name prefix in the SELECT statement (wp_ is standard - but confirm this by checking wp-config.php or your database).

I) About line 425 change qa_get_public_from_userids function to:
        $useridtopublic=array();
       
        if (count($userids)) {
            $escapeduserids=array();
            foreach ($userids as $userid)
                $escapeduserids[]="'".mysql_real_escape_string($userid, $qa_db_connection)."'";
           
            $results=mysql_query(
                'SELECT display_name, ID FROM wp_users WHERE ID IN ('.implode(',', $escapeduserids).')',
                $qa_db_connection
            );
   
            while ($result=mysql_fetch_assoc($results))
                $useridtopublic[$result['ID']]=$result['display_name'];
        }
       
        return $useridtopublic;

Again, check the users table prefix in the SELECT statement.


There are other optional functions that might improve your installation, but the above will give you a working platform accessible by your current users (note that users only get listed in the Users display when they have contributed)
by
Wow. Thanks a million for posting this. There's one issue - the qa_get_user_email(...) function needs to return the email address for the user provided as the $userid parameter, not necessarily the current user. Apart from that I haven't tested it but the code makes sense, so thanks again!
by
Sorry gidgreen - I should not have rushed it. Now fixed - thank goodness that, unlike a certain other Q&A application, we can edit entries here :-)
by
I do not understand what do you mean as

B) In step 7, change null to to 'BIGINT UNSIGNED' (including quotes). ????

how???

function qa_get_mysql_user_column_type()   to ......? any sample ?

thank in advance
by
Once I perform step F), any/all q2a pages show nothing (at least end-user display shows this).  Any advice as to how to fix this or at least to get a diagnostic as to what's not working right?
by
@Henry. I'm guessing you mean that old questions have gone? This is expected, as you have switched user tables, so old Q2A users are not accessible. It should be fairly easy to import Q2A user table to WordPress, but I've never tried it.

@minnkyaw. Sorry, but I don't understand. Why not raise a new related question with more details of what you have tried and where it is failing.
by
Hi shrewdies. Nope that's not what I meant:

I started with a fresh q2a install. I've tested your code and it appears step F) is causing all my pages to come up blank (no content). None of the other code causes this problem and I've narrowed it down to this line in step F): get_currentuserinfo();

I think the problem is that it isn't detecting the WP user session properly. When I remove that line, all my q2a pages appear again for the end user but of course the wordpress login session is never detected.

Everything else seems to be ok for now (I went into the qa_posts and manually changed the user_id of a sample anonymous post from NULL to 1, and it did in fact grab the username associated with user_id = 1 in wp_users).
by
Sorry for the misunderstanding Henry.

It suggests that WordPress isn't being called first to set $current_user (you could check this with a temporary print_r($current_user);

Somewhere, I've written about integrating the Q2A theme into WordPress, but I can't find it right now.

In your selected Q2A qa-theme.php you need to call WordPress. The following is probably overkill, but it works:
<?php
include_once('../wp-config.php');
include_once('../wp-load.php');
include_once('../wp-includes/wp-db.php');
global $wpdb;


If you want to wrap the WordPress Headers etc, then the following
function does the trick:
    class qa_html_theme extends qa_html_theme_base
    {
        function head_custom() // add WordPress Header
        {
            $this->output(get_header());
        }

        function body_suffix()
        {
            $this->output(get_footer());
        }
   
    }
by
edited by
I've added the include_once lines to qa-config.php, qa-external-users.php, qa-theme-base.php (overkill, I wanted to make sure -- I also adjusted the path to reflect my wp directory) but still can't get a login session to be recognized. The print_r($current_user); outputs this:

WP_User Object ( [data] => [ID] => 0 [id] => 0 [caps] => Array ( ) [cap_key] => [roles] => Array ( ) [allcaps] => Array ( ) [first_name] => [last_name] => [filter] => )

So it looks like [ID] is stuck at 0 even after I login to WP and go back to the page. Btw, the login redirects from step E) don't work like they should. They bring me to the WP login page but once I log in I am brought to the WP control panel instead of back to Q2A. Doesn't seem like it's a related issue though.

Did get this working? If so, could you send me your q2a install? I'll run a comparison of the files. Or if you'd like, I can send mine.
by
It definitely works OK at http://www.shrewdies.net/web-hosting-questions/

I think it might be an issue regarding relative paths vs absolute ones. My q2a install sits directly below the root, so ../wp-config.php works for me, but you might need the full path, similar to my earlier correction to the path in D)

My contact details are at the above link if you want to take this to email.
by
Thanks for the help. I was able to wrap the wordpress headers, which seemed strange, since apparently the function in theme.php was able to access the necessary wordpress variables yet external-users.php couldn't get the login session, even with the paths pointing to the right directory.

However, after observing your site, I decided to move all WP files into the root directory instead of having a wordpress directory. Login sessions were recognized as a result. Definitely a path issue.

I've switched to the absolute paths anyhow since I read there was an issue with AJAX features.


I've run into a new issue with the 1.2 Beta 1 now with external-users... Anytime I try to post a question, I get this error:

Question2Answer query failed:

INSERT INTO qa_posts (categoryid, type, parentid, userid, cookieid, createip, title, content, tags, notify, created) VALUES (NULL, _utf8 'A', 1, _utf8 '2', NULL, INET_ATON(_utf8 '61.51.76.176'), NULL, _utf8 'this is a test', NULL, NULL, NOW())

Error 1364: Field 'acount' doesn't have a default value
by
Unfortunately your tutorial wasn't successful on my BlueHost domain;
http://www.question2answer.org/qa/4782/wordpress-sso-integration-stalls-page-with-install-dialog

Please help
by
I've commented elsewhere about this not working with BlueHost (they do not manage servers or customer service as one might expect), and added the following remark to an earlier comment:

[BlueHost are no help. Also, I've changed my mind on JustHost - technically they are OK, but their shared hosting scheme is not suitable for much more than a couple of hundred users, and they don't do dedicated. So far so good with DreamHost (and with NativeSpace in the UK).]
by
Hi Shrewdies, how did you manage to add the wordpress header? I've tried your earlier comment (http://www.question2answer.org/qa/428/how-can-i-integrate-it-with-wordpress#c1623) regarding this, yet that didn't work. Nothing happens, which leds me to believe that the 'get_header()' and 'get_footer()' functions are elsewhere defined? I've searched this site for possible solutions but came up with nothing usable.
by
@Jura It's probably a path issue. First stage is to make sure Q2A works OK without a WordPress Theme integration. If it does, then coding the path to the WP files in your Q2A theme should follow the same pattern as the path in your Q2A config (Step D in my original answer).

Hint: Unless it's a development site that you want to keep secret until it's finished, put your website in your profile, and I can get a clearer picture.
by
Please note that, for version 1.3, my solution will fail on the functions at steps H) and I), as these functions are now implemented slightly differently, and $qa_db_connection is not set. This leads to 3 mysql warnings, and a failure to show the user name (though the user_id is set in the database).

You should look at the examples in qa-external-users.php where you will see that the examples have a new second line [$qa_db_connection = qa_db_connection();]. Copy this line from the examples to my functions listed above, and all will be well.
...