Today I want to journal my implementation of a WordPress site with the package of "question2answer". It comes as self-promoted as being able to integrate with WordPress "out of the box". I'm going to vent a small amount of frustration here, because the only integration going on is the simplicity of configuration with using the same database, along with the user authentication of WordPress. Otherwise they run as two separate sites/themes.
This will not do.
So let's get to some context. I have a new hobby project in mind which requires a open source stack-overflow clone. Enter question2answer. Now I don't want to come across as completely ungrateful, this package - while old, ticks all the boxes and looks like it was well maintained, but I need every page to look the same to have a seamless integration. So, let's go through this step by step.
Forum Index Update
This step probably doesn't need to be done, but I just wanted to make sure that when the page is called upon, that the SCRIPT_FILENAME is not the one that is what is being executed at the level of WordPress. So in the directory where you have placed question2Answer, modify the index like the following:
Simple enough, I started with this just to make sure it picks up the right path when called from WP.
WordPress Edit
Ok, so now assuming you have your designated forum page in Wordpress set up with blank content, get the ID from the page. You can do this when you edit the page and get the number in the URL:
/wp-admin/post.php?post=15&action=edit
Now, within the functions.php of the theme folder, add the following:
This basically will fill the content of the q2a forum in the space of the page. The problem here now is that it won't be able to load your q2a theme resources, because they point directory which is relative to the path if you were accessing the q2a page itself - so they will return a series of 404's. You now need to rewrite these URL's.
htaccess Edit
Now I'm assuming you have your .htaccess file all set up correctly, and likely as using URL's as their post-name, and not by the page ID by the installation's default. Edit the file and have it resemble the following:
You should have only needed to add the section above "BEGIN WordPress". You should also change lines 5-7 from "forum" to the name of your forum page in WordPress.
Almost There
This is where I'm up to. The home page of Q2A will now slot right in to your existing WP forum page, but the problem as it stands is whatever theme you had in Q2A will replace a lot of rules in your WP theme for this page - but that's fixable by removing those style rules from your Q2A theme. The second problem is when you use most of the links in its content, it is effectively navigating away from your WP site again - which is exactly what we're trying to circumvent.
I will keep this journal going to document my journey on this project, but what I am thinking off the top of my head is further edits to the URL rewrites so links from Q2A will be modified so that part of the URL will go in to the query string as an extension of the /forum page. Using this - the
functions update would consist of using a
file_get_contents() on the full URL of the current, plus what is contained in the query string.
UPDATE
I got some very
useful information to what is required from the
htaccess, specifically, I needed to point the redirection to a specific file - such as index.php. Because this was not possible in this implementation (as no sub-directory exists as "forum"), I needed to change my angle. I've got it corrected now by making the following changes:
htaccess:
functions.php
So what is going on now here is the forum assets are still being redirected from the htaccess, but the page itself is being read in by the file_get_contents, and the source of it replaces links to pages within itself as being put in to the query string. So when the query string exists when the page loads, it forms as part of the URL to fetch from the forum link.
UPDATE 2
But wait -- There's more! Even though I was logged in, I am only able to access the admin area if I navigate to the forum to its separate URL - worse, despite being registered and signed in, the forum would not be able to recognize it properly for any user. This forum uses a mix of both a cookie value, as well as a PHP session. So we're going to update our functions.php script again:
This time, we're throwing in some headers, including the current cookies that have been set. On the event that WordPress
init's, we are also enabling the session - this session id will be one of the cookies.
Lastly, for the API's it uses to function correctly, we need to update the value of its script variable: qa_root. The most simplest way to fix this - while being a bit of a band aid, is to modify this file:
<forum directory>\qa-include\qa-index.php
At the time of publishing, line 165: just adding the forum directory to the end of this string:
($relativedepth > 1 ? str_repeat('../', $relativedepth - 1) : './') . "<forum home directory>/"
Capturing Form Posts
We are on the home stretch. For this next part, you will need to edit the qa-theme.php located within the qa-theme/<theme_name> directory. If it doesn't exist already - add a public function of footer() and add the following line of code:
Now, if you haven't guessed already, create a new directory in this current called js and place a file called events.js inside it. Question2Answer uses jquery, so for this task, we're going to start with this simple outline:
This is a work in progress and will handle most events. Effectively if it's posting data, will override the submit event of a form and send the data via ajax, before either redirecting back to the question list or reloading the current window.
Everything should about be working now, I'll hand it over to you. Mainly what's left now is to modify the forum theme rules so that they do not override those of the theme from WordPress, so it can live in harmony. I'll also give a tip to hide/show certain elements in the administration area, since a lot of it will clash with your WordPress settings.
As always, hope this has inspired.
J.
Comments
Post a Comment