Khi website WordPress có hàng trăm nghìn đến hàng triệu bài viết, việc thao tác qua /wp-admin/
trở nên cực kỳ chậm chạp. Mỗi request phải tải hàng trăm file PHP, khởi tạo toàn bộ plugin, theme và hệ thống hook.
Giải pháp tối ưu là bỏ qua admin mặc định, và include trực tiếp wp-load.php
để viết admin riêng, hoặc chạy các script riêng (cron, import, sync, thống kê, …).
/wp-admin/
Khi truy cập WordPress Admin, hệ thống sẽ:
>200
file PHP)admin_init
, admin_menu
, …)wp_options
Hậu quả:
⚠️ Với dữ liệu lớn (hàng trăm nghìn bản ghi), mỗi thao tác có thể tốn hàng giây, CPU cao, RAM tăng mạnh.
wp-load.php
Thay vì dùng /wp-admin/
, bạn có thể tạo file PHP riêng và nạp môi trường WordPress tối thiểu.
Ví dụ:
<?php require_once '/var/www/domain.com/public/wp-load.php'; global $wpdb; $results = $wpdb->get_results(" SELECT ID, post_title FROM wp_posts WHERE post_type='post' AND post_status='publish' ORDER BY ID DESC LIMIT 100 "); foreach ($results as $r) { echo $r->ID . ' - ' . $r->post_title . '<br>'; }
✅ Ưu điểm:
get_post()
, update_post_meta()
, …)wp-admin
3–10 lầnSHORTINIT
Đây là “vũ khí bí mật” của WordPress:
define('SHORTINIT', true); require_once '/var/www/domain.com/public/wp-load.php';
SHORTINIT
là gì?SHORTINIT
báo cho WordPress rằng:
“Chỉ khởi tạo phần tối thiểu, bỏ qua plugin, theme, REST API, hooks…”
Khi bật SHORTINIT
, WordPress chỉ load các file nền tảng như:
load.php
default-constants.php
plugin.php
formatting.php
meta.php
functions.php
class-wp-error.php
l10n.php
capabilities.php
Tức là:
$wpdb
, esc_sql()
, sanitize_text_field()
…wp_insert_post()
, get_post()
, wp_schedule_event()
, …SHORTINIT
Nếu bạn cần thêm bài viết, cập nhật meta hoặc user, chỉ cần include thêm các file core tương ứng:
require_once ABSPATH . WPINC . '/post.php'; require_once ABSPATH . WPINC . '/meta.php'; require_once ABSPATH . WPINC . '/taxonomy.php'; require_once ABSPATH . WPINC . '/user.php'; require_once ABSPATH . WPINC . '/pluggable.php';
Những file này chứa:
wp_insert_post()
, wp_update_post()
update_post_meta()
, add_post_meta()
wp_set_post_terms()
wp_get_current_user()
SHORTINIT
<?php // Bật chế độ SHORTINIT define('SHORTINIT', true); require_once '/var/www/domain.com/public/wp-load.php'; // Nạp các file cần thiết require_once ABSPATH . WPINC . '/post.php'; require_once ABSPATH . WPINC . '/meta.php'; require_once ABSPATH . WPINC . '/taxonomy.php'; require_once ABSPATH . WPINC . '/user.php'; require_once ABSPATH . WPINC . '/pluggable.php'; global $wpdb; // Chuẩn bị dữ liệu $new_post = array( 'post_title' => 'Bài viết test SHORTINIT', 'post_content' => 'Nội dung bài viết được thêm bằng script riêng.', 'post_status' => 'publish', 'post_author' => 1, 'post_type' => 'post' ); // Insert bài viết $post_id = wp_insert_post($new_post, true); if (is_wp_error($post_id)) { echo "❌ Lỗi khi insert: " . $post_id->get_error_message(); } else { echo "✅ Thêm bài thành công, ID = $post_id"; }
Kết quả:
wp_update_post(['ID' => $post_id, 'post_name' => 'bai-test-shortinit']); update_post_meta($post_id, '_thumbnail_id', 123); wp_set_post_terms($post_id, array('Tin nhanh', 'Xe cộ'), 'category');
Các hàm trên vẫn hoạt động trong môi trường SHORTINIT
, miễn là bạn đã load đúng file core.
SHORTINIT
Tình huống | Có nên dùng |
---|---|
Cron job, import, export dữ liệu | ✅ Rất nên |
Dashboard admin riêng, không cần UI WP | ✅ Nên |
Script sync bài viết giữa các site | ✅ Tốt |
Plugin/theme thông thường | ⚠️ Không cần |
Frontend hiển thị cho người dùng | ❌ Không nên |
Cách chạy | Thời gian load | Tài nguyên | Ghi chú |
---|---|---|---|
/wp-admin/ mặc định | 400–800 ms | Rất nặng | Load toàn bộ WP |
include wp-load.php | 150–250 ms | Trung bình | Không có UI |
SHORTINIT + load core | 50–100 ms | Rất nhẹ | Dành cho script tùy chỉnh |
define('SHORTINIT', true);
là một kỹ thuật tối ưu mạnh mẽ cho developer WordPress chuyên xử lý dữ liệu lớn hoặc viết hệ thống riêng.
Kết hợp SHORTINIT
+ wp-load.php
, bạn có thể:
👉 Tóm lại: “Nếu WordPress của bạn có hơn 100.000 bài viết, hãy tách admin riêng bằng SHORTINIT — và bạn sẽ thấy WordPress thật sự nhanh.”