3

The new "Customize" theme screen is a welcome addition in WordPress 3.4, however, I find it conflicts with my method of loading scripts loaded in the footer:

In functions.php

if(!is_admin())
{
/* GET PUBLIC FUNCTIONS
*************************************************************/
require_once(TEMPLATEPATH . '/functions_public.php');
}

In functions_public.php

/* Move scripts to the footer, speeding up page loading time as per http://yhoo.it/IAL94Z
*************************************************************************/
if ( isset($_GET['page']) && $_GET['page'] == 'customize.php') echo "in customize.php"; else echo "NOT in customize.php";

if(!get_option('my_scripts_head')){
    remove_action('wp_head', 'wp_print_scripts');
    remove_action('wp_head', 'wp_print_head_scripts', 9);
    remove_action('wp_head', 'wp_enqueue_scripts', 1);
    add_action('wp_footer', 'wp_print_scripts', 5);
    add_action('wp_footer', 'wp_enqueue_scripts', 5);
    add_action('wp_footer', 'wp_print_head_scripts', 5);
}

This code branch moves those scripts from wp_head to wp_footer. When this option (my_scripts_head) is enabled, the "Customize" screen shows my theme preview without the scripts that are loaded in the footer.

I find that when I disable this option, the "Customize" screen loads fine, including my scripts.

When themes are previewed in customize.php, does wp_footer not fire?

  • Can we see the complete callback, as well as the add_action() call you're using to hook that callback? – Chip Bennett Jun 14 '12 at 14:52
  • I've pasted everything involved in the method. It checks the option setting and runs the code branch accordingly. There's no custom callback. It just moves scripts from wp_head to wp_footer. Perhaps wp_footer is not needed and thus not called on customize.php? – N2Mystic Jun 14 '12 at 14:56
  • "I've pasted everything involved in the method." - Where does this code reside? Is it just sitting, naked, in functions.php? If so, that's your first problem. – Chip Bennett Jun 14 '12 at 14:59
  • No, its in a php script that's called only on when the theme is on the public view. – N2Mystic Jun 14 '12 at 15:03
  • "its in a php script that's called only on when the theme is on the public view" - How does that script get called? Where is the hook? What is the conditional? We need to see that code, too. – Chip Bennett Jun 14 '12 at 15:06
5

Okay, first, let's set things up properly, with a callback hooked into an appropriate action hook:

<?php
function wpse55227_enqueue_scripts() {
    // Enqueue code goes here
}
add_action( 'wp_head', 'wpse55227_enqueue_scripts' );
?>

We'll put all of our code in to this callback.

The next step is to add our if ( ! is_admin() ) conditional wrapper:

<?php
function wpse55227_enqueue_scripts() {
    if ( ! is_admin() ) {
        // Enqueue code goes here
    }
}
add_action( 'wp_head', 'wpse55227_enqueue_scripts' );
?>

Now, let's add in your original code:

<?php
function wpse55227_enqueue_scripts() {
    if ( ! is_admin() ) {
        if(!get_option('my_scripts_head')){
            remove_action('wp_head', 'wp_print_scripts');
            remove_action('wp_head', 'wp_print_head_scripts', 9);
            remove_action('wp_head', 'wp_enqueue_scripts', 1);
            add_action('wp_footer', 'wp_print_scripts', 5);
            add_action('wp_footer', 'wp_enqueue_scripts', 5);
            add_action('wp_footer', 'wp_print_head_scripts', 5);
        }
    }
}
add_action( 'wp_head', 'wpse55227_enqueue_scripts' );
?>

So, at this point, we should be back to where you were before. Now, let's account for the Customizer. The easiest way is to check for the $wp_customize global being set:

<?php
function wpse55227_enqueue_scripts() {
    // Globalize
    global $wp_customize;
    // If $wp_customize is set, return
    if ( isset( $wp_customize ) ) {
        return;
    }

    if ( ! is_admin() ) {
        if(!get_option('my_scripts_head')){
            remove_action('wp_head', 'wp_print_scripts');
            remove_action('wp_head', 'wp_print_head_scripts', 9);
            remove_action('wp_head', 'wp_enqueue_scripts', 1);
            add_action('wp_footer', 'wp_print_scripts', 5);
            add_action('wp_footer', 'wp_enqueue_scripts', 5);
            add_action('wp_footer', 'wp_print_head_scripts', 5);
        }
    }
}
add_action( 'wp_head', 'wpse55227_enqueue_scripts' );
?>

By the way: out of curiosity, why are you moving all of the script enqueueing to the footer? I would imagine that has a very high likelihood of breaking things, and/or causing unintended consequences.

  • 1
    WordPress 4.0 introduced the conditional is_customize_preview(), makes things a little easier. – Tim Mar 13 '15 at 19:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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