Hoàng Web

Thiết Kế Website WordPress

  • Kho giao diện
  • Dịch Vụ
    • Thiết kế web giá rẻ
    • Thiết kế website WordPress
    • Hosting Miễn Phí 100GB
    • Tích hợp thanh toán MoMo, ViettelPay, Vietcombank, MB..
    • Tối ưu Google PageSpeed
    • Sửa lỗi nâng cấp website
    • Viết plugin WordPress
    • Code Tool theo yêu cầu
  • Bảng giá
  • Quy trình làm việc
  • Giới thiệu
  • Liên Lạc
Trang chủ » Wordpress » Chèn file javascript css ở mọi trang trong WordPress

Chèn file javascript css ở mọi trang trong WordPress

Thứ Năm, 03/07/2014 by Hoàng Quách

  • shares
  • Facebook
  • Facebook Messenger
  • Gmail
  • Viber
  • Skype

Trong tham số callback của hook admin_enqueue_scripts có tham số cho biết đang ở địa chỉ page nào, có thể sử dụng nó để load scripts vào trang cụ thể.

function my_enqueue($hook) {
    if( 'edit.php' != $hook )
        return;
    wp_enqueue_script( 'my_custom_script', plugin_dir_url( __FILE__ ) . '/myscript.js' );
}
#enqueuing on admin pages
add_action( 'admin_enqueue_scripts', 'my_enqueue' );

Nếu chỉ muốn load script vào trang admin (backend). Sử dụng action admin_init thay vì init.

add_action( 'admin_init', 'my_plugin_admin_init' );
function my_plugin_admin_init() {
        /* Register our script. */
        wp_register_script( 'my-plugin-script', plugins_url( '/script.js', __FILE__ ) );
    }

Bạn cũng có thể sử dụng chi tiết hook admin_print_scripts cho một page trong admin, Xem ví dụ sau:

add_action( 'admin_menu', 'my_plugin_admin_menu' );
function my_plugin_admin_menu() {
        /* Add our plugin submenu and administration screen */
        $page_hook_suffix = add_submenu_page( 'edit.php', // The parent page of this submenu
                                  __( 'My Plugin', 'myPlugin' ), // The submenu title
                                  __( 'My Plugin', 'myPlugin' ), // The screen title
				  'manage_options', // The capability required for access to this submenu
				  'my_plugin-options', // The slug to use in the URL of the screen
                                  'my_plugin_manage_menu' // The function to call to display the screen
                               );
 
        /*
          * Use the retrieved $page_hook_suffix to hook the function that links our script.
          * This hook invokes the function only on our plugin administration screen,
          * see: http://codex.wordpress.org/Administration_Menus#Page_Hook_Suffix
          */
        add_action('admin_print_scripts-' . $page_hook_suffix, 'my_plugin_admin_scripts');
    }
 
    function my_plugin_admin_scripts() {
        /* Link our already registered script to a page */
        wp_enqueue_script( 'my-plugin-script' );
    }
 
    function my_plugin_manage_menu() {
        /* Display our administration screen */
    }

Để nhận được bài viết mới vui lòng đăng ký kênh kiến thức WordPress từ A-Z ở Form bên dưới. Bạn cũng có thể nhận được sự trợ giúp trên Twitter và Facebook

  • shares
  • Facebook
  • Facebook Messenger
  • Gmail
  • Viber
  • Skype

Chuyên mục: Wordpress Tìm kiếm: Javascript, wordpress action filters hooks

Tôi giúp gì cho bạn?

HOÀNG WEB

Địa chỉ: Tây Sơn, Phường Quang Trung, Quận Đống Đa, Hà Nội

Hotline: 0987 342 124 – 0868 292 303 (8h:00 – 21h:00)

Email: [email protected]

Website: www.hoangweb.com

KẾT NỐI VỚI TÔI

  • Facebook
  • GitHub
  • YouTube

SẢN PHẨM

  • Plugin Thanh Toán Quét Mã QR Code Tự Động
  • WP2Speed – Tối ưu Google Speed
  • 23WebHost – Hosting Miễn Phí 100GB

LIÊN KẾT

  • Có nên thuê thiết kế website giá rẻ?
  • Hướng dẫn thanh toán
  • Chính sách hoàn tiền
  • Trung tâm hỗ trợ

Copyright © 2023 | All rights reserved | HOANG WEB
Mọi hình thức sao chép nội dung trên website này mà chưa được sự đồng ý đều là trái phép.