Bắt buộc khi tạo theme WordPress sẽ có các file bắt buộc là: style.css, functions.php, index.php tuỳ theo cấu trúc mỗi theme nhưng đây là 3 file bắt buộc phải có. Show
Ngoài ra cấu trúc theme WordPress còn có các file với chức năng tương ứng như list dưới đây.
File functions.php nằm ở đâu?File Cách 1: Đăng nhập vào website WordPress của bạn, vào phần Dashboard wp-admin bạn rê chuột vào phần Giao diện > Theme File Editor Khi click vào Theme File Editor bạn sẽ thấy bên tay phải sẽ có 1 file Cách 2: Đăng nhập vào tài khoản FTP hoặc Hosting > Vào thư mục chứa website và đi theo đường dẫn. /home/vutruso.com/public_html/wp-content/themes/ten-theme-ban-dang-dung Nhớ thay vutruso.com thành tên miền của bạn, và bạn cũng lưu ý tuỳ theo panel lý website trên hosting mà sẽ có vị trí lưu trữ website khác nhau. Theo đường dẫn ở trên bạn sẽ dễ dàng thấy được file Bạn dễ dàng tìm được file Code thông dụng trong file chức năng – functions.php WordPressKhai báo css và js cho theme// Register Style function vutruso_theme_assets() { wp_register_style( 'main_style', 'assets/css/styles.css', false, false ); wp_enqueue_style( 'main_style' ); wp_register_style( 'bootstrap-css', 'http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css', false, false ); wp_enqueue_style( 'bootstrap-css' ); } add_action( 'wp_enqueue_scripts', 'theme_assets' ); // Register Navigation Menus function vutruso_custom_navigation_menus() { $locations = array( );
register_nav_menus( $locations );
}
add_action( 'init', 'custom_navigation_menus' );3. Code thông báo bảo trìfunction vts_wp_maintenance_mode(){ }
add_action('get_header', 'vts_wp_maintenance_mode');4. Custom size ảnh cho WordPressadd_theme_support( 'post-thumbnails' ); add_image_size( 'supermarket-category-image', 380, 220, true ); add_image_size( 'supermarket-grid-product-image', 400, 480, true ); 5. Làm sạch file ảnh khi uploadNếu tên file ảnh chứa các ký tự đặc biệt sẽ bị loại bỏ, vd: // Register Style function vutruso_theme_assets() { wp_register_style( 'main_style', 'assets/css/styles.css', false, false ); wp_enqueue_style( 'main_style' ); wp_register_style( 'bootstrap-css', 'http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css', false, false ); wp_enqueue_style( 'bootstrap-css' ); } add_action( 'wp_enqueue_scripts', 'theme_assets' ); 4 sẽ được đổi thành // Register Style function vutruso_theme_assets() { wp_register_style( 'main_style', 'assets/css/styles.css', false, false ); wp_enqueue_style( 'main_style' ); wp_register_style( 'bootstrap-css', 'http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css', false, false ); wp_enqueue_style( 'bootstrap-css' ); } add_action( 'wp_enqueue_scripts', 'theme_assets' ); 5 /
6. Xóa bỏ code svg trong thẻ body từ WordPress 5.9Xóa bỏ code svg trong thẻ body từ WordPress 5.9 thì bạn có thể xem chi tiết tại đây 7. Tắt XML PRCMặc định XML PRC bật, nhưng bạn nên tắt tính năng này đi vì nó không giúp ích mà còn gây hại cho website add_filter('xmlrpc_enabled', '__return_false'); Function bật classic widget thay cho block widget/ Classic widget ===============================================================*/ add_filter( 'gutenberg_use_widgets_block_editor', '__return_false' ); add_filter( 'use_widgets_block_editor', '__return_false' ); Bật Classic Editor thay cho Gutenberg/ Classic editor ===============================================================*/ add_filter('use_block_editor_for_post', '__return_false', 10); add_filter( 'use_widgets_blog_editor', '__return_false' ); add_action( 'wp_enqueue_scripts', function() { }, 20 );Ở trên là 1 số hàm thông dụng khi tạo theme WordPress, nếu bạn muốn thêm code các tính năng khác bạn có thể truy cập vào đây để xem CODE SNIPPETS khác. |