Skip to:
Content
Pages
Categories
Search
Top
Bottom

Here’s how to show bbPress info inside WordPress without full integration


  • _ck_
    Participant

    @_ck_

    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.

Viewing 25 replies - 1 through 25 (of 60 total)

  • _ck_
    Participant

    @_ck_

    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 called bb_topics

    Here 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


    _ck_
    Participant

    @_ck_

    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 10

    SELECT 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 10

    Let’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=1

    or let’s say you only wanted “stickies”

    WHERE topic_status=0 AND topic_sticky!=0

    Okay 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 $results with the answers.

    Now comes the output part.


    _ck_
    Participant

    @_ck_

    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."'>"


    _ck_
    Participant

    @_ck_

    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>


    _ck_
    Participant

    @_ck_

    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>";


    _ck_
    Participant

    @_ck_

    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.

    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.


    _ck_
    Participant

    @_ck_

    I showed you the code to do the sum yesterday in the extend section.

    https://bbpress.org/plugins/topic/bb-topic-views/page/3/#post-2638

    oops, you’re right, sorry :) I just missed it because I was editing 4-5 files at the same time. Got it.

    Just wanted to thank you for this post, you’re a genius :)

    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?

    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>
    ";


    _ck_
    Participant

    @_ck_

    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>";

    since im calling it in wordpress that function doesnt exist.

    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>";

    @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?

    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*.


    scoutbrandiegmailcom
    Member

    @scoutbrandiegmailcom

    Many many thanks for this post, exactly what I was looking for, and a nice substitute for some weighty bbPress documentation in the meantime.


    _ck_
    Participant

    @_ck_

    Sorry I missed the earlier questions.

    And to post code, make sure you put it between backticks `

    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.


    _ck_
    Participant

    @_ck_

    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.

    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.

    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 ;-)

    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.


    scoutbrandiegmailcom
    Member

    @scoutbrandiegmailcom

    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.

Viewing 25 replies - 1 through 25 (of 60 total)
  • You must be logged in to reply to this topic.
Skip to toolbar