Tập tin functions.php được hiểu ngầm như là một tập tin thực thi một số hàm đặc biệt thêm vào theme để có thể thêm nhiều tính năng hay hơn. Từ các bước cơ bản như tạo sidebar, menu, thêm size ảnh cho đến các bước phức tạp khác thì bạn phải làm trong tập tin functions.php này mà không thể làm ở một tập tin nào khác, nếu có thì cũng phải include vào.

Một số thủ thuật tối ưu WordPress tại functions.php

Dưới đây là một số funtions cần thiết để tối ưu WordPress hơn.

1. Đưa tất cả JavaScript xuống Footer

Mặc định các thư viện JavaScript sẽ được đặt bên trong thẻ <head></head>, để có thể đặt nó nằm ở phía dưới footer các bạn sử dụng đoạn code như sau.

/**
 * Automatically move JavaScript code to page footer, speeding up page loading time.
 */
function remove_head_scripts() {
   remove_action('wp_head', 'wp_print_scripts');
   remove_action('wp_head', 'wp_print_head_scripts', 9);
   remove_action('wp_head', 'wp_enqueue_scripts', 1);
 
   add_action('wp_footer', 'wp_print_scripts', 5);
   add_action('wp_footer', 'wp_enqueue_scripts', 5);
   add_action('wp_footer', 'wp_print_head_scripts', 5);
}
add_action('wp_enqueue_scripts', 'remove_head_scripts');

2. Tắt tính năng Emoji

Tính năng này sẽ tải thêm một script riêng cho Emoji và thế là website lại phải tốn một xíu thời gian để tải.

function disable_emojicons_tinymce($plugins) {
    if (is_array($plugins)) {
      return array_diff($plugins, array('wpemoji'));
    } else {
      return array();
    }
}

function disable_wp_emojicons() {
    remove_action('admin_print_styles', 'print_emoji_styles');
    remove_action('wp_head', 'print_emoji_detection_script', 7);
    remove_action('admin_print_scripts', 'print_emoji_detection_script');
    remove_action('wp_print_styles', 'print_emoji_styles');
    remove_filter('wp_mail', 'wp_staticize_emoji_for_email');
    remove_filter('the_content_feed', 'wp_staticize_emoji');
    remove_filter('comment_text_rss', 'wp_staticize_emoji');
    add_filter('tiny_mce_plugins', 'disable_emojicons_tinymce');
}
add_action('init', 'disable_wp_emojicons');

3. Sử dụng jQuery từ Google Hosted Libraries

WordPress tự động tải jQuery từ thư mục wp-includes/js/jquery/jquery.js. Tuy nhiên trong một số trường hợp bạn không muốn tải từ thư mục này mà bạn muốn lấy jQuery trực tiếp từ Google Hosted Libraries thì bạn nên viết lại như thế này.

function modify_jquery() {
   if (!is_admin()) {
      wp_deregister_script('jquery');
      wp_register_script('jquery', 'https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js', false, null);
      wp_enqueue_script('jquery');
   }
}
add_action('init', 'modify_jquery');

4. Ẩn phiên bản WordPress

Nếu bạn thường không cập nhật hệ thống WordPress, thì các thông tin về phiên bản hệ thống bạn đang sử dụng cũng có thể mang lại những mối nguy nhất định. Thường thì các bản cập nhật chương trình ngoài việc bổ sung các tính năng mới, thì còn nhằm sửa các lỗ hổng bảo mật đã phát hiện trước đó. Bây giờ kẻ xấu biết được thông tin phiên bản WordPress của bạn thì hắn sẽ tìm danh sách các lỗ hỗng liên quan đến phiên bản đó xem có khai thác được không.

Để hạn chế rủi ro, bạn có thể thêm đoạn mã sau.

// Remove version from head
remove_action('wp_head', 'wp_generator');

// Remove version from rss
add_filter('the_generator', '__return_empty_string');

// Remove version from scripts and styles
function remove_version_scripts_styles($src) {
    if (strpos($src, 'ver=')) {
        $src = remove_query_arg('ver', $src);
    }
    return $src;
}
add_filter('style_loader_src', 'remove_version_scripts_styles', 9999);
add_filter('script_loader_src', 'remove_version_scripts_styles', 9999);

Chúc các bạn thành công!