1

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
            });
        });
    });

1 Answer 1

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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.