Please or Register to create posts and topics.

Asgaros Forum Thread-Status-Labels

Asgaros Forum Thread-Status-Labels

Ein komplettes Tutorial für die Integration von Status-Labels in Asgaros Forum


⚠️ Wichtiger Hinweis / Important Notice

Deutsch

Vor der Durchführung der folgenden Schritte beachten Sie bitte:

  • Sicherungspflicht: Erstellen Sie vor jeder Änderung an Ihrer WordPress-Installation ein vollständiges Backup Ihrer Datenbank und aller Dateien. Dies ist unerlässlich, um im Falle von Problemen den ursprünglichen Zustand wiederherstellen zu können.

  • Haftungsausschluss: Diese Anleitung wird nach bestem Wissen und Gewissen erstellt und in einer laufenden WordPress-Installation getestet. Dennoch übernehme ich keinerlei Gewähr für die Richtigkeit, Vollständigkeit oder Aktualität der bereitgestellten Informationen. Die Nutzung und Umsetzung der Anleitung erfolgt ausschließlich auf eigene Gefahr und Verantwortung des Anwenders.

  • Keine Garantie: Ich übernehme keine Garantie für die Funktionalität des Codes in Ihrer spezifischen Umgebung. Unterschiedliche WordPress-Konfigurationen, Serverumgebungen und installierte Plugins können zu abweichendem Verhalten führen.

  • Haftungsausschluss für Schäden: Ich hafte nicht für direkte, indirekte, zufällige oder Folgeschäden, die durch die Nutzung oder Nichtnutzung der bereitgestellten Informationen und Code-Snippets entstehen.

English

Before performing the following steps, please note:

  • Backup Required: Create a complete backup of your WordPress database and all files before making any changes to your WordPress installation. This is essential to restore the original state in case of problems.

  • Disclaimer: This guide has been prepared to the best of my knowledge and tested on a live WordPress installation. However, I assume no liability for the accuracy, completeness, or timeliness of the information provided. The use and implementation of this guide is solely at your own risk and responsibility.

  • No Warranty: I provide no warranty for the functionality of the code in your specific environment. Different WordPress configurations, server environments, and installed plugins may lead to unexpected behavior.

  • Limitation of Liability: I shall not be liable for any direct, indirect, incidental, or consequential damages arising from the use or non-use of the provided information and code snippets.

Durch die Nutzung dieser Anleitung erklären Sie sich mit den oben genannten Bedingungen einverstanden. / By using this guide, you agree to the above terms.


Inhaltsverzeichnis / Table of Contents

  • Einleitung / Introduction

  • Datenbank-Änderungen / Database Changes

  • Code-Integration / Code Integration

  • Verwendung / Usage


Einleitung / Introduction

Deutsch

Dieses Tutorial zeigt, wie man ein Thread-Status-Label-System für das Asgaros Forum WordPress-Plugin implementiert. Damit können Threads mit individuellen Status-Labels (z.B. “Offen”, “Verkauft”, “Erledigt”) versehen werden, die im Frontend angezeigt werden.

Bei mir hat diese Variante auf einer laufenden WordPress-Installation mit Asgaros Forum problemlos funktioniert. Der Code ist universell einsetzbar und wurde in der Praxis getestet.

Funktionen:

  • ✅ Admin-Menü zur Verwaltung der Status-Labels

  • ✅ Dropdown im Thread-Editor

  • ✅ Status-Anzeige im Thread und in der Forums-Übersicht

  • ✅ Berechtigungen (nur Author/Admin können Status ändern)

  • ✅ Individuelle Farben für Hintergrund und Schrift

English

This tutorial shows how to implement a Thread Status Label System for the Asgaros Forum WordPress plugin. This allows threads to be tagged with individual status labels (e.g., “Open”, “Sold”, “Completed”) that are displayed in the frontend.

This variant has been successfully tested on a live WordPress installation with Asgaros Forum. The code is universal and has been proven in practice.

Features:

  • ✅ Admin menu for managing status labels

  • ✅ Dropdown in the thread editor

  • ✅ Status display in threads and forum overview

  • ✅ Permissions (only author/admin can change status)

  • ✅ Individual background and text colors


Datenbank-Änderungen / Database Changes

Schritt 1 / Step 1: Tabelle wp_forum_topics erweitern / Extend table wp_forum_topics

SQL-Befehl / SQL Command:

sql
ALTER TABLE `wp_forum_topics` ADD COLUMN `status` varchar(50) DEFAULT NULL;

Erklärung / Explanation:

  • Die Tabelle wp_forum_topics ist die Haupttabelle für Threads in Asgaros Forum

  • The table wp_forum_topics is the main table for threads in Asgaros Forum

  • Wir fügen eine Spalte status hinzu, die den Status-Text speichert

  • We add a column status that stores the status text

Hinweis / Note: Wenn Ihr Datenbank-Prefix nicht wp_ ist, ersetzen Sie wp_ durch Ihr Prefix (z.B. asgaros_, custom_, etc.). / If your database prefix is not wp_, replace wp_ with your prefix (e.g., asgaros_, custom_, etc.).


Code-Integration / Code Integration

Schritt 1 / Step 1: functions.php öffnen / Open functions.php

Die functions.php Ihres WordPress-Themes befindet sich unter:
The functions.php of your WordPress theme is located at:

text
/wp-content/themes/ihr-theme/functions.php

Wichtig / Important: Fügen Sie den gesamten Code NACH <?php und VOR dem schließenden ?> (falls vorhanden) ein. / Add the entire code AFTER <?php and BEFORE the closing ?> (if present).


Schritt 2 / Step 2: Vollständigen Code einfügen / Add complete code

/**
 * ================================================================
 * ASGAROS FORUM – THREAD-STATUS-LABELS (UNIVERSELL)
 * ================================================================
 * BEGINN DES STATUS-LABEL-BLOCKS
 * ================================================================
 */

// 1. TABELLEN-PREFIX ERMITTELN / GET TABLE PREFIX
function af_get_forum_tables() {
    global $wpdb;
    $prefix = $wpdb->prefix;
    
    return array(
        ‘topics’ => $prefix . ‘forum_topics’,
        ‘posts’  => $prefix . ‘forum_posts’,
        ‘forums’ => $prefix . ‘forum_forums’
    );
}

// 2. ADMIN-MENÜ (UNTER “FORUM”) / ADMIN MENU (UNDER “FORUM”)
add_action(‘admin_menu’, function() {
    global $menu, $submenu;

    $forum_slug = null;
    foreach ($menu as $position => $item) {
        if ($item[0] === ‘Forum’) {
            $forum_slug = $item[2];
            break;
        }
    }

    if (!$forum_slug) {
        $forum_slug = ‘asgarosforum’;
    }

    add_submenu_page(
        $forum_slug,
        __(‘Status Labels’, ‘asgaros-forum’),
        __(‘Status Labels’, ‘asgaros-forum’),
        ‘manage_options’,
        ‘af-status-labels’,
        ‘af_status_labels_page’
    );
}, 999);

// 3. ADMIN-SEITE / ADMIN PAGE
function af_status_labels_page() {
    if (isset($_POST[‘save_labels’])) {
        $labels = array();
        if (isset($_POST[‘labels’]) && is_array($_POST[‘labels’])) {
            foreach ($_POST[‘labels’] as $label) {
                if (!empty($label[‘name’]) && !empty($label[‘color’])) {
                    $text_color = isset($label[‘text_color’]) ? sanitize_hex_color($label[‘text_color’]) : ‘#ffffff’;
                    $labels[] = array(
                        ‘name’ => sanitize_text_field($label[‘name’]),
                        ‘color’ => sanitize_hex_color($label[‘color’]),
                        ‘text_color’ => $text_color
                    );
                }
            }
        }
        update_option(‘af_status_labels’, $labels);
        echo ‘<div class=notice notice-success><p>’ . __(‘Labels saved.’, ‘asgaros-forum’) . ‘</p></div>’;
    }

    $labels = get_option(‘af_status_labels’, array());
    ?>
    <div class=wrap>
        <h1><?php _e(’Status Labels', ’asgaros-forum'); ?></h1>
        <form method=post>
            <table class=form-table>
                <thead><tr><th><?php _e(’Name', ’asgaros-forum'); ?></th><th><?php _e(’Background', ’asgaros-forum'); ?></th><th><?php _e(’Text Color', ’asgaros-forum'); ?></th><th><?php _e(’Action', ’asgaros-forum'); ?></th></tr></thead>
                <tbody id=label-rows>
                    <?php if (!empty($labels)): ?>
                        <?php foreach ($labels as $index => $label): ?>
                        <tr>
                            <td><input type=text name=labels[<?php echo $index; ?>][name] value=<?php echo esc_attr($label[’name']); ?> /></td>
                            <td><input type=color name=labels[<?php echo $index; ?>][color] value=<?php echo esc_attr($label[’color']); ?> /></td>
                            <td><input type=color name=labels[<?php echo $index; ?>][text_color] value=<?php echo isset($label[’text_color']) ? esc_attr($label[’text_color']) : ’#ffffff'; ?> /></td>
                            <td><button type=button class=button remove-label><?php _e(’Remove', ’asgaros-forum'); ?></button></td>
                        </tr>
                        <?php endforeach; ?>
                    <?php else: ?>
                        <tr>
                            <td><input type=text name=labels[0][name] placeholder=Offen /></td>
                            <td><input type=color name=labels[0][color] value=#28a745 /></td>
                            <td><input type=color name=labels[0][text_color] value=#ffffff /></td>
                            <td><button type=button class=button remove-label><?php _e(’Remove', ’asgaros-forum'); ?></button></td>
                        </tr>
                    <?php endif; ?>
                </tbody>
            </table>
            <p><button type=button class=button id=add-label><?php _e(’Add Label', ’asgaros-forum'); ?></button></p>
            <p><input type=submit name=save_labels class=button button-primary value=<?php _e(’Save', ’asgaros-forum'); ?> /></p>
        </form>
    </div>
    <script>
    jQuery(document).ready(function($) {
        var rowIndex = <?php echo count($labels); ?>;
        $(‘#add-label’).on(‘click’, function() {
            var row = ‘<tr><td><input type=text name=labels[‘ + rowIndex + ‘][name] placeholder=Neu /></td><td><input type=color name=labels[‘ + rowIndex + ‘][color] value=#ffc107 /></td><td><input type=color name=labels[‘ + rowIndex + ‘][text_color] value=#000000 /></td><td><button type=button class=button remove-label><?php _e(’Remove', ’asgaros-forum'); ?></button></td></tr>’;
            $(‘#label-rows’).append(row);
            rowIndex++;
        });
        $(document).on(‘click’, ‘.remove-label’, function() {
            $(this).closest(‘tr’).remove();
        });
    });
    </script>
    <?php
}

// 4. STANDARD-LABELS SETZEN / SET DEFAULT LABELS
add_action(’init', function() {
    $labels = get_option(’af_status_labels', array());
    if (empty($labels)) {
        $labels = array(
            array(’name' => ’Offen', ’color' => ’#28a745′, ’text_color' => ’#ffffff'),
            array(’name' => ’Verkauft', ’color' => ’#dc3545′, ’text_color' => ’#ffffff'),
            array(’name' => ’Erledigt', ’color' => ’#17a2b8′, ’text_color' => ’#ffffff'),
            array(’name' => ’Verkaufe', ’color' => ’#ffc107′, ’text_color' => ’#000000′)
        );
        update_option(’af_status_labels', $labels);
    }
});

// 5. FUNKTION: PRÜFT OB USER DEN STATUS ÄNDERN DARF / CHECK IF USER CAN CHANGE STATUS
function af_can_change_status($thread_id) {
    if (current_user_can(’manage_options') || current_user_can(’moderate_comments')) {
        return true;
    }
    if (!is_user_logged_in()) {
        return false;
    }
    global $wpdb;
    $tables = af_get_forum_tables();
    $author_id = $wpdb->get_var($wpdb->prepare(
        ”SELECT author_id FROM {$tables[’topics']} WHERE id = %d”,
        $thread_id
    ));
    return ($author_id == get_current_user_id());
}

// 6. DROPDOWN IM EDITOR / DROPDOWN IN EDITOR
add_action(’asgarosforum_editor_custom_content_bottom', function() {
    $labels = get_option(’af_status_labels', array());
    if (empty($labels)) return;

    $post_id = 0;
    $url = $_SERVER[’REQUEST_URI'];
    if (preg_match(’/\/post-bearbeiten\/([0-9]+)\//', $url, $matches)) {
        $post_id = intval($matches[1]);
    }
    if (!$post_id && isset($_GET[’id'])) {
        $post_id = intval($_GET[’id']);
    }
    
    global $wpdb;
    $tables = af_get_forum_tables();
    $thread_id = 0;
    if ($post_id) {
        $thread_id = $wpdb->get_var($wpdb->prepare(
            ”SELECT parent_id FROM {$tables[’posts']} WHERE id = %d”,
            $post_id
        ));
    }
    
    if (!af_can_change_status($thread_id)) {
        return;
    }
    
    $current = ;
    if ($thread_id) {
        $current = $wpdb->get_var($wpdb->prepare(
            ”SELECT status FROM {$tables[’topics']} WHERE id = %d”,
            $thread_id
        ));
    }
    if ($current === null) {
        $current = ;
    }
    ?>
    <div style=margin:15px 0; padding:10px; background:#f9f9f9; border:1px solid #ddd; border-radius:4px;>
        <label><strong><?php _e(’Status:', ’asgaros-forum'); ?></strong></label><br>
        <select name=af_status id=af-status style=width:100%; max-width:300px; padding:8px;>
            <option value=><?php _e(’No Status', ’asgaros-forum'); ?></option>
            <?php foreach ($labels as $label): ?>
                <option value=<?php echo esc_attr($label[’name']); ?> <?php echo ($current === $label[’name']) ? ’selected' : ; ?>><?php echo esc_html($label[’name']); ?></option>
            <?php endforeach; ?>
        </select>
    </div>
    <?php
});

// 7. STATUS SPEICHERN / SAVE STATUS
add_action(’asgarosforum_after_add_topic_submit', ’af_save_status_from_submit', 10, 6);
add_action(’asgarosforum_after_add_post_submit', ’af_save_status_from_submit', 10, 6);
add_action(’asgarosforum_after_edit_post_submit', ’af_save_status_from_submit', 10, 6);

function af_save_status_from_submit($post_id, $thread_id, $subject, $content, $link, $author_id) {
    if (!isset($_POST[’af_status'])) {
        return;
    }
    if (!af_can_change_status($thread_id)) {
        return;
    }
    
    $status = sanitize_text_field($_POST[’af_status']);
    global $wpdb;
    $tables = af_get_forum_tables();
    
    if (!empty($status)) {
        $wpdb->update(
            $tables[’topics'],
            array(’status' => $status),
            array(’id' => $thread_id),
            array(’%s'),
            array(’%d')
        );
    } else {
        $wpdb->update(
            $tables[’topics'],
            array(’status' => null),
            array(’id' => $thread_id),
            array(’%s'),
            array(’%d')
        );
    }
}

// 8. STATUS IM THREAD ANZEIGEN / DISPLAY STATUS IN THREAD
add_action(’wp', function() {
    $url = $_SERVER[’REQUEST_URI'];
    if (strpos($url, ’/thema/') === false) return;
    
    $slug = ;
    if (preg_match(’/\/thema\/([^\/]+)\//', $url, $matches)) {
        $slug = $matches[1];
    }
    if (empty($slug)) return;
    
    global $wpdb;
    $tables = af_get_forum_tables();
    $thread_id = $wpdb->get_var($wpdb->prepare(”SELECT id FROM {$tables[’topics']} WHERE slug = %s”, $slug));
    if (!$thread_id) return;
    
    $status = $wpdb->get_var($wpdb->prepare(”SELECT status FROM {$tables[’topics']} WHERE id = %d”, $thread_id));
    if (empty($status)) return;
    
    $labels = get_option(’af_status_labels', array());
    $color = ’#6c757d';
    $text_color = ’#ffffff';
    foreach ($labels as $label) {
        if ($label[’name'] === $status) {
            $color = $label[’color'];
            $text_color = isset($label[’text_color']) ? $label[’text_color'] : ’#ffffff';
            break;
        }
    }
    
    $badge = ’<div style=”background:' . esc_attr($color) . ’; 
                  color:’ . esc_attr($text_color) . ’; 
                  padding:8px 16px; 
                  border-radius:4px; 
                  font-size:14px; 
                  font-weight:bold; 
                  margin:10px 0; 
                  display:inline-block; 
                  box-shadow: 0 2px 4px rgba(0,0,0,0.1);”>
        ‘ . esc_html($status) . '
    </div>';
    
    ob_start(function($buffer) use ($badge) {
        $buffer = str_replace(’</h1>', ’</h1>' . $badge, $buffer);
        return $buffer;
    });
}, 1);

// 9. STATUS IN DER FORUMS-ÜBERSICHT ANZEIGEN / DISPLAY STATUS IN FORUM OVERVIEW
add_action(’wp', function() {
    $url = $_SERVER[’REQUEST_URI'];
    if (strpos($url, ’/thema/') !== false) return;
    if (strpos($url, ’/chopperforum/') === false) return;
    
    ob_start(function($buffer) {
        $pattern = ’/(<a[^>]*href=”[^”]*\/thema\/([^\/”]+)\/”[^>]*>[^<]*<\/a>)/';
        
        $buffer = preg_replace_callback($pattern, function($matches) {
            $link = $matches[1];
            $slug = $matches[2];
            
            global $wpdb;
            $tables = af_get_forum_tables();
            $thread_id = $wpdb->get_var($wpdb->prepare(”SELECT id FROM {$tables[’topics']} WHERE slug = %s”, $slug));
            if (!$thread_id) return $link;
            
            $status = $wpdb->get_var($wpdb->prepare(”SELECT status FROM {$tables[’topics']} WHERE id = %d”, $thread_id));
            if (empty($status)) return $link;
            
            $labels = get_option(’af_status_labels', array());
            $color = ’#6c757d';
            $text_color = ’#ffffff';
            foreach ($labels as $label) {
                if ($label[’name'] === $status) {
                    $color = $label[’color'];
                    $text_color = isset($label[’text_color']) ? $label[’text_color'] : ’#ffffff';
                    break;
                }
            }
            
            $badge = ’ <span style=”background:' . esc_attr($color) . ’; 
                              color:’ . esc_attr($text_color) . ’; 
                              padding:2px 8px; 
                              border-radius:3px; 
                              font-size:10px; 
                              font-weight:bold; 
                              margin-left:5px;
                              display:inline-block;
                              vertical-align:middle;
                              line-height:1.4;”>
                ‘ . esc_html($status) . '
            </span>';
            
            return $link . $badge;
        }, $buffer);
        return $buffer;
    });
}, 2);

// 10. CSS
add_action(’wp_head', function() {
    $url = $_SERVER[’REQUEST_URI'];
    if (strpos($url, ’/chopperforum/') !== false || strpos($url, ’/thema/') !== false) {
        echo ’<style>
            .af-thread-status-label {
                display: inline-block;
                padding: 4px 12px;
                border-radius: 4px;
                font-size: 0.85em;
                font-weight: bold;
                color: #fff;
                line-height: 1.4;
            }
        </style>';
    }
});

/**
 * ================================================================
 * ENDE DES STATUS-LABEL-BLOCKS
 * ================================================================
 */

Verwendung / Usage

Deutsch

1. Admin-Bereich

  • Gehen Sie zu Forum → Status-Labels

  • Fügen Sie neue Labels hinzu oder bearbeiten Sie bestehende

  • Wählen Sie Hintergrund- und Schriftfarbe

  • Speichern Sie Ihre Änderungen

2. Thread-Editor

  • Beim Erstellen/Bearbeiten eines Threads erscheint ein Dropdown

  • Nur Thread-Author und Admins können den Status ändern

  • Wählen Sie den gewünschten Status aus

3. Frontend-Anzeige

  • Thread-Ansicht: Status wird unter der Überschrift angezeigt

  • Forums-Übersicht: Status wird neben dem Thread-Titel angezeigt

English

1. Admin Area

  • Go to Forum → Status Labels

  • Add new labels or edit existing ones

  • Choose background and text color

  • Save your changes

2. Thread Editor

  • When creating/editing a thread, a dropdown appears

  • Only thread author and admins can change the status

  • Select the desired status

3. Frontend Display

  • Thread View: Status is displayed below the headline

  • Forum Overview: Status is displayed next to the thread title


Viel Erfolg mit Ihrem Status-Label-System! / Good luck with your status label system! 🚀

Nur ein Biker weiß, warum ein Hund seinen Kopf aus einem Autofenster steckt.
Only a biker knows why a dog sticks its head out a car window.