How to show latest phpBB3 posts in your WordPress sidebar

As you may notice, at the top of the rightmost column of this blog I am showing the latest posts of the forum.

I did not find any widget fitting my needs so I had to solve the problem by myself. And I am going to tell you how.

At the moment it’s not a widget, just a bunch of lines in a PHP Code Widget to do the trick. Should I receive good feedback, I will develop a fully customizable widget.

Anyway, following this tutorial will make you able to show your phpBB3 posts in your WP sidebar in a matter of minutes.

First, in your WP admin area go to Presentation -> Widgets to go into Sidebar Arrangement section.

You should find a screen like this one

WP admin area

… but it may be a bit different according to your theme. Anyway, what you need is a PHP Code Widget, so have to create one (look at the bottom arrow in the image) and drag/drop it in the sidebar.

Then you’re ready to enter the code in your widget:

1
2
3
4
5
6
7
8
9
10
<?php
$connection = mysql_connect(localhost,"your_login","your password") or die("Service temporairly unavailable");
$db = mysql_select_db("your_db_name",$connection) or die("Service temporairly unavailable");
$sql = "select * from phpbb_topics order by topic_last_post_time desc limit 0,10";
$result = mysql_query($sql) or die("Service temporairly unavailable");
for($x=1;$x<=10;$x++){
	$row = mysql_fetch_array($result);
	echo "<a href = \"http://www.yourforumdomain.com/viewtopic.php?f=$row[forum_id]&t=$row[topic_id]\" target = \"_blank\">$row[topic_title]</a><br>";
}
?>

Line 1: Opening php tag. Normally it makes no difference between <? and >?php but my widget told me

PHP Code MUST be enclosed in tags!

Line 2: Connecting with the database where you are hosting your phpBB3. Change localhost with the address of your database, your_login with database login and your_password with your database password. In case something should fail (database is down, login or password are incorrect), I return a “Service temporairly unavaiable” and terminate the script.

Line 3: Selecting the database where you installed your phpBB3. Change your_db_name with the name of the database.

If you do not remember one (or more) data explained at lines 2-3, simply give a look to your config.php file in the folder where you installed phpBB3.

It’s made in this way:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php
// phpBB 3.0.x auto-generated configuration file
// Do not change anything in this file!
$dbms = 'mysql';
$dbhost = '122.122.122.122';
$dbport = '';
$dbname = 'emanuele';
$dbuser = 'triqui';
$dbpasswd = 'banana';
 
$table_prefix = 'phpbb_';
$acm_type = 'file';
$load_extensions = '';
 
@define('PHPBB_INSTALLED', true);
// @define('DEBUG', true);
// @define('DEBUG_EXTRA', true);
?>

At line 5 you will find the database address (122.122.122.122), at line 7 db name (emanuele), at line 8 db username (triqui) and at line 9 the password (banana).

Also remember line 11 and write down somewhere the content of table_prefix variable (phpbb_)

I did not used any of those names, so don’t even try to hack my forum… but let’s go back to the script…

Line 4: This is the string representing the query that will create the table with the last 10 updated topcis. phpbb_topics is the name of the table than contains the topics. If you have another value in your table_prefix variable, then you will need to change phpp_topics with your_table_prefix_topics. So, as an example, if your table prefix is triqui, then your table name will be triqui_topics, while you won’t have to change topic_last_post_time that represents the timestamp of the last post in the topic. In this way, you will get a table with the latest 10 topic ordered by last post time. If you want more (or less) than 10 topics, change the 10 in limit 0,10

Line 5: Processing the query, or returning in case of error the same fake message “Service temporairly unavaiable”

Line 6: Loop to be executed 10 times

Line 7: Fetching the row of the table in an array called row

Line 8: Displaying the link: notice that the link starts with http://www.yourforumdomain.com/, you will have to change this address with the path of your phpBB3 forum. For instance, if your forum is in the forum folder in the google domain, you will have to write http://www.google.com/forum/. You don’t need to change anything else

