Thiết kế website Ninh Bình – nbpage.com mình sẽ hướng dẫn 2 cách: (A) dùng plugin có sẵn (dễ, nhanh), và (B) chèn code tự lưu vào bảng riêng (chuyên nghiệp, linh hoạt). Mình cũng kèm đoạn code sẵn để paste vào functions.php của child theme (Flatsome) hoặc chèn bằng plugin “Code Snippets”. Nhắc trước: nhớ xem luật bảo mật / GDPR — thông báo trong privacy policy và xoá/ẩn IP sau một khoảng thời gian nếu cần.
A — Dùng plugin (đơn giản)
-
Cài plugin như WP Statistics, User IP and Location hoặc các plugin thống kê/visitor-tracking khác từ kho WordPress. Chúng có sẵn chức năng lưu IP, báo cáo, geo-lookup.
-
Cấu hình plugin (bật lưu IP, kiểm tra chế độ ẩn/anonymize nếu cần). Một số plugin có tùy chọn ẩn bớt hoặc hash IP để phù hợp GDPR.
-
Ưu/nhược: nhanh, giao diện; nhưng ít tùy biến và có thể lưu trữ nhiều dữ liệu — kiểm tra privacy.
B — Tự lưu bằng code (cho dev / nhiều kiểm soát)
Mình đưa đoạn code mẫu: nó sẽ
-
tạo bảng
wp_visitor_ips(nếu chưa có), -
lấy IP khách một cách tương đối an toàn,
-
lưu một record mỗi lần trang được load (bạn có thể điều chỉnh để chỉ lưu lần đầu mỗi session, chỉ lưu page cụ thể, bỏ bot, v.v.).
Đặt chỗ nào: chèn vào functions.php của child theme (không chèn trực tiếp vào theme gốc của Flatsome) hoặc dùng plugin “Code Snippets”. (Flatsome khuyến nghị dùng child theme cho custom PHP).

