$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>admin - admin_enqueue_scripts not working|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

admin - admin_enqueue_scripts not working

matteradmin10PV0评论

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.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far