Creating a simple WordPress Widget – Recent phpBB 3.1 Topics

This is a simple WordPress widget I did to display the recent topics for phpBB 3.1 forums.
First create a WordPress Plugin as explain in this site Creating a WordPress Plugin
What I did is created a folder in my computer like where ever you want to and call it “phpBB3-Recent” but you can call it what ever you want. Then I open notepad and enter this code


Than I save it as “recent.php” again call it what ever you want but make sure is save as .php
Now we want to use the plugin to add a widget into wordpress, this was explain in this site How to Create a Custom WordPress Widget they show how to create a widget by using such code as
Creating the widget

class wpb_widget extends WP_Widget {

function __construct() {
parent::__construct(
// Base ID of your widget
'wpb_widget',

// Widget name will appear in UI
__('WPBeginner Widget', 'wpb_widget_domain'),

// Widget description
array( 'description' => __( 'Sample widget based on WPBeginner Tutorial', 'wpb_widget_domain' ), )
);
}

// Creating widget front-end
// This is where the action happens
public function widget( $args, $instance ) {
$title = apply_filters( 'widget_title', $instance['title'] );
// before and after widget arguments are defined by themes
echo $args['before_widget'];
if ( ! empty( $title ) )
echo $args['before_title'] . $title . $args['after_title'];

// This is where you run the code and display the output
echo __( 'Hello, World!', 'wpb_widget_domain' );
echo $args['after_widget'];
}
      
// Widget Backend
public function form( $instance ) {
if ( isset( $instance[ 'title' ] ) ) {
$title = $instance[ 'title' ];
}
else {
$title = __( 'New title', 'wpb_widget_domain' );
}
// Widget admin form
?>

<label for="get_field_id( 'title' ); ?>"> <input class="widefat" id="get_field_id( 'title' ); ?>" name="get_field_name( 'title' ); ?>" type="text" value="" />

<?php } // Updating widget replacing old instances with new public function update( $new_instance, $old_instance ) { $instance = array(); $instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : ''; return $instance; } } // Class wpb_widget ends here // Register and load the widget function wpb_load_widget() { register_widget( 'wpb_widget' ); } add_action( 'widgets_init', 'wpb_load_widget' );

Okay now we want to add that widget code into our plugin so it should look like this

 __( 'Sample widget based on WPBeginner Tutorial', 'wpb_widget_domain' ), )
);
}

// Creating widget front-end
// This is where the action happens
public function widget( $args, $instance ) {
$title = apply_filters( 'widget_title', $instance['title'] );
// before and after widget arguments are defined by themes
echo $args['before_widget'];
if ( ! empty( $title ) )
echo $args['before_title'] . $title . $args['after_title'];

// This is where you run the code and display the output
echo __( 'Hello, World!', 'wpb_widget_domain' );
echo $args['after_widget'];
}
      
// Widget Backend
public function form( $instance ) {
if ( isset( $instance[ 'title' ] ) ) {
$title = $instance[ 'title' ];
}
else {
$title = __( 'New title', 'wpb_widget_domain' );
}
// Widget admin form
?>

<label for="get_field_id( 'title' ); ?>"> <input class="widefat" id="get_field_id( 'title' ); ?>" name="get_field_name( 'title' ); ?>" type="text" value="" />

Save that again as you “recent.php” in the widget folder you created
In my case I modefied to display phpBB 3.1 recent topics and the code looks like this

 __( 'Add Recent phpBB3.1 Topics Widget', 'wp_phpbb_recent_domain' ), )
);
}

// Creating widget front-end
// This is where the action happens
public function widget( $args, $instance ) {
$title = apply_filters( 'widget_title', $instance['title'] );
// before and after widget arguments are defined by themes
echo $args['before_widget'];
if ( ! empty( $title ) )
echo $args['before_title'] . $title . $args['after_title'];

// This is where you run the code and display the output
// How Many Topics you want to display?
$topicnumber = 5;
 // Scrolling towards up or down?
    $scroll = "up";

// Change this to your phpBB path
    $urlPath = "/forums";


// Database Configuration (Where your phpBB config.php file is located)
    include 'forums/config.php';

$table_topics = $table_prefix. "topics";
    $table_forums = $table_prefix. "forums";
    $table_posts = $table_prefix. "posts";
    $table_users = $table_prefix. "users";
    $link = mysql_connect("$dbhost", "$dbuser", "$dbpasswd") or die("Could not connect");
    mysql_select_db("$dbname") or die("Could not select database");
 
    $query = "SELECT t.topic_id, t.topic_title, t.topic_last_post_id, t.forum_id, p.post_id, p.poster_id, p.post_time, u.user_id, u.username
    FROM $table_topics t, $table_forums f, $table_posts p, $table_users u
    WHERE t.topic_id = p.topic_id AND
    f.forum_id = t.forum_id AND
   

t.forum_id != 34 AND
t.forum_id != 13 AND
t.forum_id != 31 AND
t.forum_id != 32 AND
t.forum_id != 33 AND
t.forum_id != 18 AND
t.forum_id != 21 AND


    t.topic_status  2 AND
    p.post_id = t.topic_first_post_id AND
    p.poster_id = u.user_id
        ORDER BY p.post_id DESC LIMIT $topicnumber";
    $result = mysql_query($query) or die("Query failed");   

print  " ";

while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
    {

            echo  "
  • " .$row["topic_title"] ."
  • "; } print ""; mysql_free_result($result); mysql_close($link); echo $args['after_widget']; } // Widget Backend public function form( $instance ) { if ( isset( $instance[ 'title' ] ) ) { $title = $instance[ 'title' ]; } else { $title = __( 'New title', 'wp_phpbb_recent_domain' ); } // Widget admin form ?>

    <label for="get_field_id( 'title' ); ?>"> <input class="widefat" id="get_field_id( 'title' ); ?>" name="get_field_name( 'title' ); ?>" type="text" value="" />

    now you save the folder and in your WordPress admin panel go to Plugins than click on Add New there click on Upload Plugin and upload it  😀 I will attached the plugin for my site but you need to do some changes for it to work with you site. Like where it says $urlPath = “/forums”; change that to the folder where your site is and include ‘forums/config.php’; where the config file is, also if you want to block forums such as private forums not displaying the topics where it says lines like t.forum_id != 34 AND add the Forums IDs you want to block
    😀 Anyways if you need any help let me know I don’t know much about this stuff but i am thinking on doing stuff like settings via wordpress heheh in the future  :mrgreen:

    Leave a Reply

    You must Register or Login to comment on Creating a simple WordPress Widget – Recent phpBB 3.1 Topics