Skip to:

Hack: bb replies as wp comments; using bb API?

  • Wanting to integrate bb and wp more, I hacked up wp’s comments.php template to get replies in the forum to show up below posts in the wp section of the site too.

    This currently requires the bbPress Post plugin.

    Fairly simple job: find the corresponding topic, get the replies, and output them. My solution should work for you, too, if you have bbPress post installed and activated as a wp plugin. Code’s below.

    I want to make it possible to reply to the topic from the wp section of the site if you’re logged in. Obviously I could make queries to insert the post into the database, but I’d like to use the bb API so it can be validated correctly, hooks are called etc. Not sure how I would go about making it accessible. Currently calling any bb functions returns the “undefined function” error; how can I make them available for use?

    Paste this in front of the rest of your wp comments.php template:

    function felblogtotopicid( $felpostID ) {
    global $table_prefix, $wpdb;
    $posts_table = $table_prefix . "bbpress_post_posts";
    $topic_id = $wpdb->get_var("SELECT topic_id FROM <code.>$posts_table</.code> WHERE <.code>post_id</.code> = $felpostID LIMIT 1;");
    return $topic_id;
    global $post, $wpdb;
    if( $forumtopic = felblogtotopicid( $post->ID ) ) {
    $bbprefix = get_option( 'wpbb_bbprefix' );
    $replies = $wpdb->get_results( '
    SELECT poster_id, post_text, post_time
    FROM ' . $bbprefix . 'posts
    WHERE topic_id = ' . $forumtopic
    array_shift( $replies ); //takes off first entry, ie. starting post, and moves the rest back one
    //$replies = array of object( poster_id, post_text, post_time ) for each post I think
    echo "<ol>n"; //start of list
    foreach( $replies as $reply ) {
    $poster = get_userdata( $reply->poster_id );
    $poster = $poster->user_login;
    echo '<li class="thread">' . "n"
    . '<div class="reply">' . "n"
    . '<div class="poster">' . "n"
    . "<strong>" . $poster . "</strong>n"
    . "<span> @ " . $reply->post_time . "</span>n"
    . "</div>n"
    . '<div class="post">' . "n"
    . $reply->post_text
    . "</div>n"
    . "</div>n"
    . "</li>n";
    echo "</ol>n"; //end of list
    //$replylink = bb_add_replies_to_topic_link( $forumtopic );
    $replylink = get_option( 'wpbb_path' );
    echo '<h3><a href="' . $replylink . '">Reply!</a></h3>' . "n";

    } else { ?>

    and paste this at the very back:

    <?php } //end else conditional for corresponding topic to blogpost ?>

    Example here

Viewing 16 replies - 1 through 16 (of 16 total)
  • I get this error on my comments page

    WordPress database error: [You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near ‘wp_bbpress_post_posts WHERE post_id =]

    SELECT topic_id FROM wp_bbpress_post_posts WHERE post_id = 32 LIMIT 1;

    Nevermind i figured it out. You put < code > tags on this line:

    $topic_id = $wpdb->get_var(“SELECT topic_id FROM <code.>$posts_table</.code> WHERE <.code>post_id</.code> = $felpostID LIMIT 1;”);

    Now it works great, big thanks!

    Oh, bizarre. It must have edited it in somehow. Trent, could you take that out in case someone else comes along? My hour’s up.

    Excellent! :)

    I just noticed this thread fel64! Brilliant! The code above should be edited correctly now!



    Further hack here, to migrate any comments made to WP posts that have a corresponding bb topic (via the bbPress Post plugin again) as replies to the thread. Unfortunately the date/time won’t be right, it’ll be like a new post just made.

    This needs to be installed/activated as a plugin to work, and to prevent it somehow accidentally happening again I suggest you immediately uninstall it. To run it, simply append “?lulz=p” to a URL and it’ll get to work – make sure you only do this once! An interface would be a lot neater, but, well, meh.

    At your own risk.

    Plugin Name: Comments migrate
    Plugin URI:
    Description: moves wp comments into bb posts.
    Author: fel64
    Version: 0.7
    Author URI:
    function felmigrate()
    global $bbdb, $bb_table_prefix;
    //foreach blogpost/topic in wp_bbpress_post_posts
    $bridge = $bbdb->get_results( ' SELECT post_id, topic_id
    FROM wp_bbpress_post_posts', ARRAY_A );
    foreach( $bridge as $sumthing => $link )
    //foreach comment
    $wppost = $link['post_id'];
    $bbtopic = $link['topic_id'];
    $felcoms = $bbdb->get_results( " SELECT comment_author, comment_date, comment_content, comment_author_IP
    FROM wp_comments
    WHERE comment_post_ID = $wppost" );
    if( $felcoms )
    foreach( $felcoms as $sumthing => $felcom )
    //if valid user
    $felu = bb_get_user_by_name( $felcom->comment_author );
    if( $felu )
    //create new post in that thread with said details
    fel_new_post( $bbtopic, $felcom, $felu->ID );
    function fel_new_post( $topic_id, $post_details, $felID ) {
    global $bbdb, $bb_cache, $bb_table_prefix, $thread_ids_cache;
    $topic_id = (int) $topic_id;

    $bb_post = $post_details->comment_content;
    //$bb_post = apply_filters('pre_post', $bb_post, false, $topic_id);
    $bb_post = htmlspecialchars( $bb_post, ENT_QUOTES );
    $post_status = 0;
    $now = bb_current_time('mysql');
    //$now = $post_details->comment_date;
    $uid = $felID;
    $uname = $post_details->comment_author;
    $ip = $post_details->comment_author_IP;

    $topic = get_topic( $topic_id );
    $forum_id = $topic->forum_id;

    if ( $bb_post && $topic ) {
    $topic_posts = ( 0 == $post_status ) ? $topic->topic_posts + 1 : $topic->topic_posts;
    $bbdb->query("INSERT INTO $bbdb->posts
    (forum_id, topic_id, poster_id, post_text, post_time, poster_ip, post_status, post_position)
    ('$forum_id', '$topic_id', '$uid', '$bb_post','$now', '$ip', '$post_status', $topic_posts)");
    $post_id = $bbdb->insert_id;
    if ( 0 == $post_status ) {
    $bbdb->query("UPDATE $bbdb->forums SET posts = posts + 1 WHERE forum_id = $topic->forum_id");

    /* if( $now < $topic->topic_time )
    $now = $topic->topic_time;
    $uid = $topic->topic_last_poster;
    $uname = $topic->topic_last_poster_name;
    $post_id = $topic->topic_last_post_id;
    */ $bbdb->query("UPDATE $bbdb->topics SET topic_time = '$now', topic_last_poster = '$uid', topic_last_poster_name = '$uname',
    topic_last_post_id = '$post_id', topic_posts = '$topic_posts' WHERE topic_id = '$topic_id'");
    if ( isset($thread_ids_cache[$topic_id]) ) {
    $thread_ids_cache[$topic_id]['post'][] = $post_id;
    $thread_ids_cache[$topic_id]['poster'][] = $uid;
    $post_ids = get_thread_post_ids( $topic_id );
    if ( !in_array($uid, array_slice($post_ids['poster'], 0, -1)) )
    bb_update_usermeta( $uid, $bb_table_prefix . 'topics_replied', $bb_current_user->data->topics_replied + 1 );
    } else
    bb_update_topicmeta( $topic->topic_id, 'deleted_posts', isset($topic->deleted_posts) ? $topic->deleted_posts + 1 : 1 );
    if ( !bb_current_user_can('throttle') )
    bb_update_usermeta( $uid, 'last_posted', time() );
    $bb_cache->flush_one( 'topic', $topic_id );
    $bb_cache->flush_many( 'thread', $topic_id );
    $bb_cache->flush_many( 'forum', $forum_id );
    do_action('bb_new_post', $post_id);
    return $post_id;
    } else {
    return false;

    if( $_GET['lulz'] = 'p' )

    Erm… hey guys, could you tell me what I’ve done wrong?

    I’ve just installed “bbPress Post”, and WordPress is telling me there’s no table called “wp_bbpress_post_options” (which is true). I reckon I should solve this problem before hacking wp’s comments.php template… but how?

    ps.: Oh, and you guys are doing a great job here, congratulations :)

    You do need to use that table, but if you installed WP yourself it should also be created when you activate the plugin. Maybe de-activate and re-activate.

    However I’m working on a plugin that will do the entire thing (complete integration hopefully) based on a different, IMO much neater system. I don’t want to promise anything before it’s done, but if this plugin doesn’t work for you there’ll be a simpler alternative soon. :)

    De-activated, re-activated and it can’t even feed the database. I’m hacking it now, but I can’t figure the problem out at all… oh well, I’m going to ask the author for help, and I’m sure looking forward to seeing your plugin, fel64 :)

    Fel, great work as usual. I just implemented your original hack (with a few minor mods of my own) on the team blog I just started last week. It is my first attempt at a bbpress-as-comments install, and it is working very well so far. This hack adds the extra comment visibility I was missing.

    Keep up the good work.

    Got a problem on my hands. I’m trying to simplify a lot of things by require’ing bb-load.php and then using the bb API. However bbdb loads with a fatal error if I do this when wordpress has fully loaded, but it works perfectly fine if I require bb-load when the plugin is run.

    What I mean is require( 'path/to/bb-load.php' ) works fine if placed anywhere inside the main code of the plugin. If I call it using the wp hook init it no longer works, nor does calling it by other hooks (that I’ve tried) or through function calls in templates and the like.

    I went through wp-settings.php and require’d bb-load systematically until I found that it only worked once wordpress had loaded general-template.php (line 153). It worked from any point onwards, including after do_action('init'); (which I don’t understand at all). Using the init hook in my plugin to require it does not work.

    I suspect this means that the init hook in wordpress maybe does something or other, then loads plugins, then does something else that stops bb being included, then runs any other funtions registered with the init hook – but my wp mojo is not impressive. I tested this with all other plugins deactivated.

    The output if bb-load is required too late in the code:

    Fatal error: Call to a member function get_row() on a non-object in D:xampphtdocsforumsbb-includesfunctions.php on line 1224

    That makes me think bbdb is not created, but I can’t see why it wouldn’t be.

    Can someone help me with this please? I’d much rather use the API than duplicate functions.

    Trent, do you think you could point this topic out to one of those people with mighty WP/bb mojo? Just a gentle pointing out or something. Nothing pushy. :)

    The best bet would be to ask in the bbdevlist as both mdawaffe, matt and other ‘head dudes’ read the list on a regular basis, but visit these forums very seldom….! That is the best way to get some attention to this issue!


    How does this crazy devlist thing work? Signed up but got no emails, so I assume no-one’s talking. I send an email to I don’t want to completely break etiquette or somesuch first time, so I thought I’d wait and see but there isn’t that much to see.



    The list has been quiet for a while. The last message I have is Volume 30, Issue 1 from May 11, 2007. The one prior to that was Volume 29, Issue 17 dated April 23, 2007.

    i tried it but might be some problem check where i use that one

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