Hướng dẫn how do i move javascript to wordpress footer? - làm cách nào để chuyển javascript sang footer wordpress?

  • Thông tin chi tiết
  • Đánh giá
  • Cài đặt
  • Ủng hộ
  • Sự phát triển

Plugin nhỏ [2 kb] này di chuyển JavaScript đến chân trang và cải thiện thời gian tải trang. Lưu ý: Điều này chỉ hoạt động nếu các plugin và chủ đề khác của bạn thêm chính xác JavaScripts.
Note: this only works if your other plugins and theme add the JavaScripts correctly.

Cấu hình không bắt buộc! Bạn chỉ cần cài đặt nó và sau khi plugin thực hiện tất cả, không có hành động nào nữa mà nó yêu cầu.

  1. Tải thư mục hoàn chỉnh javascript-to-footer lên thư mục /wp-content/plugins/
  2. Kích hoạt plugin thông qua menu ‘plugin trong WordPress

Tôi đã thử nó với chủ đề Divi và nó thực sự làm cho trang web chậm hơn.

Spero Che Si Aggiorni Presto All'ultima Phiên bản WP, Ottimo Lavoro!

Một lời cảm ơn lớn đến ông Simone Nigro cho plugin nhẹ tuyệt vời này.

Làm cho trang web ngay lập tức nhanh hơn. Quá tệ, nó cũng không tối ưu hóa CSS 'trên nếp gấp', điều đó sẽ rất tuyệt.

Làm chính xác những gì nó được cho là và không có gì hơn. Rất, rất gầy!

Tôi đã cài đặt 3 trường hợp "hiển thị chặn JavaScript", xóa bộ nhớ cache của tôi và cả 3 lỗi trên bài kiểm tra tốc độ trang Google đã được sửa! Cảm ơn vì đã tiết kiệm cho tôi một thời gian!

Đọc tất cả 6 đánh giá

Tốc độ tăng tốc - JavaScript đến Footer là phần mềm nguồn mở. Những người sau đây đã đóng góp cho plugin này.

Người đóng góp

  • Nigro.simone

1.0.10

  • Đã thử nghiệm lên đến WordPress 5.9

1.0.9

  • Đã thử nghiệm lên đến WordPress 5.7

1.0.8

  • Đã thử nghiệm lên đến WordPress 5.5

1.0.7

  • Readme

1.0.6

  • Đã thử nghiệm lên đến WordPress 5.3

1.0.5

  • Đã kiểm tra lên đến WordPress 5.2

1.0.4

  • Đã thử nghiệm lên đến WordPress 4.9

1.0.3

  • Khắc phục plugin uri
  • Thêm preload scritp vào đầu

1.0.2

  • Đã thử nghiệm lên đến WordPress 4.7

1.0.1

  • Khắc phục: Chỉ di chuyển scritps sang chân trang chứ không phải kiểu.

1.0.0

  • Phát hành lần đầu.

IMHO, tôi vẫn nghĩ rằng việc tải các tập lệnh và kiểu trực tiếp trong tiêu đề là thực tiễn xấu vì nó luôn luôn là một vấn đề khi loại bỏ chúng và tải chúng theo cách thức.

Cách tốt nhất để làm việc xung quanh điều này là tạo một [chủ đề con] và sau đó sao chép header.php vào chủ đề con của bạn. WordPress sẽ tải tiêu đề của chủ đề con bạn thay vì tiêu đề của chủ đề mẹ.

Bây giờ bạn có thể xóa tất cả các tập lệnh khỏi tiêu đề của mình và enqueue đúng cách và đăng ký chúng thông qua hook wp_enqueue_scripts trong chủ đề con của bạn functions.php. Chỉ cần nhớ đặt tham số $in_footer trong các hàm wp_enqueue_script[]

add_action['wp_enqueue_scripts', 'enqueue_my_scripts', PHP_INT_MAX];

