Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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');
share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

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' );
share|improve this answer
    
It did work but my script is loaded on every page and that is not that what I want –  stamp Oct 8 '12 at 11:16
2  
admin_enqueue_scripts passes a variable with the current hook name. Use this to only select the admin page you want. echo or var_dump to see what string is passed to your specific page. (pb_admin_style($hook)). –  hampusn Oct 8 '12 at 11:25
1  
Sometimes you just have to read the manual –  Miha Rekar Oct 8 '12 at 11:37
    
@hampusn Did as you siad. Checked with vardump what value is hidden in $hook and added if( 'toplevel_page_add-wallpaper' != $hook) return; –  stamp Oct 8 '12 at 12:51
    
@Miha Could you please edit your answer to include a working example with the hook condition aswell? It will be more useful to others who find this later. –  hampusn Oct 8 '12 at 15:54
show 1 more comment

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');
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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