Why is my function to call javascript file not working in wordpress plugin? [closed]

I’m trying to add a function inside my php file, which is inside an /admin/ folder, to call a specific javascript file, roomzeroadmin.js.
I’ve tried using:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>function roomzero_load_admin_scripts() {
// Enqueue admin.js for the admin dashboard
wp_enqueue_script(
'roomzero-admin-script', // Unique handle
plugin_dir_url(__FILE__) . 'js/roomzeroadmin.js', // Path to admin.js
array('jquery'), // Dependencies (if any)
'1.0.0', // Version
true // Load in footer
);
}
add_action('admin_enqueue_scripts', 'roomzero_load_admin_scripts');
</code>
<code>function roomzero_load_admin_scripts() { // Enqueue admin.js for the admin dashboard wp_enqueue_script( 'roomzero-admin-script', // Unique handle plugin_dir_url(__FILE__) . 'js/roomzeroadmin.js', // Path to admin.js array('jquery'), // Dependencies (if any) '1.0.0', // Version true // Load in footer ); } add_action('admin_enqueue_scripts', 'roomzero_load_admin_scripts'); </code>
function roomzero_load_admin_scripts() {
    // Enqueue admin.js for the admin dashboard
    wp_enqueue_script(
        'roomzero-admin-script', // Unique handle
        plugin_dir_url(__FILE__) . 'js/roomzeroadmin.js', // Path to admin.js
        array('jquery'), // Dependencies (if any)
        '1.0.0', // Version
        true // Load in footer
    );
}
add_action('admin_enqueue_scripts', 'roomzero_load_admin_scripts');

but it just doesn’t seem to work. I also tried

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>add_action('wp_enqueue_scripts','roomzero_js');
function roomzero_js() {
wp_enqueue_script( 'roomzero-test-js', plugins_url( '/js/roomzero.js', __FILE__ ));
}
</code>
<code>add_action('wp_enqueue_scripts','roomzero_js'); function roomzero_js() { wp_enqueue_script( 'roomzero-test-js', plugins_url( '/js/roomzero.js', __FILE__ )); } </code>
add_action('wp_enqueue_scripts','roomzero_js');

function roomzero_js() {
    wp_enqueue_script( 'roomzero-test-js', plugins_url( '/js/roomzero.js', __FILE__ ));
}

but that doesn’t work either. Any suggestions on how I can make this work?

3

Trying to help troubleshoot a bit.

How are you running the javascript? Via a page-specific snippet, via your functions or via a plugin?

Also, you probably already did that, just double check that since you’re working within the /admin/ folder, that you make sure that you’re specifically targeting the admin pages and loading your script there.

Also, double check to make sure your filepath actually points to the correct location of roomzeroadmin.js.

If your roomzeroadmin.js file is located at /wp-content/plugins/your-plugin/js/roomzeroadmin.js, plugin_dir_url(FILE) will give the correct base URL, so you should be fine there. However, double-check the structure.

Make sure the array(‘jquery’) dependency is correct. Do you specifically need jquery?

Here if a clean piece of code that you can test with:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>function roomzero_load_admin_scripts($hook) {
// Check if we're on the admin pages (optional: you can specify exact pages by checking the $hook)
if (is_admin()) {
// Enqueue the roomzeroadmin.js file
wp_enqueue_script(
'roomzero-admin-script', // Unique handle for the script
plugin_dir_url(__FILE__) . 'js/roomzeroadmin.js', // Path to the JS file
array('jquery'), // Dependencies (jQuery is common in the admin area)
'1.0.0', // Version of your script
true // Load in the footer (set to false if you want it in the header)
);
}
}
add_action('admin_enqueue_scripts', 'roomzero_load_admin_scripts');
</code>
<code>function roomzero_load_admin_scripts($hook) { // Check if we're on the admin pages (optional: you can specify exact pages by checking the $hook) if (is_admin()) { // Enqueue the roomzeroadmin.js file wp_enqueue_script( 'roomzero-admin-script', // Unique handle for the script plugin_dir_url(__FILE__) . 'js/roomzeroadmin.js', // Path to the JS file array('jquery'), // Dependencies (jQuery is common in the admin area) '1.0.0', // Version of your script true // Load in the footer (set to false if you want it in the header) ); } } add_action('admin_enqueue_scripts', 'roomzero_load_admin_scripts'); </code>
function roomzero_load_admin_scripts($hook) {
    // Check if we're on the admin pages (optional: you can specify exact pages by checking the $hook)
    if (is_admin()) {
        // Enqueue the roomzeroadmin.js file
        wp_enqueue_script(
            'roomzero-admin-script', // Unique handle for the script
            plugin_dir_url(__FILE__) . 'js/roomzeroadmin.js', // Path to the JS file
            array('jquery'), // Dependencies (jQuery is common in the admin area)
            '1.0.0', // Version of your script
            true // Load in the footer (set to false if you want it in the header)
        );
    }
}

add_action('admin_enqueue_scripts', 'roomzero_load_admin_scripts');

Good luck.

New contributor

Flower88 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật