Simpel chat
Her er koden til plugind
<?php
/**
* Plugin Name: Simple Chat Demo
* Description: Et simpelt chat-plugin med shortcode.
* Version: 1.0.0
* Author: Ulrik
*/
if (!defined(‘ABSPATH’)) {
exit;
}
class Simple_Chat_Demo {
private $table_name;
public function __construct() {
global $wpdb;
$this->table_name = $wpdb->prefix . ‘simple_chat_messages’;
register_activation_hook(__FILE__, array($this, ‘activate’));
add_shortcode(‘simple_chat’, array($this, ‘render_chat’));
add_action(‘init’, array($this, ‘handle_form_submit’));
}
public function activate() {
global $wpdb;
$charset_collate = $wpdb->get_charset_collate();
$sql = “CREATE TABLE {$this->table_name} (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
username VARCHAR(100) NOT NULL,
message TEXT NOT NULL,
created_at DATETIME NOT NULL,
PRIMARY KEY (id)
) $charset_collate;”;
require_once ABSPATH . ‘wp-admin/includes/upgrade.php’;
dbDelta($sql);
}
public function handle_form_submit() {
if (
!isset($_POST[‘simple_chat_submit’]) ||
!isset($_POST[‘simple_chat_nonce’])
) {
return;
}
if (!wp_verify_nonce($_POST[‘simple_chat_nonce’], ‘simple_chat_send’)) {
return;
}
global $wpdb;
$username = isset($_POST[‘simple_chat_username’]) ? sanitize_text_field($_POST[‘simple_chat_username’]) : ”;
$message = isset($_POST[‘simple_chat_message’]) ? sanitize_textarea_field($_POST[‘simple_chat_message’]) : ”;
if ($username === ” || $message === ”) {
return;
}
$wpdb->insert(
$this->table_name,
array(
‘username’ => $username,
‘message’ => $message,
‘created_at’ => current_time(‘mysql’),
),
array(‘%s’, ‘%s’, ‘%s’)
);
wp_safe_redirect(add_query_arg(‘chat_sent’, ‘1’, wp_get_referer()));
exit;
}
public function render_chat() {
global $wpdb;
$messages = $wpdb->get_results(
“SELECT * FROM {$this->table_name} ORDER BY created_at DESC LIMIT 20”
);
ob_start();
?>
<div class=”simple-chat-box” style=”max-width:600px;border:1px solid #ccc;padding:16px;border-radius:8px;”>
<h3>Simpel chat</h3>
<?php if (isset($_GET[‘chat_sent’])) : ?>
<p style=”color:green;”>Besked sendt.</p>
<?php endif; ?>
<div class=”simple-chat-messages” style=”height:300px;overflow-y:auto;border:1px solid #ddd;padding:10px;margin-bottom:16px;background:#fafafa;”>
<?php if (!empty($messages)) : ?>
<?php foreach ($messages as $msg) : ?>
<div style=”margin-bottom:12px;padding-bottom:8px;border-bottom:1px solid #eee;”>
<strong><?php echo esc_html($msg->username); ?></strong>
<small style=”color:#666;”>
(<?php echo esc_html($msg->created_at); ?>)
</small>
<div><?php echo nl2br(esc_html($msg->message)); ?></div>
</div>
<?php endforeach; ?>
<?php else : ?>
<p>Ingen beskeder endnu.</p>
<?php endif; ?>
</div>
<form method=”post”>
<?php wp_nonce_field(‘simple_chat_send’, ‘simple_chat_nonce’); ?>
<p>
<label for=”simple_chat_username”>Navn</label><br>
<input type=”text” id=”simple_chat_username” name=”simple_chat_username” required style=”width:100%;padding:8px;”>
</p>
<p>
<label for=”simple_chat_message”>Besked</label><br>
<textarea id=”simple_chat_message” name=”simple_chat_message” required rows=”4″ style=”width:100%;padding:8px;”></textarea>
</p>
<p>
<button type=”submit” name=”simple_chat_submit” value=”1″>Send</button>
</p>
</form>
</div>
<?php
return ob_get_clean();
}
}
new Simple_Chat_Demo();