That’s all. Hope you will find it useful… maybe some day I will make a widget that will be easier to configure.

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (12 votes, average: 4.33 out of 5)
Loading ... Loading ...
WordPress themes are designs for WordPress - one of the most popular blogging software nowadays.
You will be pleasantly surprised by WordPress Themes provided by Template Monster. All of them are of professional design and high quality.
Be my fan on Facebook and follow me on Twitter! Exclusive content for my Facebook fans and Twitter followers

This post has 44 comments

  1. RJ

    on February 8, 2008 at 5:52 pm

    I know a guy who used this in his page…
    I think he was called Emanuele… and it was very cool to see the latest posts without having to go to the forum, I think you should use it, Emanuele

    ;)

  2. Sunil Patel

    on February 8, 2008 at 7:11 pm

    Sorry to be fussy, but you put: or die(“Service temporairly unavaiable”);
    There is a typo, should be: available.
    Anyway, great idea – its really useful!

  3. Emanuele Feronato

    on February 8, 2008 at 8:25 pm

    Thanks. Fixed.

  4. andrew

    on February 10, 2008 at 2:48 pm

    Hi Emanuele, I have recently been making a game based on your ball game with visual from above tutorial. I would like you to put it on your website if that is possible. I have changed the background and the tiles, and have made some types of my own. These include the “rollercoaster tiles” which hurtle you in the direction they are pointing, and a few other surprises. Your tutorials are brilliant and without them I would really be stuck with actionscript.

  5. Graham Kaemmer

    on February 10, 2008 at 7:16 pm

    I am making a website, but I don’t know how to get my phpBB posts/topics into my mySQL server. How do I do this? Did you already show how?

    Thanks a bunch.

  6. Pinoylandia

    on February 11, 2008 at 10:34 am

    thanks to this post. it’s my first time to see that it you can show the latest forum posts on my blog. :)

  7. Monkios

    on February 13, 2008 at 6:58 pm

    It doesn’t make you lose a lot of time but since it is MySQL, you could put “Limit 0, 10″ at the end of your query.

  8. Emanuele Feronato

    on February 13, 2008 at 7:02 pm

    That’s what I did, Monkios

  9. chris

    on February 14, 2008 at 12:53 am

    I am having issues with this. I implemented exactly how you stated and the widget works fine and does display the latest forum posts but all the other widgets below this widget in my sidebar are no longer displaying their data. Any idea why?

  10. stef

    on February 20, 2008 at 12:20 am

    I have the problem that i see the title of the widgetfine but not any latest topics?? any tricks to fix?

  11. Hijack

    on February 24, 2008 at 6:07 pm

    I get this error

    Parse error: parse error, unexpected T_STRING in /home/content/html/example.php on line 7

    Which is this line:

    $row = mysql_fetch_array($result);

    Could you help me?

  12. Tyler

    on February 24, 2008 at 8:49 pm

    Hi Emanuele -

    This is great!! This helped me out a ton, thank you so much!

    I have multiple phpbb3 boards running out of one database (I use different prefix’s to distinguish them all).. Can you think of a way to modify your code so that I can display the past 10 topics out of all of my boards, and not just one specific prefix?

    I appreciate your help!

  13. Tyler

    on February 24, 2008 at 9:54 pm

    Hello again,

    I was actually able to figure out how to select the correct topics using the following SQL statement:

    select * from prefix1_topics
    UNION ALL
    select * from prefix2_topics
    UNION ALL
    select * from prefix3_topics
    order by topic_last_post_time desc limit 0,10

    Is that the best way to accomplish this?

    Now I cannot seem to figure out a way to direct the users after they click on a topic to the correct board. In other words I need to somehow modify your line to the appropriate board:

    echo “$row[topic_title]“;

    The problem I am having is in the bottom section where I am trying to link to the correct topic. I cannot figure out a way to have the topic link to the correct board. How can I make it so that http://prefix.mysite.com matches the correct prefix from my SQL query?

  14. tanketom

    on March 12, 2008 at 7:42 am

    Hi and thanks for the code. Helped me, both from tearing the hair off my head and a lot of time to make this myself.

    Just wondering about one thing. I’ve got part of a forum that is hidden from “normal users” (an admin forum), and this comes up on the frontpage! Would it be possible to attach some code that makes only “visible” forums appear on the frontpage?

    Thanks in advance!

  15. Floroskop

    on March 18, 2008 at 12:00 pm

    Hello!
    I think this try.

  16. Paul

    on April 7, 2008 at 3:55 am

    Hi, great mod, but I have the same query as tanketom. How do I prevent titles of posts in private forums from being displayed?

    I know it can be done, as I have mods within phpBB that do exactly that, but my knowledge of php isn’t good enough to port the code across into a WordPress sidebar. :-(

  17. FrozenHaddock

    on April 7, 2008 at 7:26 pm

    Would this work for Phpbb2?

    We’re having issues with 3…

  18. hellows

    on May 4, 2008 at 6:06 pm

    Wow. It is working. Well done. I am so happy now. I have been looking for this for a long time.

    NOTE: This php is working for 2.5+ worspress but you must istall a php widget plugin so that you can use this code.

    PS: How can I get the topic names to change color when I roll over them with a mouse. something like a flash highlight ?

    Congratulations one more time!

  19. hellows

    on May 4, 2008 at 6:08 pm

    UPDATE. Make a widget out of it or a plugin. This will be so popular eith the WP users as all other plugins in the WP plugin section are not wotking.
    + make the phpBB use the WP databes so a user will have to only login once. This will be so popular. thanks :>

  20. Jimmy

    on May 8, 2008 at 9:41 am

    I have the problem that i see the title of the widgetfine but not any latest topics?? any tricks to fix?

  21. Dudebuddy

    on June 3, 2008 at 6:28 pm

    You also spelled “temporarily” wrong…

  22. Jai

    on July 3, 2008 at 8:50 am

    lol, banana.

  23. Mithun

    on August 4, 2008 at 7:49 am

    hi ,
    I have similar problem.
    I want to add some information from my forum to be displayed on my homepage.
    Information is like current user , latest member name etc ..
    I dn’t kno how to do it ..
    can you help me…

    one more thing , in your given code , what will happen if topics are less than 10 ??

  24. Jonathan Cremin

    on August 6, 2008 at 12:54 pm

    use
    while ($row = mysql_fetch_array($result)){
    echo “forum post link”;
    }
    instead.

    Also, instead of putting numbers alongside your code, use and ordered list to achieve the same effect without us having to delete it from your code.

  25. Ed

    on September 2, 2008 at 2:17 am

    Thanks for the small snippet of code that allowed me to not do any work. :) Using it on http://www.hiptop3.com in the sidebar.

  26. Chris

    on September 24, 2008 at 7:28 pm

    this code just destroyed my site. I also cannot remove the widget anymore in my Dashboard.

    Please help fast.

  27. werutzb

    on October 7, 2008 at 2:03 am

    Hi!

    I would like make better my SQL capabilities.
    I red so many SQL resources and still feel, that I am not an expert
    in SQL. What would you recommend?

    Thanks,
    Werutz

  28. HSCharles

    on November 14, 2008 at 5:23 am

    I have a flash site
    i’m looking for the script who of google ads on flash.
    how can i get it?

  29. selleriee

    on November 17, 2008 at 8:33 pm

    did you mean http://flashsense.t35.com/ ?

  30. iTashaLIL

    on December 10, 2008 at 1:48 am

    Hello

    I have an idea to set my Photo as avatar. But I have some problems when upload my photo.
    Some times it does not upload at all. Or when I upload the photo, the photo has some deformation.
    What should I do to fix that?

    Thanks a lot

  31. fresher

    on December 21, 2008 at 10:51 pm

    Hi. can it be used for joomla site? i tried but nothing apiers in my panel on my site…can somebody help me? thanks

  32. fuqaha

    on December 22, 2008 at 9:27 pm

    waahahahahaha.. thanks a lot!

    This works great on my Joomla site!

    thanks again!

  33. NoignFiedrign

    on January 22, 2009 at 5:33 am

    Nothing seems to be easier than seeing someone whom you can help but not helping.
    I suggest we start giving it a try. Give love to the ones that need it.
    God will appreciate it.

  34. JoRDANA

    on February 4, 2009 at 2:22 pm

    can you help me? I don’t know whay should i do
    i dont noob!

  35. vasilis

    on March 2, 2009 at 8:34 pm

    how to show the post with utf8 support?

  36. Gofree

    on March 6, 2009 at 10:34 am

    Please, make this WP plugin.

  37. Marko

    on April 26, 2009 at 10:52 am

    Hi,

    I tried a lot of those PHP codes to show last posts on my page … but this one only works for me :-) … THX!!

    I’m not using WordPress, but I manage to implement. But now I have “local” issue. In Slovenia we are using “ŠČŽ – funny letters :-)” and I have problem showing them. OK just one letter “ÄŒ”. You can look on my page (www.drp-drustvo.si) under “..:: Sveže na forumu ::..” (light yellow notes) …

    Thx in advance and thx again for really useful code!

    Regards, Marko

  38. paul

    on January 8, 2010 at 11:57 am

    This is a cool idea but there’s a security issue in here. Actually, you don’t need a password from the database and username, all you should have is msql server so the script can retrieve info/topics last 5 posts…etc

  39. niv

    on March 4, 2010 at 6:19 pm

    UTF-8 Support please….

    How can it be done?

  40. Ryan

    on September 18, 2010 at 11:43 am

    I have my phpBB latest posts showing on home page, built into my theme properly.

    http://www.webtechglobal.co.uk

    If anyone needs help getting these things working and fully themed just go to my forum for some help.

    Thanks for another good plugin.

  41. Marius

    on March 28, 2011 at 2:47 pm

    How to add UTF-8 support please help!

  42. lalami

    on April 15, 2011 at 11:47 am

    The same problem as on the Slovenian site – only that my site is in Turkish.

    Is there any way to make Turkish letters appear with diacritics?

    I’m really greateful for the code, I’ve tried all available plugins and none of them works for my site (another explanation might be – your instructions are really good, so I could understand and put them in action;)

  43. dahgapflilalf

    on June 11, 2011 at 11:36 pm

    hi amazing topic you have going here.

  44. OpenUser

    on July 27, 2011 at 3:47 pm

    This code worked for me (WP 3.2.1) in combination with PHP Code Widget ( http://wordpress.org/extend/plugins/php-code-widget/ ), though some modifications were needed. The “die()” function seems to stop further execution of PHP code and can so prevent the remaining content from loading. So the code had to be rewritten like this (not pretending to be the most elegant way ;) ):

    <?php
    if($connection = mysql_connect("your_db_server","your_db_login","your_db_password"))
    {
    if($db = mysql_select_db("your_db_name",$connection))
    {
    $sql = "select * from phpbb_topics order by topic_last_post_time desc limit 0,10";
    if($result = mysql_query($sql))
    {
    for($x=1;$x<=10;$x++)
    {
    $row = mysql_fetch_array($result);
    echo "$row[topic_title]“;
    }
    }
    else
    {
    echo(“Service temporairly unavailable”);
    }
    }
    else
    {
    echo(“Service temporairly unavailable”);
    }
    }
    else
    {
    echo(“Service temporairly unavailable”);
    }
    ?>

    Could the same really be accomplished without storing database login data in the script code? Please post the code if you know how to achieve that!