Info
- 60 posts
- 19 voices
- Started 3 years ago by _ck_
- Latest reply from chrishajer
- This topic is not a support question
Here's how to show bbPress info inside WordPress without full integration
-
- Posted 3 years ago #
I'm not sure if this has been addressed elsewhere already or a plugin already exists but for novices that have even just a beginner's knowledge of how php+mysql works I want to show you how easy it is to show bbPress info inside of WordPress and vise-versa.
You should NOT be using overly complex plugins like bbPress-Live or parsing RSS feeds if you have WordPress and bbPress sharing the same database but different tables. Instead, it's a piece-of-cake to grab info from each other directly and display it. You don't even need a plugin, you can code it right into your templates (as long as you know they will remain working together).
So I'll give some examples here and then if anyone has questions feel free to ask.
-
- Posted 3 years ago #
Both bbPress and WordPress have a very simple way of fetching data.
WordPress uses
$wpdb
bbPress uses$bbdb
(the db part means database, very simple)Then there's the good old "get_results". They both use that.
bbpress:
$results=$bbdb->get_results("mysql query goes here");
wordpress:
$results=$wpdb->get_results("mysql query goes here");Many times you can use the same query in either bbpress or wordpress by just changing $bbdb to $wpdb or visa-versa.
Then you have to figure out what you are asking for.
Let's use the really simple bbPress Topics table as an example. Unless you've customized your install, the Topics table is probably calledbb_topicsHere are all the fields available inside of
bb_topics.
topic_id topic_title topic_slug topic_poster topic_poster_name topic_last_poster topic_last_poster_name topic_start_time topic_time forum_id topic_status topic_open topic_last_post_id topic_sticky topic_posts tag_count -
- Posted 3 years ago #
Now we need to put together a correct mysql query.
Let's try something simple.SELECT * FROM bb_topics WHERE topic_status=0 ORDER BY topic_time DESC LIMIT 10SELECT means "grab the following"
the asterisk means "all the fields in the table"
FROM bb_topics is kinda obvious, it's the table we want
topic_status=0 means it's topics not deleted
ORDER BY topic_time DESC means put the newest topics on top
LIMIT 10 means we want only the first 10Let's say we also wanted to exclude topics that were closed, since people can't reply, we don't want to tease them. In that case you would change the
WHERE topic_status=0
to
WHERE topic_status=0 AND topic_open=1or let's say you only wanted "stickies"
WHERE topic_status=0 AND topic_sticky!=0Okay now to use that in WordPress we do the following:
global $wpdb; $query="SELECT * FROM bb_topics WHERE topic_status=0 ORDER BY topic_time DESC LIMIT 10"; $results=$wpdb->get_results($query);If all goes well, WordPress will then execute the query and then fill
$resultswith the answers.Now comes the output part.
-
- Posted 3 years ago #
So we have the $results, how do we make a pretty list of them, say inside of our sidebar?
We have to loop through them and print them out. This is where that list of fields inside of bb_topics comes in handy.
Here's just a list of titles to start with:
foreach ($results as $result) { echo "<li>".$result->topic_title."</li>"; }Of course that's not very useful, because they aren't clickable. To make them clickable will take a little bit more work:
foreach ($results as $result) { echo "<li><a href='/forums/topic.php?id=".$result->topic_id."'>".$result->topic_title."</a></li>"; }That example uses quite a few shortcuts to get the job done, it hardcoded the path to your forums (change /forums/ if needed) and even if your bbPress uses pretty permalinks, it simply uses the topic id number to get there - bbPress will redirect back to permalinks. If you absolutely know you have permalinks and want to use them, you could have done something like this instead:
<a href='/forums/topic/".$result->topic_slug."'>" -
- Posted 3 years ago #
Let's put that all together - this should work right inside any wordpress template:
<h2>Latest Forum Discussions</h2> <ul> <?php global $wpdb; $query="SELECT * FROM bb_topics WHERE topic_status=0 ORDER BY topic_time DESC LIMIT 10"; $results=$wpdb->get_results($query); foreach ($results as $result) { echo "<li><a href='/forums/topic.php?id=".$result->topic_id."'>".$result->topic_title."</a></li>"; } ?> </ul> -
- Posted 3 years ago #
And you can just keep making it fancier and fancier.
Let's say you want to also show how many posts each topic has.
echo "<li><a href='/forums/topic.php?id=".$result->topic_id."'>".$result->topic_title."</a> (".$result->topic_posts." posts)</li>";
or how old the last reply is
echo "<li><a href='/forums/topic.php?id=".$result->topic_id."'>".$result->topic_title."</a> (".human_time_diff(strtotime($result->topic_time." GMT"))." ago)</li>"; -
- Posted 3 years ago #
Since bbPress-Live also does a list of forums, here's how to do that too:
<h2>Forum List</h2> <ul> <?php global $wpdb; $query="SELECT * FROM bb_forums WHERE topics!=0 ORDER BY forum_order ASC LIMIT 10"; $results=$wpdb->get_results($query); foreach ($results as $result) { echo "<li><a href='/forums/forum.php?id=".$result->forum_id."'>".$result->forum_name."</a></li>"; } ?> </ul>
of course this example doesn't take into account nested forums and will just display them flat. -
- Posted 3 years ago #
Superb post! :)
I followed your guide and a have a last 10 posts query working in my worldwide famous 404 page.
Regarding your last code, if you are quering bbpress database, don't you want a global $BBdb; instead of global $wpdb; and a $results=$BBdb instead of $results=$wpdb ?
BTW, how do you query all 'views' fields that bb topic views plugin adds to bb_meta table? If we sum up the value stored in all those field we have a hit counter.
-
- Posted 3 years ago #
I showed you the code to do the sum yesterday in the extend section.
http://bbpress.org/plugins/topic/bb-topic-views/page/3/#post-2638 -
- Posted 3 years ago #
oops, you're right, sorry :) I just missed it because I was editing 4-5 files at the same time. Got it.
-
- Posted 3 years ago #
Just wanted to thank you for this post, you're a genius :)
-
- Posted 3 years ago #
man, this is so great. I am using this now instead of rss and i like it so much more!!!
would you be willing to show how we would add
"Posted by $user in $forum."under the list of topics?
-
- Posted 3 years ago #
also, I got this far... just not sure how to get the forum name, since only the forum ID is listed in this table... not sure how I pull the otehr table and combine the two.
echo " <li><a>topic_id."'>".$result->topic_title."</a>Posted by: ".$result->topic_poster_name.".</li> "; -
- Posted 3 years ago #
Looks like the forum ate your code but to display the forum name when you only have the id, you can use the built in bbpress functions
echo get_forum_name($result->forum_id);or
echo "Posted by $result->topic_poster_name in ".get_forum_name($result->forum_id);or
echo "Posted by <a href='".bb_get_profile_link($result->topic_poster)."'>$result->topic_poster_name</a> in <a href='".get_forum_link($result->forum_id);."'>".get_forum_name($result->forum_id)."</a>"; -
- Posted 3 years ago #
since im calling it in wordpress that function doesnt exist.
-
- Posted 3 years ago #
right now I am using:
echo "<li><a>topic_id."'>".$result->topic_title."</a>Posted by: <a>topic_poster."'>".$result->topic_poster_name."</a> in </li>"; -
- Posted 3 years ago #
@ck - when i try to call the get_forum_name in a wordpress widget it doesnt work. i believe because this is a bbpress function? How would I pull the forum name without using this funtion?
-
- Posted 3 years ago #
figured this out!
but can't get the damn code to post right. if anyone is interested let me know how to post a code snipplet without the forum eating it... it outputs like this:*post title*
Posted by: *user* in *forum*. -
- Posted 3 years ago #
Many many thanks for this post, exactly what I was looking for, and a nice substitute for some weighty bbPress documentation in the meantime.
-
- Posted 3 years ago #
Sorry I missed the earlier questions.
And to post code, make sure you put it between backticks ` -
- Posted 3 years ago #
a nice substitute for some weighty bbPress documentation in the meantime
Actually some of us are looking forward the next chapter ;-) This one should go to the "documentation" section as an entry or something like this.
-
- Posted 3 years ago #
You can't document something until it's in a stable state.
bbPress 1.0 is twice the size of bbPress 0.9 and constantly changing, even in the alpha.Database functions have been mostly the same except the entire meta changed.
But the fundamentals are identical to how WordPress works so you can use the WP codex for that.Oh and WordPress didn't have decent documentation until many years after it was around, don't expect bbPress to be any different. Unless someone volunteers for free, Matt is unlikely to waste paid employee time on documentation.
-
- Posted 3 years ago #
You wrote
WordPress didn't have decent documentation until many years after it was around, don't expect bbPress to be any different
I am on the verge of writing something about my experience of creating a bbPress plugin as a set of guideposts for others to make use of. Not sure how much it would count as "documentation" so much as a quick-start to get someone rolling. -
- Posted 3 years ago #
You can't document something until it's in a stable state.
we could call it "document of the unstable" XD I have a suggestion for chapter two; a juicy thread on producing custom loops.
I am on the verge of writing something about my experience of creating a bbPress plugin as a set of guideposts for others to make use of
I'm pretty sure your guide will be very welcome ;-)
-
- Posted 3 years ago #
ganzua - While documenting the unstable may appear useful, it's a stone cold mother to realize, six months later, that all your work was for nothing, because things were re-written. In order to discourage burnout, never have the tech writers start until you have it baked.
-
- Posted 3 years ago #
Yikes, really sorry if I offended anyone above. I haven't looked into the project so much that I'm aware of the employment/progress etc. and was honestly just delighted to find ck's post as it thoroughly explained everything I was looking for.
-
- Posted 3 years ago #
Ipstenu; I'm not requesting at all :) just suggesting in case CK has another inspired day and has an unrestrained need of writing another guide. Just that, no demand.
-
- Posted 3 years ago #
Once 1.0 beta is released things will be far more solid.
You have to understand that 1.0 is almost a 75% re-write (or re-structuring) of 0.9 so many things have changed. To an end user it seems the same but that's because of much backwards compatibility work by Sam.bbPress 1.0 is 150% the code size of 0.9
So when 1.0 goes beta, that would be a good time to start documentation.
-
- Posted 2 years ago #
One final addition to this that I didn't think of mentioning earlier.
To use bbPress queries like these on a foreign (non-bbPress / non-WordPress) PHP page, all you need to do is include the bbPress core like this at the start:
<?php require('/local-path-to-bbpress/bb-load.php'); ?>or to load WordPress core use this
<?php require('/local-path-to-wordpress/wp-config.php'); ?>(change the 'local-path' bit to your local path)
and remember to use $wpdb on WordPress pages, vs $bbdb on bbPress pages. -
- Posted 2 years ago #
and remember to use $wpdb on WordPress pages, vs $bbdb on bbPress pages
I want to query $bbdb from a wp page. In particular, I want to fetch the total amount of views from your bb Topic Views plugin. This code didn't work;
<?php global $bbdb; $results=$bbdb->get_results("SELECT SUM(meta_value) FROM bb_meta WHERE object_type='bb_topic' AND meta_key='views'"); $results=$bbdb->get_results($query); ?>WordPress is installed in /wordpress/ and bbpress in /wordpress/bbpress/ both deep integrated. What's missing?