Adding scripts to admin page in my theme

In functions.php of my theme I have code to add page in admin area and append scripts to it. But scripts are not loaded. Bellow is code. Commented out add_action lines are the one I did check.

    // functions.php
    // Append user style and scripts to Add New Wallpaper menu
    function pb_admin_scripts() {
      wp_enqueue_script('media-upload');
      wp_enqueue_script('thickbox');
      wp_register_script('pb-upload', get_template_directory_uri() . '/pb-upload.js');
      wp_enqueue_script('pb-upload');
    }

    function pb_admin_style() {
      wp_enqueue_style('thickbox');
    }


    function pb_wallpaper_adminmenu() {
      $page = add_menu_page('Dodaj Tapetę', 'Dodaj Tapetę', 'edit_posts', 'add-wallpaper', 'pb_add_wallpaper', '', 6);
    }
    //  add_action( 'admin_head-add-wallpaper', 'pb_admin_scripts');
    //  add_action( 'admin_print_scripts-add-wallpaper', 'pb_admin_scripts');
    //  add_action( 'admin_print_styles-add-wallpaper', 'pb_admin_style');
    //  add_action( 'load-add-wallpaper', 'pb_admin_scripts');
    //  add_action( 'load-add-wallpaper', 'pb_admin_style');


    // add new wallpaper
    function pb_add_wallpaper() {
      ?>
    <div class="wallpaper-add">
      <h3>Dodaj Nową Tapetę</h3>
      <form method="post" action="">
        <input type="hidden" name="action" value="pb_add_wallpaper" />
        <input id="upload_image_button" type="button" value="Upload Image" />
        <input type="hidden" name="upload_image" id="upload_image" />
        <?php wp_nonce_field('pb_add_wallpaper', '_nonce'); ?>
      </form>
    </div>

    <?php
    } 

    add_action('admin_menu', 'pb_wallpaper_adminmenu');

Related posts

Leave a Reply

2 comments

  1. Use add_action('admin_enqueue_scripts', 'pb_admin_style'); and as the manual states, the admin_enqueue_scripts can also be used to target a specific admin page. Use this to only select the admin page you want.

    function pb_admin_style($hook) {
        if( 'edit.php' != $hook )
            return;
        wp_enqueue_script( 'my_custom_script', plugins_url('/myscript.js', __FILE__) );
    }
    add_action( 'admin_enqueue_scripts', 'pb_admin_style' );
    
  2. This is my load script method , as this i load scripts in specific post type this example is for post_type=’resources’

    if(!function_exists('load_pdf_uploader_scripts'))
    {
        function load_pdf_uploader_scripts($hook)
        {
            //Check if is in edit post
            if(isset($_GET['post']) && isset($_GET['action']))
            {
                //Get post type
                $post_type = get_post_type( $_GET['post'] );
    
                //Check if we are in resources post type to load specific scripts
                if($post_type == 'resources' && $_GET['action']=='edit')
                {
                    //Load the script
                    wp_enqueue_script('pdfupload', get_stylesheet_directory_uri().'/assets/js/pdfupload.js', array('jquery')); // in footer
                }
            }
            //Check if is new post item
            elseif(isset($_GET['post_type']))
            {
                //Check if we are in resources post type to load specific scripts
                if($_GET['post_type'] == 'resources')
                {
                    //Load the script
                    wp_enqueue_script('pdfupload', get_stylesheet_directory_uri().'/assets/js/pdfupload.js', array('jquery')); // in footer
                }
            }
        }
        //We load this script only in admin area
        add_action('admin_enqueue_scripts', 'load_pdf_uploader_scripts');
    }