Đăng nhập

WordPress Developer Cheatsheet (Cập nhật 2025)

Dưới đây là một bản WordPress Developer Cheatsheet – tập hợp các đoạn mã và kỹ thuật thường dùng nhất khi phát triển plugin, theme, hoặc tùy biến website WordPress. Bản này được sắp xếp rõ ràng, dễ tra cứu cho cả người mới và người đã có kinh nghiệm.


1. Các Biến Toàn Cục Quan Trọng

global $wpdb;        // Đối tượng thao tác với database
global $post;        // Đối tượng bài viết hiện tại (trong Loop)
global $current_user; // Thông tin user đang đăng nhập

2. Hook Cơ Bản

Actions (thực thi hành động)

add_action('init', 'my_function');
add_action('wp_footer', 'add_footer_script');

Filters (can thiệp & sửa nội dung)

add_filter('the_content', 'filter_content');

3. Truy Vấn Cơ Sở Dữ Liệu với $wpdb

global $wpdb;

$results = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}posts");
$wpdb->insert($wpdb->prefix . 'my_table', ['name' => 'John'], ['%s']);
$wpdb->update($wpdb->prefix . 'my_table', ['name' => 'New'], ['id' => 1]);
$wpdb->delete($wpdb->prefix . 'my_table', ['id' => 1]);

4. Thêm Menu Trang Admin

add_action('admin_menu', 'my_admin_menu');
function my_admin_menu() {
    add_menu_page('Tên trang', 'Menu Admin', 'manage_options', 'slug-trang', 'ten_callback');
}

5. Đăng ký Shortcode

add_shortcode('hello', function() {
    return 'Hello World!';
});

6. Làm việc với Option

add_option('my_option', 'giá trị');
update_option('my_option', 'giá trị mới');
get_option('my_option');
delete_option('my_option');

7. Thêm Script & Style

add_action('wp_enqueue_scripts', 'my_enqueue');
function my_enqueue() {
    wp_enqueue_style('my-style', get_template_directory_uri() . '/style.css');
    wp_enqueue_script('my-js', get_template_directory_uri() . '/script.js', [], false, true);
}

8. Đăng ký Post Type

register_post_type('sach', [
    'label' => 'Sách',
    'public' => true,
    'supports' => ['title', 'editor', 'thumbnail'],
    'has_archive' => true,
]);

9. Đăng ký Taxonomy

register_taxonomy('the_loai', 'sach', [
    'label' => 'Thể loại',
    'hierarchical' => true,
    'public' => true,
]);

10. Loop Tuỳ Biến

$args = ['post_type' => 'sach', 'posts_per_page' => 5];
$query = new WP_Query($args);
while ($query->have_posts()) {
    $query->the_post();
    the_title();
    the_content();
}
wp_reset_postdata();

11. Hook kích hoạt plugin

register_activation_hook(__FILE__, 'my_plugin_activate');
function my_plugin_activate() {
    // Code tạo bảng database, thêm option, v.v.
}

12. Tạo bảng với dbDelta()

require_once ABSPATH . 'wp-admin/includes/upgrade.php';
$sql = "CREATE TABLE {$wpdb->prefix}ten_bang (
    id INT NOT NULL AUTO_INCREMENT,
    name VARCHAR(255),
    PRIMARY KEY (id)
) {$wpdb->get_charset_collate()};";

dbDelta($sql);

13. Kiểm tra quyền user

if (current_user_can('manage_options')) {
    // Chỉ admin mới vào được
}

14. Thêm Widget Tùy Biến

class My_Widget extends WP_Widget {
    function __construct() {
        parent::__construct('my_widget', 'My Widget');
    }
    function widget($args, $instance) {
        echo 'Hello from widget!';
    }
}
add_action('widgets_init', function() {
    register_widget('My_Widget');
});

15. Lấy URL & đường dẫn thường dùng

site_url();                  // https://example.com
home_url();                  // URL trang chủ
admin_url();                 // https://example.com/wp-admin/
get_template_directory_uri() // URL thư mục theme
plugins_url();               // URL thư mục plugins

Thảo luận

1 bình luận

  • A WordPress Commenter

    Tháng 7 19, 2025

    Hi, this is a comment.
    To get started with moderating, editing, and deleting comments, please visit the Comments screen in the dashboard.
    Commenter avatars come from Gravatar.

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *

Đăng ký nhận tin mới

Nhận bài học, tài nguyên và cơ hội việc làm qua email hàng tuần.

Chúng tôi cam kết không spam. Bạn có thể hủy bất cứ lúc nào.