admin - admin_enqueue_scripts not working

admin2025-06-02  6

enqueue.php

function sunset_load_admin_scripts( $hook )
{
    if($hook != 'toplevel_page_mypluginname') {
        return;
    }
    wp_enqueue_media();
    wp_register_script('sunset-admin-script',get_template_directory_uri() .'/js/news_admin.js', array('jquery'), '1.0.0', true);
    wp_enqueue_script('sunset-admin-script');
}
add_action( 'admin_enqueue_scripts', 'sunset_load_admin_scripts' );

news_admin.js

    jQuery(document).ready(function() {
        console.log("ddddddddd");

        var mediaUploader;

        jQuery('#upload_button').on('click', function(e){
            e.preventDefault();
            if(mediaUploader){
                mediaUploader.open();
                return;
            }
            mediaUploader = wp.media.frames.file_frame = wp.media({
                title: 'Choose a Image',
                button: {
                    text: 'Choose a Image'
                },
                multiple: false
            });
        });
    });

enqueue.php

function sunset_load_admin_scripts( $hook )
{
    if($hook != 'toplevel_page_mypluginname') {
        return;
    }
    wp_enqueue_media();
    wp_register_script('sunset-admin-script',get_template_directory_uri() .'/js/news_admin.js', array('jquery'), '1.0.0', true);
    wp_enqueue_script('sunset-admin-script');
}
add_action( 'admin_enqueue_scripts', 'sunset_load_admin_scripts' );

news_admin.js

    jQuery(document).ready(function() {
        console.log("ddddddddd");

        var mediaUploader;

        jQuery('#upload_button').on('click', function(e){
            e.preventDefault();
            if(mediaUploader){
                mediaUploader.open();
                return;
            }
            mediaUploader = wp.media.frames.file_frame = wp.media({
                title: 'Choose a Image',
                button: {
                    text: 'Choose a Image'
                },
                multiple: false
            });
        });
    });
Share Improve this question edited Feb 24, 2019 at 11:36 Mayeenul Islam 12.9k21 gold badges85 silver badges169 bronze badges asked Feb 24, 2019 at 11:08 abudo lokyabudo loky 1131 gold badge1 silver badge4 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 3

If you remove that conditional in the first line of the enqueue function, your scripts should load. I can't see the broader context of why your function is written that way, but if you just copied it from a tutorial or something, you don't need it here.

Try:

<?php 
function sunset_load_admin_scripts(){ 
    wp_enqueue_media();
    wp_register_script('sunset-admin-script',get_template_directory_uri() .'/js/news_admin.js', array('jquery'), '1.0.0', true);
    wp_enqueue_script('sunset-admin-script'); 
}
add_action( 'admin_enqueue_scripts', 'sunset_load_admin_scripts' );

Please note admin_enqueue_scripts will only enqueue scripts for the back-end of your site, not the front-end. You can replace it with wp_enqueue_scripts if you want it to work on the frontend.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748871882a314418.html

最新回复(0)