WordPress模板插件定制

您现在的位置是:首页 > WordPress教程WordPress教程

WordPress用户同意记录

查看 WP集市 的更多文章WP集市 2025-09-08 【WordPress教程】 694人已围观

  1. 好的,咱们聊聊WordPress里面用户同意记录这档子事儿。说白了,现在搞网站都得讲规矩,特别是用户隐私这块儿,比如GDPR或者CCPA啥的,你得让用户明确同意你收集他们的数据,比如邮箱、cookies之类的。WordPress本身没给你弄个现成的完整解决方案,但咱可以自己捣鼓一下,核心思路就是:在需要的地方弹出同意选项,把用户选择记到数据库,还能随时查证。

  2. 先想清楚要记录啥。常见的有新闻订阅勾选框、隐私政策同意、cookies使用同意。比方说,用户在注册时得勾选“我同意接收营销邮件”,这个动作就得记下来。数据库里最好建个表存这些记录,比如表名wp_user_consents,字段包括用户ID、同意类型、同意时间、IP地址(可选,但注意隐私合规)。

// 示例SQL创建表(可以在插件安装时执行)
CREATE TABLE wp_user_consents (
    id INT AUTO_INCREMENT PRIMARY KEY,
    user_id BIGINT(20) NOT NULL,
    consent_type VARCHAR(100) NOT NULL,
    consent_date DATETIME DEFAULT CURRENT_TIMESTAMP,
    ip_address VARCHAR(45),
    FOREIGN KEY (user_id) REFERENCES wp_users(ID)
);
  1. 前端怎么搞?简单点,在表单里加个复选框,用户勾选了才提交。用WordPress的短代码(shortcode)嵌入到页面或文章里,这样非技术人员也能管理。下面是个简单短代码例子,输出一个同意框和提交按钮。
// 在主题的functions.php或自定义插件中添加短代码
add_shortcode('consent_form', 'consent_form_shortcode');
function consent_form_shortcode($atts) {
    $atts = shortcode_atts(array(
        'type' => 'newsletter',
        'text' => '我同意接收更新邮件'
    ), $atts);
    ob_start();
    ?>
    <form method="post">
        <label>
            <input type="checkbox" name="user_consent" required> <?php echo esc_html($atts['text']); ?>
        </label>
        <input type="hidden" name="consent_type" value="<?php echo esc_attr($atts['type']); ?>">
        <button type="submit" name="submit_consent">提交</button>
    </form>
    <?php
    return ob_get_clean();
}
  1. 处理表单提交。当用户点击提交,咱们得用PHP抓取数据,验证后存到数据库。记得用nonce防CSRF攻击,还有清理输入数据避免SQL注入。钩子用initwp,但最好在插件里用admin-ajax处理异步请求,这里简化例子。
// 处理表单提交
add_action('wp', 'handle_consent_submission');
function handle_consent_submission() {
    if (isset($_POST['submit_consent']) && isset($_POST['user_consent'])) {
        if (!wp_verify_nonce($_POST['_wpnonce'], 'consent_nonce')) {
            wp_die('安全校验失败');
        }
        $user_id = get_current_user_id();
        $consent_type = sanitize_text_field($_POST['consent_type']);
        $ip = $_SERVER['REMOTE_ADDR'];

        global $wpdb;
        $table_name = $wpdb->prefix . 'user_consents';
        $wpdb->insert(
            $table_name,
            array(
                'user_id' => $user_id,
                'consent_type' => $consent_type,
                'ip_address' => $ip
            ),
            array('%d', '%s', '%s')
        );
        echo "<p>同意已记录!</p>";
    }
}
  1. 查询和管理记录。后台可能需要个界面让管理员查看谁同意了啥。用WordPress的列表表(WP_List_Table)类做个管理页面,显示用户、同意类型、时间等。代码略长,但核心是扩展WP_List_Table,定义列和数据获取。
// 简化的管理列表示例(需完整实现)
class Consent_List_Table extends WP_List_Table {
    public function prepare_items() {
        global $wpdb;
        $table_name = $wpdb->prefix . 'user_consents';
        $this->items = $wpdb->get_results("SELECT * FROM $table_name", ARRAY_A);
        $this->_column_headers = array($this->get_columns(), array(), array());
    }
    public function get_columns() {
        return array(
            'user_id' => '用户ID',
            'consent_type' => '同意类型',
            'consent_date' => '同意时间'
        );
    }
    public function column_default($item, $column_name) {
        return $item[$column_name];
    }
}
// 添加到admin菜单
add_action('admin_menu', 'add_consent_admin_page');
function add_consent_admin_page() {
    add_menu_page('用户同意记录', '同意记录', 'manage_options', 'consent-records', 'display_consent_list');
}
function display_consent_list() {
    $consent_table = new Consent_List_Table();
    $consent_table->prepare_items();
    $consent_table->display();
}
  1. 别忘了,用户可能想撤销同意。得提供个方式,比如在用户个人资料页加个按钮,点击后更新数据库或删除记录。同时,前端用Ajax让体验流畅,不用刷新页面。
// Ajax处理撤销同意
add_action('wp_ajax_revoke_consent', 'revoke_consent_callback');
function revoke_consent_callback() {
    $user_id = get_current_user_id();
    $consent_type = sanitize_text_field($_POST['consent_type']);
    global $wpdb;
    $table_name = $wpdb->prefix . 'user_consents';
    $wpdb->delete($table_name, array('user_id' => $user_id, 'consent_type' => $consent_type), array('%d', '%s'));
    wp_send_json_success('同意已撤销');
    wp_die();
}
  1. 最后,合规性很重要。定期清理旧记录,提供数据导出功能(WordPress自带一些工具),并确保整个流程透明。测试时多用不同用户角色试试,避免bug。总之,这活儿虽然琐碎,但用WordPress的钩子和数据库API,能搞出挺灵活的系统。记住,代码要加注释,方便后来人维护,毕竟隐私法规老变,可能得常改。

Tags:

WordPress模板插件定制

WP集市

V管理员
文章 723 篇 | 评论 0 次
最新文章