要为WordPress添加留言表单功能并实现后台管理,你可以按照以下步骤操作:
- 创建留言表单
首先,你需要创建一个留言表单。可以使用插件(如Contact Form 7)或手动编写代码。
使用Contact Form 7插件
安装并激活Contact Form 7插件。
创建一个新表单,添加以下字段:
姓名(单行文本框)
邮箱(邮箱输入框)
电话(单行文本框)
地址(单行文本框)
留言内容(多行文本框)
将生成的短代码插入到页面或文章中。
手动编写代码
如果你更喜欢手动编写代码,可以在主题的functions.php文件中添加以下代码:
function custom_contact_form() {
ob_start(); ?>
<form action="<?php echo esc_url($_SERVER['REQUEST_URI']); ?>" method="post">
<p>
<label for="name">姓名</label>
<input type="text" name="name" required>
</p>
<p>
<label for="email">邮箱</label>
<input type="email" name="email" required>
</p>
<p>
<label for="phone">电话</label>
<input type="text" name="phone" required>
</p>
<p>
<label for="address">地址</label>
<input type="text" name="address" required>
</p>
<p>
<label for="message">留言内容</label>
<textarea name="message" required></textarea>
</p>
<p>
<input type="submit" name="submit" value="提交">
</p>
</form>
<?php
return ob_get_clean();
}
add_shortcode('custom_contact_form', 'custom_contact_form');
然后在页面或文章中使用[custom_contact_form]短代码来显示表单。
- 处理表单提交
在functions.php中添加代码来处理表单提交并将数据保存到数据库中:
function handle_form_submission() {
if (isset($_POST['submit'])) {
global $wpdb;
$table_name = $wpdb->prefix . 'custom_messages';
$name = sanitize_text_field($_POST['name']);
$email = sanitize_email($_POST['email']);
$phone = sanitize_text_field($_POST['phone']);
$address = sanitize_text_field($_POST['address']);
$message = sanitize_textarea_field($_POST['message']);
$wpdb->insert(
$table_name,
array(
'name' => $name,
'email' => $email,
'phone' => $phone,
'address' => $address,
'message' => $message,
'status' => '未读',
'created_at' => current_time('mysql')
)
);
}
}
add_action('init', 'handle_form_submission');
- 创建数据库表
在functions.php中添加代码来创建数据库表:
function create_custom_messages_table() {
global $wpdb;
$table_name = $wpdb->prefix . 'custom_messages';
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $table_name (
id mediumint(9) NOT NULL AUTO_INCREMENT,
name tinytext NOT NULL,
email tinytext NOT NULL,
phone tinytext NOT NULL,
address tinytext NOT NULL,
message text NOT NULL,
status tinytext NOT NULL,
created_at datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
register_activation_hook(__FILE__, 'create_custom_messages_table');
- 创建后台管理菜单
在functions.php中添加代码来创建后台管理菜单:
function custom_messages_menu() {
add_menu_page(
'留言管理',
'留言管理',
'manage_options',
'custom-messages',
'custom_messages_page'
);
}
add_action('admin_menu', 'custom_messages_menu');
function custom_messages_page() {
global $wpdb;
$table_name = $wpdb->prefix . 'custom_messages';
$messages = $wpdb->get_results("SELECT * FROM $table_name ORDER BY created_at DESC");
echo '<div class="wrap">';
echo '<h1>留言管理</h1>';
echo '<table class="wp-list-table widefat fixed striped">';
echo '<thead><tr><th>ID</th><th>姓名</th><th>邮箱</th><th>电话</th><th>地址</th><th>留言内容</th><th>状态</th><th>操作</th></tr></thead>';
echo '<tbody>';
foreach ($messages as $message) {
echo '<tr>';
echo '<td>' . esc_html($message->id) . '</td>';
echo '<td>' . esc_html($message->name) . '</td>';
echo '<td>' . esc_html($message->email) . '</td>';
echo '<td>' . esc_html($message->phone) . '</td>';
echo '<td>' . esc_html($message->address) . '</td>';
echo '<td>' . esc_html($message->message) . '</td>';
echo '<td>' . esc_html($message->status) . '</td>';
echo '<td>
<a href="' . admin_url('admin.php?page=custom-messages&action=mark_as_read&id=' . $message->id) . '">标记为已读</a> |
<a href="' . admin_url('admin.php?page=custom-messages&action=mark_as_replied&id=' . $message->id) . '">标记为已回</a> |
<a href="' . admin_url('admin.php?page=custom-messages&action=delete&id=' . $message->id) . '">删除</a>
</td>';
echo '</tr>';
}
echo '</tbody>';
echo '</table>';
echo '</div>';
}
- 处理留言管理操作
在functions.php中添加代码来处理留言管理操作(标记为已读、标记为已回、删除):
function handle_custom_messages_actions() {
if (isset($_GET['action']) && isset($_GET['id'])) {
global $wpdb;
$table_name = $wpdb->prefix . 'custom_messages';
$id = intval($_GET['id']);
if ($_GET['action'] == 'mark_as_read') {
$wpdb->update($table_name, array('status' => '已读'), array('id' => $id));
} elseif ($_GET['action'] == 'mark_as_replied') {
$wpdb->update($table_name, array('status' => '已回'), array('id' => $id));
} elseif ($_GET['action'] == 'delete') {
$wpdb->delete($table_name, array('id' => $id));
}
wp_redirect(admin_url('admin.php?page=custom-messages'));
exit;
}
}
add_action('admin_init', 'handle_custom_messages_actions');
- 样式和脚本
你可以根据需要添加自定义样式和脚本来美化表单和管理页面。
原文