How to Add a Custom Script to Customize.php

I am having a rather difficult time adding a script to the 3.4 Theme Customizer (i.e., customize.php). If I want, I can deregister jquery and add it from googleapi as follows:

wp_deregister_script( 'jquery' );
wp_register_script( 'jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js');
wp_enqueue_script( 'jquery' );

That works just fine. However, if I want to add jquery-color or some other script I just can’t get it to work. For instance, this doesn’t work:

Read More
wp_enqueue_script( 'jquery-color' );

This also does not work:

wp_register_script( 'bpjquerycolor', 'http://localhost/bp2/wp-content/themes/buildpress/admin/scripts/bpjquerycolor.js');

wp_enqueue_script( 'bpjquerycolor' );

In terms of add_action, none of the following work:

add_action( 'admin_print_scripts', 'add_admin_scripts' );
add_action( 'wp_enqueue_scripts', 'add_admin_scripts' );
add_action( 'init', 'add_admin_scripts' );
add_action( 'admin_init', 'add_admin_scripts' );

I have also tried the following with no success:

global $wp_customize;
if ( isset( $wp_customize ) ) {
wp_enqueue_script( 'jquery-color' );
}

In short, I’ve tried every combination that I can think of with no success.

What am I doing wrong? How can I add this to the theme customizer?

Thanks in advance for any help you can offer.

Related posts

Leave a Reply

2 comments

  1. It appears that ‘customize_controls_enqueue_scripts’ also works and may be the intended function.

    function theme_customize_style() {
        wp_enqueue_style('customize-styles', get_template_directory_uri() . '/customize.css');
    }
    add_action( 'customize_controls_enqueue_scripts', 'theme_customize_style' );
    
  2. Okay, I figured it out. Instead of “init” or “wp_enqueue_scripts”, etc. I had to use “customize_controls_print_footer_scripts” as such:

    add_action( 'customize_controls_print_footer_scripts', 'bp_admin_scripts' );
    

    If you want the script in the header then you use: “customize_controls_print_scripts”

    And if you want to enqueue a style you use “customize_controls_print_styles”

    I found this out by looking at the code for customize.php and noting the add_action codes used there on lines 30 – 32 (as of version 3.4.1)