Please or Register to create posts and topics.

Create WordPress Posts From Asgarus Topics

This is a plugin I developed for my installation of Asgarus Forum. It will import topics from Asgarus Forum and create WordPress posts, which will link to the corresponding forum topic. Once the initial import has completed, it will check for new topics and import them on a daily schedule. It is unique to my installation and will require some knowledge of PHP to configure for your installation, but feel free to give it a try. You will need to know how to create a plugin from this file and import it into your WordPress installation, either via Plugins > Add New Plugin or FTP. I use this successfully with the Astra Pro and Spectra Pro Theme. This tool is provided without any warranty, express or implied. Use at your own risk.

<?php
/**
* Plugin Name: Asgaros Topic Importer
* Description: Imports Asgaros Forum topics as Gutenberg-formatted WordPress posts using wp:html blocks, including first post content and forum_topic_id.
* Version: 2.5
* Author: Developer
*/

add_action(‘admin_menu’, function () {
add_management_page(
‘Import Forum Topics’,
‘Import Forum Topics’,
‘manage_options’,
‘import-forum-topics’,
‘asgaros_topic_importer_page’
);
});

function asgaros_topic_importer_page() {
if (!current_user_can(‘manage_options’)) return;

if (isset($_POST[‘run_import’]) && check_admin_referer(‘asgaros_import_nonce’)) {
asgaros_topic_importer_run();
}

?>
<div class=”wrap”>
<h1>Import Asgaros Forum Topics</h1>
<form method=”post”>
<?php wp_nonce_field(‘asgaros_import_nonce’); ?>
<p>This will create a WordPress post for each Asgaros Forum topic, using the first post content.</p>
<p><strong>Posts will be published under the “t1-diabetes” category, with a forum_topic_id custom field.</strong></p>
<p><input type=”submit” name=”run_import” class=”button button-primary” value=”Run Import Now”></p>
</form>
</div>
<?php
}

function asgaros_topic_importer_run() {
global $wpdb;

$topics_table = $wpdb->prefix . ‘forum_topics’;
$posts_table = $wpdb->prefix . ‘forum_posts’;

$topics = $wpdb->get_results(“
SELECT id, name, slug, parent_id
FROM $topics_table
WHERE parent_id IN (1,2,3,4,5,6,7,8,9,10,11,12,13)
“);

$category = get_category_by_slug(‘wordpress category slug‘);
$category_id = $category ? $category->term_id : wp_create_category(‘wordpress category name‘);

$forum_page_id = get_option(‘asgarosforum_forum_id’);
$forum_base = $forum_page_id ? get_permalink($forum_page_id) : site_url(‘/forum/‘); //This is the Wordpress page that hosts your forum
$topic_slug_base = get_option(‘asgarosforum_slug_topic’, ‘topic’); //This is your topic base – see URLs & SEO in Asgarus Forum

$imported = 0;

foreach ($topics as $topic) {
// Skip if already imported based on _forum_topic_id meta
$existing = get_posts([
‘post_type’ => ‘post’,
‘meta_key’ => ‘_forum_topic_id’,
‘meta_value’ => $topic->id,
‘numberposts’ => 1,
]);
if (!empty($existing)) continue;

// Get first post by parent_id
$first_post = $wpdb->get_row($wpdb->prepare(“
SELECT text FROM $posts_table WHERE parent_id = %d ORDER BY id ASC LIMIT 1
“, $topic->id));

$first_post_content = $first_post ? wpautop(wp_kses_post($first_post->text)) : ‘<p>(No content available)</p>’;

// Build forum topic URL
$forum_url = trailingslashit($forum_base) . trailingslashit($topic_slug_base) . $topic->slug . ‘/’;

// Create Gutenberg-friendly content
$content = <<<HTML
<!– wp:paragraph –>
<p><strong>Join the conversation:</strong> <a href=”{$forum_url}” rel=”noopener”>{$topic->name}</a></p>
<!– /wp:paragraph –>

<!– wp:separator {“className”:”is-style-default”} –>
<hr class=”wp-block-separator is-style-default”/>
<!– /wp:separator –>

<!– wp:html –>
{$first_post_content}
<!– /wp:html –>
HTML;

// Insert the post
$post_id = wp_insert_post([
‘post_title’ => $topic->name,
‘post_content’ => $content,
‘post_status’ => ‘draft’, //Change to publish if you want the posts published on import. Leave as draft if you want to review them before publishing
‘post_type’ => ‘post’,
‘post_category’ => [$category_id],
]);

if ($post_id && !is_wp_error($post_id)) {
update_post_meta($post_id, ‘_forum_topic_id’, $topic->id);
$imported++;
}
}

if (!defined(‘DOING_CRON’)) {
add_action(‘admin_notices’, function () use ($imported) {
echo ‘<div class=”notice notice-success is-dismissible”><p>’ . esc_html($imported) . ‘ forum topics imported as Gutenberg posts.</p></div>’;
});
}
}

// Daily cron setup
register_activation_hook(__FILE__, function () {
if (!wp_next_scheduled(‘asgaros_topic_import_cron’)) {
wp_schedule_event(time(), ‘daily’, ‘asgaros_topic_import_cron’);
}
});
register_deactivation_hook(__FILE__, function () {
wp_clear_scheduled_hook(‘asgaros_topic_import_cron’);
});
add_action(‘asgaros_topic_import_cron’, ‘asgaros_topic_importer_run’);

Biker has reacted to this post.
Biker