Topic: [Integration] WordPress Posts Re-posted to punBB Forum
This is a re-post of http://punbb.informer.com/forums/topic/ … -posting/, now in a more-appropriate v.1.4 forum. Thank you, Jones, for starting this much needed integration between WordPress posts and punBB auto-posts.
For members like freequest and others, the code below is to be added to your WordPress theme's function.php.
I've made some adjustments to make the code easier to edit. This was tested and works with punBB 1.4 and WordPress 3.5.1. Whenever a new post is published within WordPress, it is also auto-posted to the punBB board with a permalink back to the original URL.
You can see it in action with posts between the home page of the following link and www [dot] bobafettfanclub [dot] com/boards/forum/8/dialogue/.
Simply replace the defaults noted in the code below:
function punbb_publish_post($post_ID) {
/*
Based upon code by Jones
Revisions on 2013-05-03 by FWD:labs / Aaron Proctor include:
- Replace WordPress HTML content to valid BBCode
- Sanitize database imputs with mysql_real_escape_string(trim($variable))
- Add link within punBB re-post back to WordPress permalink
*/
global $wpdb;
/* Defaults - declare all of the following before using */
$punbbPrefix = ""; // e.g. punbb_
$punbbUsername = ""; // e.g. admin
$punbbUserID = ""; // e.g. 2
$punbbUserIP = ""; // e.g 1.2.3.4
$punbbForumID = ""; // e.g. 8
$wppost = $wpdb->get_row("SELECT ID, post_date, post_title, post_content FROM ".$wpdb->posts." WHERE ID = ".mysql_real_escape_string(trim($post_ID))." LIMIT 1");
/* Check if edited or new, based on username and post date match only */
$wppost_check = $wpdb->get_row("SELECT id, topic_id FROM ".$punbbPrefix."posts WHERE poster = '".$punbbUsername."' AND posted = ".strtotime($wppost->post_date));
$getArticle = $wppost->post_content;
// Convert HTML to BBCode
// Images need some extra help to get to BBCode
// WordPress formatted
$getArticle = preg_replace("/<img src=([^>]+) alt=([^>]+) width=([^>]+) height=([^>]+) class=([^>]+)\>/i", '[img]$1[/img]', $getArticle);
// By-hand formatted
$getArticle = preg_replace("/<img src=([^>]+) \/\>/i", '[img]$1[/img]', $getArticle);
// <br style="clear:both;/"> may break the input
$getArticle = preg_replace("/<br style=([^>]+)\>/i", "\n", $getArticle);
// Based upon bb2html() by Louai Munajim
// Improvements made for list items and double quotes
$htmlcode = array("<", ">",
"<ul>", "</ul>",
"<li>", "</li>",
"<strong>", "</strong>",
"<u>", "</u>",
"<i>", "</i>",
'<a href="', "</a>",
"<blockquote>", "</blockquote>",
'">',
'"');
$bbcode = array("<", ">",
"[list]", "[/list]",
"[*]", "[/*]",
"[b]", "[/b]",
"[u]", "[/u]",
"[i]", "[/i]",
'[url="', "[/url]",
"[quote]", "[/quote]",
'"]',
'');
$getArticle = str_replace($htmlcode, $bbcode, $getArticle);
if ($wppost_check) {
/* Edited Post */
$wpdb->query("UPDATE ".$punbbPrefix."topics SET subject = '".mysql_real_escape_string(trim($wppost->post_title))."' WHERE id = ".$wppost_check->topic_id);
$wpdb->query("UPDATE ".$punbbPrefix."posts SET message = '".mysql_real_escape_string(trim($getArticle))."' WHERE id = ".$wppost_check->id);
} else {
/* New Post */
/* Puts a link to the original posting in the message board post */
$permalink = get_permalink($post_ID);
$wp_add = "\n\nView the original post [url=$permalink]here[/url]";
$displayArticle = $getArticle.$wp_add;
$wpdb->query("INSERT INTO ".$punbbPrefix."topics (poster, subject, posted, last_post, last_poster, forum_id) VALUES(
'".$punbbUsername."',
'".$wppost->post_title."',
".strtotime($wppost->post_date).",
".strtotime($wppost->post_date).",
'".$punbbUsername."',
'".$punbbForumID."'
)");
$topic_id = $wpdb->insert_id;
$wpdb->query("INSERT INTO ".$punbbPrefix."posts (poster, poster_id, message, posted, topic_id, poster_ip) VALUES(
'".$punbbUsername."',
".$punbbUserID.",
'".mysql_real_escape_string(trim($displayArticle))."',
".strtotime($wppost->post_date).",
".$topic_id.",
'".$punbbUserIP."'
)");
$post_id = $wpdb->insert_id;
/* Add new topic needs first_post_id and last_post_id to successfully delete within punBB */
$wpdb->query("UPDATE ".$punbbPrefix."topics SET last_post_id = ".$post_id.", first_post_id = ".$post_id." WHERE id = ".$topic_id);
$wpdb->query("UPDATE ".$punbbPrefix."forums SET last_post = ".strtotime($wppost->post_date).", last_post_id = ".$post_id.", last_poster = '".$punbbUsername."' WHERE id = $punbbForumID");
$punbb_postnumber = $wpdb->get_row("SELECT num_posts, username FROM ".$punbbPrefix."users WHERE username = '".$punbbUsername."' LIMIT 1");
$wpdb->query("UPDATE ".$punbbPrefix."users SET num_posts = ".$punbb_postnumber->num_posts."+1 WHERE username = '".$punbbUsername."'");
}
return $post_ID; /* Optional? */
}
add_action( 'publish_post', 'punbb_publish_post' );