function enqueue_my_scripts[] 
{

    /**
     * Make sure, get_template_directory_uri[] if script stays in parent theme
     * Use get_stylesheet_directory_uri[] if script is in child theme
    */ 
    wp_enqueue_script['jquery-min', get_template_directory_uri[] . '/js/jquery-1.7.2.min.js', array[], false, true];
    wp_enqueue_script['jquery-tools', get_template_directory_uri[] . '/js/jquery.tools.js-1.2.7.min.js', array['jquery-min'], false, true];
    wp_enqueue_script['prefixfree', get_template_directory_uri[] . '/js/prefixfree-1.0.6.min.js', array[], false, true];
    wp_enqueue_script['modernizr', get_template_directory_uri[] . '/js/modernizr.js', array[], false, true];

    /**
     * The two conditional scripts which there is no work around for, load them or drop support
    */ 
    wp_enqueue_script['html5shiv', get_template_directory_uri[] . '/js/html5shiv.js', array[], false, true];
    wp_enqueue_script['selectivizr', get_template_directory_uri[] . '/js/selectivizr-1.0.2.min.js', array[], false, true];

    wp_enqueue_script['acciones', get_template_directory_uri[] . '/js/acciones.js', array[], false, true];
    wp_enqueue_script['openx', '//openx.elclarinweb.com/www/delivery/spcjs.php?id=2&target=_blank', array[], false, true];
    wp_enqueue_script['analytics', get_stylesheet_directory_uri[] . '/js/analytics.script.js', array[], false, true];

}
0 thành `true

CHỈNH SỬA

Từ header.php được liên kết của bạn, các tập lệnh của bạn được thêm vào giữa các dòng 56 - 95. Điều này bạn sẽ cần phải xóa. Nếu bạn truy cập trang web, bạn sẽ không thấy bất kỳ jQuery nào đang được tải.

ThenBuild trong thư viện jQuery đã được tải, không cần phải lo lắng về điều đó. Phần còn lại bạn cần để tự mình. Đây là một ví dụ [hãy nhớ, mỗi tập lệnh nên có một tay cầm duy nhất, vì vậy hãy đặt tên cho chúng một thứ gì đó duy nhất sẽ không tạo ra xung đột]

add_action['wp_enqueue_scripts', 'enqueue_my_scripts'];

function enqueue_my_scripts[] 
{
    wp_enqueue_script['jquery-tools', get_template_directory_uri[] . '/js/jquery.tools.js-1.2.7.min.js', array['jquery'], false, true];
    //Do the same for the other scripts
}

Lưu ý rằng tôi đã sử dụng

add_action['wp_enqueue_scripts', 'enqueue_my_scripts', PHP_INT_MAX];

function enqueue_my_scripts[] 
{

    /**
     * Make sure, get_template_directory_uri[] if script stays in parent theme
     * Use get_stylesheet_directory_uri[] if script is in child theme
    */ 
    wp_enqueue_script['jquery-min', get_template_directory_uri[] . '/js/jquery-1.7.2.min.js', array[], false, true];
    wp_enqueue_script['jquery-tools', get_template_directory_uri[] . '/js/jquery.tools.js-1.2.7.min.js', array['jquery-min'], false, true];
    wp_enqueue_script['prefixfree', get_template_directory_uri[] . '/js/prefixfree-1.0.6.min.js', array[], false, true];
    wp_enqueue_script['modernizr', get_template_directory_uri[] . '/js/modernizr.js', array[], false, true];

    /**
     * The two conditional scripts which there is no work around for, load them or drop support
    */ 
    wp_enqueue_script['html5shiv', get_template_directory_uri[] . '/js/html5shiv.js', array[], false, true];
    wp_enqueue_script['selectivizr', get_template_directory_uri[] . '/js/selectivizr-1.0.2.min.js', array[], false, true];

    wp_enqueue_script['acciones', get_template_directory_uri[] . '/js/acciones.js', array[], false, true];
    wp_enqueue_script['openx', '//openx.elclarinweb.com/www/delivery/spcjs.php?id=2&target=_blank', array[], false, true];
    wp_enqueue_script['analytics', get_stylesheet_directory_uri[] . '/js/analytics.script.js', array[], false, true];

}
2 ở đây vì bạn sẽ để lại thư mục JS của bạn trong chủ đề cha mẹ của bạn. Tuy nhiên, bạn có thể chuyển thư mục JS của bạn sang chủ đề con của bạn, nhưng sau đó bạn sẽ cần sử dụng
add_action['wp_enqueue_scripts', 'enqueue_my_scripts', PHP_INT_MAX];

function enqueue_my_scripts[] 
{

    /**
     * Make sure, get_template_directory_uri[] if script stays in parent theme
     * Use get_stylesheet_directory_uri[] if script is in child theme
    */ 
    wp_enqueue_script['jquery-min', get_template_directory_uri[] . '/js/jquery-1.7.2.min.js', array[], false, true];
    wp_enqueue_script['jquery-tools', get_template_directory_uri[] . '/js/jquery.tools.js-1.2.7.min.js', array['jquery-min'], false, true];
    wp_enqueue_script['prefixfree', get_template_directory_uri[] . '/js/prefixfree-1.0.6.min.js', array[], false, true];
    wp_enqueue_script['modernizr', get_template_directory_uri[] . '/js/modernizr.js', array[], false, true];

    /**
     * The two conditional scripts which there is no work around for, load them or drop support
    */ 
    wp_enqueue_script['html5shiv', get_template_directory_uri[] . '/js/html5shiv.js', array[], false, true];
    wp_enqueue_script['selectivizr', get_template_directory_uri[] . '/js/selectivizr-1.0.2.min.js', array[], false, true];

    wp_enqueue_script['acciones', get_template_directory_uri[] . '/js/acciones.js', array[], false, true];
    wp_enqueue_script['openx', '//openx.elclarinweb.com/www/delivery/spcjs.php?id=2&target=_blank', array[], false, true];
    wp_enqueue_script['analytics', get_stylesheet_directory_uri[] . '/js/analytics.script.js', array[], false, true];

}
3

Ghi chú về các tập lệnh có điều kiện

Vẫn còn, sau bốn năm sau khi được nâng lên, không có cách xây dựng nào để tải các tập lệnh một cách có điều kiện theo trình duyệt IE như có cho các kiểu. Bạn có thể kiểm tra vé Trac và một câu hỏi khác đưa ra vấn đề tương tự ở đây

Tôi chưa bao giờ thử tải các tập lệnh một cách có điều kiện theo trình duyệt, vì vậy tôi không thể bình luận về phần này hoặc nêu rõ liệu bất kỳ công việc giải pháp nào được đề cập trong câu trả lời được liên kết hoặc vé TRAC. Những gì tôi có thể nói với bạn, nếu các giải pháp không hoạt động, bạn sẽ cần sao chép

add_action['wp_enqueue_scripts', 'enqueue_my_scripts', PHP_INT_MAX];

function enqueue_my_scripts[] 
{

    /**
     * Make sure, get_template_directory_uri[] if script stays in parent theme
     * Use get_stylesheet_directory_uri[] if script is in child theme
    */ 
    wp_enqueue_script['jquery-min', get_template_directory_uri[] . '/js/jquery-1.7.2.min.js', array[], false, true];
    wp_enqueue_script['jquery-tools', get_template_directory_uri[] . '/js/jquery.tools.js-1.2.7.min.js', array['jquery-min'], false, true];
    wp_enqueue_script['prefixfree', get_template_directory_uri[] . '/js/prefixfree-1.0.6.min.js', array[], false, true];
    wp_enqueue_script['modernizr', get_template_directory_uri[] . '/js/modernizr.js', array[], false, true];

    /**
     * The two conditional scripts which there is no work around for, load them or drop support
    */ 
    wp_enqueue_script['html5shiv', get_template_directory_uri[] . '/js/html5shiv.js', array[], false, true];
    wp_enqueue_script['selectivizr', get_template_directory_uri[] . '/js/selectivizr-1.0.2.min.js', array[], false, true];

    wp_enqueue_script['acciones', get_template_directory_uri[] . '/js/acciones.js', array[], false, true];
    wp_enqueue_script['openx', '//openx.elclarinweb.com/www/delivery/spcjs.php?id=2&target=_blank', array[], false, true];
    wp_enqueue_script['analytics', get_stylesheet_directory_uri[] . '/js/analytics.script.js', array[], false, true];

}
4 vào chủ đề con của bạn và sau đó di chuyển các dòng 61 -66 từ tiêu đề của bạn sang chân trang của bạn

Ghi chú trên các dòng kịch bản 67 -69

Các phần này chuyển một biến PHP cho jQuery. Cách chính xác để làm điều này sẽ là sử dụng

add_action['wp_enqueue_scripts', 'enqueue_my_scripts', PHP_INT_MAX];

function enqueue_my_scripts[] 
{

    /**
     * Make sure, get_template_directory_uri[] if script stays in parent theme
     * Use get_stylesheet_directory_uri[] if script is in child theme
    */ 
    wp_enqueue_script['jquery-min', get_template_directory_uri[] . '/js/jquery-1.7.2.min.js', array[], false, true];
    wp_enqueue_script['jquery-tools', get_template_directory_uri[] . '/js/jquery.tools.js-1.2.7.min.js', array['jquery-min'], false, true];
    wp_enqueue_script['prefixfree', get_template_directory_uri[] . '/js/prefixfree-1.0.6.min.js', array[], false, true];
    wp_enqueue_script['modernizr', get_template_directory_uri[] . '/js/modernizr.js', array[], false, true];

    /**
     * The two conditional scripts which there is no work around for, load them or drop support
    */ 
    wp_enqueue_script['html5shiv', get_template_directory_uri[] . '/js/html5shiv.js', array[], false, true];
    wp_enqueue_script['selectivizr', get_template_directory_uri[] . '/js/selectivizr-1.0.2.min.js', array[], false, true];

    wp_enqueue_script['acciones', get_template_directory_uri[] . '/js/acciones.js', array[], false, true];
    wp_enqueue_script['openx', '//openx.elclarinweb.com/www/delivery/spcjs.php?id=2&target=_blank', array[], false, true];
    wp_enqueue_script['analytics', get_stylesheet_directory_uri[] . '/js/analytics.script.js', array[], false, true];

}
5. Bạn sẽ cần liên hệ với nhà phát triển ở đây để được hỗ trợ vì đây là chủ đề thẳng thắn liên quan và tôi thực sự không biết nơi này thực sự được sử dụng trong các kịch bản của bạn. Kiểm tra liên kết cũng để sử dụng và thông tin

Ghi chú về các dòng Phân tích 75 - 94

Bạn sẽ cần tạo một tệp JS cho phần này. Nếu bạn chưa sao chép thư mục JS từ chủ đề phụ huynh sang con bạn, hãy tạo thư mục JS mới cho chủ đề con của bạn. Mở nó ra và tạo một tệp JS mới và gọi nó là những gì bạn thích, một cái gì đó như

add_action['wp_enqueue_scripts', 'enqueue_my_scripts', PHP_INT_MAX];

function enqueue_my_scripts[] 
{

    /**
     * Make sure, get_template_directory_uri[] if script stays in parent theme
     * Use get_stylesheet_directory_uri[] if script is in child theme
    */ 
    wp_enqueue_script['jquery-min', get_template_directory_uri[] . '/js/jquery-1.7.2.min.js', array[], false, true];
    wp_enqueue_script['jquery-tools', get_template_directory_uri[] . '/js/jquery.tools.js-1.2.7.min.js', array['jquery-min'], false, true];
    wp_enqueue_script['prefixfree', get_template_directory_uri[] . '/js/prefixfree-1.0.6.min.js', array[], false, true];
    wp_enqueue_script['modernizr', get_template_directory_uri[] . '/js/modernizr.js', array[], false, true];

    /**
     * The two conditional scripts which there is no work around for, load them or drop support
    */ 
    wp_enqueue_script['html5shiv', get_template_directory_uri[] . '/js/html5shiv.js', array[], false, true];
    wp_enqueue_script['selectivizr', get_template_directory_uri[] . '/js/selectivizr-1.0.2.min.js', array[], false, true];

    wp_enqueue_script['acciones', get_template_directory_uri[] . '/js/acciones.js', array[], false, true];
    wp_enqueue_script['openx', '//openx.elclarinweb.com/www/delivery/spcjs.php?id=2&target=_blank', array[], false, true];
    wp_enqueue_script['analytics', get_stylesheet_directory_uri[] . '/js/analytics.script.js', array[], false, true];

}
6.

Tiếp theo, bạn sẽ di chuyển mọi thứ bên trong thẻ tập lệnh vào thư mục này, đây là dòng 77 - 92. Hãy chắc chắn sử dụng trình bao bọc xung đột để bọc tập lệnh này như được mô tả ở đây

Bạn chỉ có thể thực hiện tập lệnh này như bình thường như mô tả trước đó, chỉ cần nhớ sử dụng

add_action['wp_enqueue_scripts', 'enqueue_my_scripts', PHP_INT_MAX];

function enqueue_my_scripts[] 
{

    /**
     * Make sure, get_template_directory_uri[] if script stays in parent theme
     * Use get_stylesheet_directory_uri[] if script is in child theme
    */ 
    wp_enqueue_script['jquery-min', get_template_directory_uri[] . '/js/jquery-1.7.2.min.js', array[], false, true];
    wp_enqueue_script['jquery-tools', get_template_directory_uri[] . '/js/jquery.tools.js-1.2.7.min.js', array['jquery-min'], false, true];
    wp_enqueue_script['prefixfree', get_template_directory_uri[] . '/js/prefixfree-1.0.6.min.js', array[], false, true];
    wp_enqueue_script['modernizr', get_template_directory_uri[] . '/js/modernizr.js', array[], false, true];

    /**
     * The two conditional scripts which there is no work around for, load them or drop support
    */ 
    wp_enqueue_script['html5shiv', get_template_directory_uri[] . '/js/html5shiv.js', array[], false, true];
    wp_enqueue_script['selectivizr', get_template_directory_uri[] . '/js/selectivizr-1.0.2.min.js', array[], false, true];

    wp_enqueue_script['acciones', get_template_directory_uri[] . '/js/acciones.js', array[], false, true];
    wp_enqueue_script['openx', '//openx.elclarinweb.com/www/delivery/spcjs.php?id=2&target=_blank', array[], false, true];
    wp_enqueue_script['analytics', get_stylesheet_directory_uri[] . '/js/analytics.script.js', array[], false, true];

}
3 chứ không phải
add_action['wp_enqueue_scripts', 'enqueue_my_scripts', PHP_INT_MAX];

function enqueue_my_scripts[] 
{

    /**
     * Make sure, get_template_directory_uri[] if script stays in parent theme
     * Use get_stylesheet_directory_uri[] if script is in child theme
    */ 
    wp_enqueue_script['jquery-min', get_template_directory_uri[] . '/js/jquery-1.7.2.min.js', array[], false, true];
    wp_enqueue_script['jquery-tools', get_template_directory_uri[] . '/js/jquery.tools.js-1.2.7.min.js', array['jquery-min'], false, true];
    wp_enqueue_script['prefixfree', get_template_directory_uri[] . '/js/prefixfree-1.0.6.min.js', array[], false, true];
    wp_enqueue_script['modernizr', get_template_directory_uri[] . '/js/modernizr.js', array[], false, true];

    /**
     * The two conditional scripts which there is no work around for, load them or drop support
    */ 
    wp_enqueue_script['html5shiv', get_template_directory_uri[] . '/js/html5shiv.js', array[], false, true];
    wp_enqueue_script['selectivizr', get_template_directory_uri[] . '/js/selectivizr-1.0.2.min.js', array[], false, true];

    wp_enqueue_script['acciones', get_template_directory_uri[] . '/js/acciones.js', array[], false, true];
    wp_enqueue_script['openx', '//openx.elclarinweb.com/www/delivery/spcjs.php?id=2&target=_blank', array[], false, true];
    wp_enqueue_script['analytics', get_stylesheet_directory_uri[] . '/js/analytics.script.js', array[], false, true];

}
2

Chỉnh sửa 2

Bản vá từ câu trả lời được liên kết theo ghi chú về các tập lệnh có điều kiện không hoạt động, nó chưa được thực hiện và như tôi đã nói trong các bình luận, nó có lẽ sẽ không được đưa vào phiên bản 10 trong thời gian 100 năm :-]. Thật không may, bạn sẽ cần phải sống với điều này, hiện tại không có cách nào để đạt được điều này. Đây thực sự là sự thu hút để hỗ trợ IE6 - 8. Chà, thành thật mà nói, nếu bạn vẫn ủng hộ IE6 và 7, bạn đang chiến đấu với một trận chiến đã bị mất từ ​​lâu. Tất cả các nhà phát triển phần mềm lớn đã bỏ IE6 [bao gồm WordPress], IE7 đã bị chính Microsoft bỏ đi, vì vậy các nhà phát triển phần mềm sẽ sớm theo dõi và IE 8 sẽ không sống để xem IMHO cuối năm 2016

Để khắc phục vấn đề tương thích này với jQuery, có thể tốt hơn là nên gắn bó với những gì chủ đề cung cấp

Bạn có thể thử một cái gì đó như thế này

add_action['wp_enqueue_scripts', 'enqueue_my_scripts', PHP_INT_MAX];

function enqueue_my_scripts[] 
{

    /**
     * Make sure, get_template_directory_uri[] if script stays in parent theme
     * Use get_stylesheet_directory_uri[] if script is in child theme
    */ 
    wp_enqueue_script['jquery-min', get_template_directory_uri[] . '/js/jquery-1.7.2.min.js', array[], false, true];
    wp_enqueue_script['jquery-tools', get_template_directory_uri[] . '/js/jquery.tools.js-1.2.7.min.js', array['jquery-min'], false, true];
    wp_enqueue_script['prefixfree', get_template_directory_uri[] . '/js/prefixfree-1.0.6.min.js', array[], false, true];
    wp_enqueue_script['modernizr', get_template_directory_uri[] . '/js/modernizr.js', array[], false, true];

    /**
     * The two conditional scripts which there is no work around for, load them or drop support
    */ 
    wp_enqueue_script['html5shiv', get_template_directory_uri[] . '/js/html5shiv.js', array[], false, true];
    wp_enqueue_script['selectivizr', get_template_directory_uri[] . '/js/selectivizr-1.0.2.min.js', array[], false, true];

    wp_enqueue_script['acciones', get_template_directory_uri[] . '/js/acciones.js', array[], false, true];
    wp_enqueue_script['openx', '//openx.elclarinweb.com/www/delivery/spcjs.php?id=2&target=_blank', array[], false, true];
    wp_enqueue_script['analytics', get_stylesheet_directory_uri[] . '/js/analytics.script.js', array[], false, true];

}

Như tôi đã nói trước đây, có một biến PHP được chuyển cho một kịch bản mà bạn nên nói với tác giả chủ đề. Ngoài ra, bất kỳ vấn đề tương thích nên được thảo luận thêm với các tác giả họ. Đây là bố cục chính xác và trong công việc lý thuyết. Đối với bất kỳ vấn đề liên quan đến chủ đề và tương thích nào khác, vui lòng liên hệ với tác giả chủ đề để được hỗ trợ

Chỉnh sửa 3

Đây là cách tiêu đề chủ đề con của bạn.php trông giống như




    
    
    
    
    
    
    

Bài Viết Liên Quan

Chủ Đề