Author
FWD:labs, based on code by Jones at http://punbb.informer.com/forums/topic/23056/wordpress2punbb-posting/
Requirements
WordPress 4.9+, punBB 1.4.2+
Version (Last Updated)
1.00 (2017-11-17 09:10:01)
1. Edit config to match your punBB install and preferred user.
2. Add this code to your WordPress theme's functions.php file.
3. Publish a post on WordPress and see it auto-publish to your punBB.
function punbb_publish_post($post_ID) { global $wpdb; // Config $punbbPrefix = ""; // Example: punbb_ $punbbUsername = ""; // Example: Forum Admin $punbbUserID = ""; // Example: 2 $punbbUserIP = ""; // Example: 1.2.3.4 $punbbForumID = ""; // Example: 8 // Exit gracefully if config unchanged if ($punbbPrefix == "" || $punbbUsername == "" || $punbbUserID == "" || $punbbUserIP == "" || $punbbForumID == "") { return false; } // Select WordPress post based on the provided $post_ID $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"); $getArticle = $wppost->post_content; // Convert HTML to BBCode // Images need some extra help to get to BBCode // If WordPress formatted the image // $getArticle = preg_replace("/<img src=([^>]+) alt=([^>]+) width=([^>]+) height=([^>]+) class=([^>]+)\>/i", '[img]$1[/img]', $getArticle); // If a content editor formatted the image by hand // $getArticle = preg_replace("/<img src=([^>]+) \/\>/i", '[img]$1[/img]', $getArticle); $getArticle = preg_replace('#<img.+?src=[\'"]([^\'"]+)[\'"].*?>#i', '[img]$1[/img]', $getArticle); // Remove <br style="clear:both;/">, which may break the input $getArticle = preg_replace("/<br style=([^>]+)\>/i", "\n", $getArticle); // Remove [caption] content // Source: http://stackoverflow.com/a/16988328/1527619 $getArticle = preg_replace('#\s*\[caption[^]]*\].*?\[/caption\]\s*#is', '', $getArticle); // Convert HTML to punBB'd BBCODE // Improvements made for list items and double quotes // Based upon bb2html() by Louai Munajim $htmlcode = array("<", ">", "<ul>", "</ul>", "<li>", "</li>", "<strong>", "</strong>", "<u>", "</u>", "<i>", "</i>", "<em>", "</em>", '<a href="', "</a>", "<blockquote>", "</blockquote>", '">', '"'); $bbcode = array("<", ">", "[list]", "[/list]", "[*]", "[/*]", "[b]", "[/b]", "[u]", "[/u]", "[i]", "[/i]", "[i]", "[/i]", '[url="', "[/url]", "[quote]", "[/quote]", '"]', ''); $getArticle = str_replace($htmlcode, $bbcode, $getArticle); // Check punBB if we're editing a topic or adding a new topic, simply based on the username and post date matching $wppost_check = $wpdb->get_row("SELECT id, topic_id FROM ".$punbbPrefix."posts WHERE poster = '".$punbbUsername."' AND posted = ".strtotime($wppost->post_date)); // punBB found a match, so we're editing an existing topic if ($wppost_check) { // Update the title of the punBB topic with any changes made within WordPress $wpdb->query("UPDATE ".$punbbPrefix."topics SET subject = '".mysql_real_escape_string(trim($wppost->post_title))."' WHERE id = ".$wppost_check->topic_id); // Update the content of the punBB post with any changes made within WordPress $wpdb->query("UPDATE ".$punbbPrefix."posts SET message = '".mysql_real_escape_string(trim($getArticle))."' WHERE id = ".$wppost_check->id); // punBB did not find a match, so we're adding a new topic } else { // Add a permalink to the original post in WordPress to the punBB message board re-post $permalink = get_permalink($post_ID); $wp_add = "\n\nView the original post [url=$permalink]here[/url]"; $displayArticle = $getArticle.$wp_add; // Modify the title of the punBB version, perhaps with a prefix like [NEWS] $displayTopicTitle = $wppost->post_title; $displayTopicTitle = "[NEWS] ".$displayTopicTitle; // Create the punBB topic $wpdb->query("INSERT INTO ".$punbbPrefix."topics (poster, subject, posted, last_post, last_poster, forum_id) VALUES( '".$punbbUsername."', '".$displayTopicTitle."', ".strtotime($wppost->post_date).", ".strtotime($wppost->post_date).", '".$punbbUsername."', '".$punbbForumID."' )"); $topic_id = $wpdb->insert_id; // Create the punBB post in the new topic $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; // New topic in punBB needs first_post_id and last_post_id to successfully set $wpdb->query("UPDATE ".$punbbPrefix."topics SET last_post_id = ".$post_id.", first_post_id = ".$post_id." WHERE id = ".$topic_id); // Update the forums table with last post, last post ID, and last poster, which emulates how posts are done within punBB $wpdb->query("UPDATE ".$punbbPrefix."forums SET last_post = ".strtotime($wppost->post_date).", last_post_id = ".$post_id.", last_poster = '".$punbbUsername."' WHERE id = $punbbForumID"); // Get the number of posts of the user $punbb_postnumber = $wpdb->get_row("SELECT num_posts FROM ".$punbbPrefix."users WHERE username = '".$punbbUsername."' LIMIT 1"); // Add one to the number of their posts $wpdb->query("UPDATE ".$punbbPrefix."users SET num_posts = ".$punbb_postnumber->num_posts."+1 WHERE username = '".$punbbUsername."'"); } return $post_ID; } add_action( 'publish_post', 'punbb_publish_post' );
Questions, comments, concerns or ideas about this? Please let us know.