为wordpress自定义一个留言表单并可以在后台进行管理的实现方法

要为WordPress添加留言表单功能并实现后台管理,你可以按照以下步骤操作:

  1. 创建留言表单

首先,你需要创建一个留言表单。可以使用插件(如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]短代码来显示表单。

  1. 处理表单提交

在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');
  1. 创建数据库表

在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');
  1. 创建后台管理菜单

在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>';
}
  1. 处理留言管理操作

在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');
  1. 样式和脚本

你可以根据需要添加自定义样式和脚本来美化表单和管理页面。

原文

http://www.shadahan.com/jianzhan/7806.html

相关推荐
WEB前端圈1 天前
WordPress报502错误问题解决-php-fpm-84.service loaded failed failed LSB: starts php-fpm
开发语言·nginx·php·wordpress
wodrpress资源分享2 天前
wordpress自定the_category的输出结构
wordpress
林润庚7 天前
【Mark】记录用宝塔+Nginx+worldpress+域名遇到的跨域,301,127.0.0.1,CSS加载失败问题
运维·前端·css·nginx·阿里云·腾讯云·wordpress
wodrpress资源分享7 天前
WordPress多语言插件GTranslate
wordpress
wodrpress资源分享8 天前
给wordpress仪表盘添加自定义图标
wordpress
wodrpress资源分享8 天前
开源程序wordpress在海外品牌推广中的重要作用
wordpress
SmallBambooCode11 天前
【WordPress】发布文章时自动通过机器人推送到钉钉
机器人·php·钉钉·博客·wordpress
Web极客码11 天前
如何在WordPress网站中查看移动版本—快速预览与自定义设置
wordpress·移动端·手机端
still13 天前
WordPress Elementor提示错误无法保存500的解决指南
wordpress