Code Plugins lấy địa chỉ IP truy cập trong Wordpres
Mình sẽ hoàn thiện đoạn code thành một “mini plugin” để:
-
Tạo bảng
wp_visitor_ips(tự động khi kích hoạt). -
Ghi IP, User Agent, URL, Referrer, thời gian truy cập mỗi lần người dùng vào site.
-
Có trang quản trị riêng trong WordPress admin để xem danh sách truy cập, kèm nút xuất CSV.
-
Có thể bật/tắt chức năng ghi log trong Admin.
Bạn chỉ cần copy code này vào file visitor-ip-logger.php rồi nén thành .zip và cài như plugin bình thường (hoặc tạo thư mục visitor-ip-logger trong wp-content/plugins/ rồi đặt file vào).
Chức năng xuất TXT của Plugín để lấy danh sách địa chỉ IP đã truy cập vào website wordpress
-
Chỉ xuất danh sách IP duy nhất (unique).
-
Mỗi IP nằm một dòng trong file
.txt. -
Không xuất kèm HTML (do trước có thể echo lẫn HTML admin).
-
Thêm chức năng lọc bot (Googlebot, Bingbot, AhrefsBot, SemrushBot, v.v.) ngay từ lúc ghi log, để bảng dữ liệu không bị “ngập” bot.
Code toàn bộ của plugins thống kê địa chỉ IP trong wordpress:
<?php
/*
Plugin Name: Visitor IP Logger
Description: Lưu và hiển thị danh sách IP người dùng truy cập website.
Version: 1.0
Author: Bạn
*/if ( ! defined( ‘ABSPATH’ ) ) exit;
class Visitor_IP_Logger {
private $table;
public function __construct() {
global $wpdb;
$this->table = $wpdb->prefix . ‘visitor_ips’;// Hook cài đặt / gỡ
register_activation_hook( __FILE__, [ $this, ‘install_table’ ] );
register_deactivation_hook( __FILE__, [ $this, ‘deactivate’ ] );// Ghi IP
add_action( ‘wp’, [ $this, ‘log_visitor’ ] );// Trang admin
add_action( ‘admin_menu’, [ $this, ‘add_admin_page’ ] );
}/** Tạo bảng khi kích hoạt */
public function install_table() {
global $wpdb;
$charset_collate = $wpdb->get_charset_collate();$sql = “CREATE TABLE IF NOT EXISTS {$this->table} (
id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
ip VARCHAR(45) NOT NULL,
user_agent TEXT,
url TEXT,
referrer TEXT,
visit_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id),
KEY ip (ip),
KEY visit_time (visit_time)
) $charset_collate;”;require_once( ABSPATH . ‘wp-admin/includes/upgrade.php’ );
dbDelta( $sql );
}public function deactivate() {
// Không xoá bảng để giữ log, có thể tuỳ biến thêm nếu cần
}/** Lấy IP an toàn */
private function get_ip() {
if ( ! empty( $_SERVER[‘HTTP_X_FORWARDED_FOR’] ) ) {
$ips = explode( ‘,’, $_SERVER[‘HTTP_X_FORWARDED_FOR’] );
$ip = trim( $ips[0] );
} elseif ( ! empty( $_SERVER[‘HTTP_CLIENT_IP’] ) ) {
$ip = $_SERVER[‘HTTP_CLIENT_IP’];
} else {
$ip = $_SERVER[‘REMOTE_ADDR’] ?? ”;
}
return filter_var( $ip, FILTER_VALIDATE_IP ) ? $ip : ”;
}/** Ghi log mỗi lần truy cập */
public function log_visitor() {
if ( is_admin() ) return; // bỏ qua admin
global $wpdb;$ip = $this->get_ip();
if ( ! $ip ) return;$user_agent = isset($_SERVER[‘HTTP_USER_AGENT’]) ? substr($_SERVER[‘HTTP_USER_AGENT’], 0, 600) : ”;
// Danh sách bot phổ biến cần loại bỏ
$bots = [
‘googlebot’, ‘bingbot’, ‘slurp’, ‘duckduckbot’, ‘baiduspider’,
‘yandexbot’, ‘sogou’, ‘exabot’, ‘facebot’, ‘ia_archiver’,
‘ahrefsbot’, ‘semrushbot’, ‘mj12bot’, ‘dotbot’
];
foreach ( $bots as $bot ) {
if ( stripos($user_agent, $bot) !== false ) {
return; // bỏ qua bot
}
}$url = isset($_SERVER[‘REQUEST_URI’]) ? esc_url_raw($_SERVER[‘REQUEST_URI’]) : ”;
$referrer = isset($_SERVER[‘HTTP_REFERER’]) ? esc_url_raw($_SERVER[‘HTTP_REFERER’]) : ”;$wpdb->insert( $this->table, [
‘ip’ => $ip,
‘user_agent’ => $user_agent,
‘url’ => $url,
‘referrer’ => $referrer,
‘visit_time’ => current_time( ‘mysql’, 1 )
] );
}/** Thêm menu admin */
public function add_admin_page() {
add_menu_page(
‘Visitor Logs’,
‘Visitor Logs’,
‘manage_options’,
‘visitor-ip-logger’,
[ $this, ‘admin_page_html’ ],
‘dashicons-visibility’,
80
);
}/** Hiển thị bảng log trong admin kèm lọc theo ngày */
public function admin_page_html() {
if ( ! current_user_can( ‘manage_options’ ) ) return;
global $wpdb;$from_date = isset($_GET[‘from_date’]) ? sanitize_text_field($_GET[‘from_date’]) : ”;
$to_date = isset($_GET[‘to_date’]) ? sanitize_text_field($_GET[‘to_date’]) : ”;// Xuất CSV
if ( isset($_POST[‘export_csv’]) ) {
$where = “”;
if ( $from_date && $to_date ) {
$where = $wpdb->prepare(“WHERE visit_time BETWEEN %s AND %s”, $from_date . ” 00:00:00″, $to_date . ” 23:59:59″);
}
$results = $wpdb->get_results( “SELECT * FROM {$this->table} $where ORDER BY id DESC”, ARRAY_A );
if ( $results ) {
header(“Content-Type: text/csv”);
header(“Content-Disposition: attachment; filename=visitor_logs.csv”);
$out = fopen(“php://output”, “w”);
fputcsv($out, array_keys($results[0]));
foreach ($results as $row) fputcsv($out, $row);
fclose($out);
exit;
}
}// Xuất TXT (chỉ IP, unique)
if ( isset($_POST[‘export_txt’]) ) {
$where = “”;
if ( $from_date && $to_date ) {
$where = $wpdb->prepare(
“WHERE visit_time BETWEEN %s AND %s”,
$from_date . ” 00:00:00″,
$to_date . ” 23:59:59″
);
}$results = $wpdb->get_col( “SELECT DISTINCT ip FROM {$this->table} $where ORDER BY ip ASC” );
if ( $results ) {
// Xoá hết output trước khi gửi header (tránh lẫn HTML)
if ( ob_get_length() ) ob_end_clean();header(“Content-Type: text/plain; charset=utf-8”);
header(“Content-Disposition: attachment; filename=visitor_ips.txt”);foreach ( $results as $ip ) {
echo $ip . “\n”;
}
exit;
}
}// Lấy dữ liệu theo ngày
$where = “”;
if ( $from_date && $to_date ) {
$where = $wpdb->prepare(“WHERE visit_time BETWEEN %s AND %s”, $from_date . ” 00:00:00″, $to_date . ” 23:59:59″);
}$logs = $wpdb->get_results( “SELECT * FROM {$this->table} $where ORDER BY id DESC LIMIT 200″ );
echo ‘<div class=”wrap”><h1>Visitor IP Logs</h1>’;
// Form lọc theo ngày
echo ‘<form method=”get” style=”margin-bottom:15px”>’;
echo ‘<input type=”hidden” name=”page” value=”visitor-ip-logger”>’;
echo ‘Từ ngày: <input type=”date” name=”from_date” value=”‘ . esc_attr($from_date) . ‘”> ‘;
echo ‘Đến ngày: <input type=”date” name=”to_date” value=”‘ . esc_attr($to_date) . ‘”> ‘;
echo ‘<input type=”submit” class=”button” value=”Lọc”>’;
echo ‘</form>’;// Nút xuất file
echo ‘<form method=”post” style=”margin-bottom:15px; display:inline-block; margin-right:10px”>’;
echo ‘<button class=”button button-primary” name=”export_csv”>Xuất CSV</button>’;
echo ‘</form>’;echo ‘<form method=”post” style=”margin-bottom:15px; display:inline-block”>’;
echo ‘<button class=”button” name=”export_txt”>Xuất TXT (chỉ IP, unique)</button>’;
echo ‘</form>’;// Bảng log
echo ‘<table class=”widefat striped”><thead><tr>
<th>ID</th><th>IP</th><th>Loại</th><th>User Agent</th><th>URL</th><th>Referrer</th><th>Thời gian</th>
</tr></thead><tbody>’;if ( $logs ) {
foreach ( $logs as $log ) {
$type = ( strpos($log->ip, ‘:’) !== false ) ? ‘IPv6’ : ‘IPv4′;
echo “<tr>
<td>{$log->id}</td>
<td>{$log->ip}</td>
<td>{$type}</td>
<td style=’max-width:250px; word-wrap:break-word’>{$log->user_agent}</td>
<td>{$log->url}</td>
<td>{$log->referrer}</td>
<td>{$log->visit_time}</td>
</tr>”;
}
} else {
echo ‘<tr><td colspan=”7″>Chưa có dữ liệu</td></tr>’;
}echo ‘</tbody></table></div>’;
}}
new Visitor_IP_Logger();
Để lấy toàn bộ code của plugins chỉ việc cài đặt lên website wordpress các bạn hãy liên hệ với mình nhé:
- Tư vấn thiết kế website, seo website, đăng bài, đăng sản phẩm tự động trong wordpress uy tín, chất lượng, chuẩn SEO tại NBpage.com
- Website: https://nbpage.com
- Điện thoại/zalo: 0966.25.66.26 (Mr Dương).

