wp_head() not including styles and javascripts after a template redirect

I am using the template_redirect action to redirect a user to a custom template which has no header, sidebar or footer (the intent is for it to be in an iframe as a dialog box). After I done the redirect though, I could not seem to get all the enqueued JS and CSS to list.

Here’s the code:

Read More
    <?php
    /**
     * This theme file is accessed by template redirect
     * No header, sidebar or footer
     */

    ?>
    <!DOCTYPE html>
    <head>
    <title>Customize Product</title>

    <?php wp_head()?>
    </head>
    <body>
    <div id="container">
                <div id="content" role="main">

                <?php
                /**
                 * Output the content of the template 
                 */
                do_action('product_customize_display_customization_content');
                ?>

                </div><!-- #content -->
            </div><!-- #container -->

    </body>

Edit: Here’s how I am including the JS

// bootstrap code, will refactor later as necessary
if (is_admin())
{
    global $admin_class;
    $admin_class = new product_customize_admin();
    $admin_class->init_admin_actions();
    $admin_class->init_ajax_actions();

    add_action('admin_menu', 'init_admin_menu');
    add_action('admin_print_scripts', 'my_admin_scripts');
    add_action('admin_print_styles', 'my_admin_styles');


}
else
{
    add_action('wp_head', 'include_frontend_scripts');


    // setup frontend
    global $frontend_class;
    $frontend_class = new product_customize();


}

function include_frontend_scripts()
{   


    // preset scripts
    wp_register_script('product-customize-preset', plugin_dir_url(__FILE__).'js/preset.js');
    wp_enqueue_script('product-customize-preset');
}

Here’s the code for the template redirect

function theme_redirect()
{
    global $wp;
    $plugin_dir = dirname(__FILE__);

    //echo '<pre>'.print_r($_GET, true).'</pre>';

    // if we are doing product customization
    if ($_GET['prod_cust_do_customize'] == 1)
    {
        $return_template = $plugin_dir.'/themefiles/customize_preset_page.php';
        echo "template = $return_template";
        $this->do_theme_redirect($return_template);

    }

}

function do_theme_redirect($url)
{
    include ($url);
    exit();
}

Related posts

Leave a Reply

2 comments

  1. If CSS is not outputting inside wp_head, it may imply that they’re not being enqueued correctly. See here for more info. Maybe they’re hardcoded in the theme file? In header.php?

    As for scripts, my guess is that they are being enqueued and set to trigger during footer actions, so the fact that you’re not calling wp_footer() means that those actions never fire.

    If you cannot edit the footer.php template file to remove all the unnecessary output, while still leaving wp_footer() the code above, then at least trigger the usual actions manually, like so:

    // ...
            </div><!-- #content -->
        </div><!-- #container -->
    
    <?php do_action('wp_print_footer_scripts'); ?>
    
    </body>
    

    It’s hard to tell if this will work, because it can depend on how the scripts are enqueued, and it varies wildly from plugin to plugin (especially if the original coder doesn’t follow WP coding standards), but hopefully it’ll lead you in the right direction.

    Edit:

    If you need to add dependencies to your scripts, like jQuery, to make sure those deps are enqueued and loaded before your own, do this:

    wp_register_script('product-customize-preset', plugin_dir_url(__FILE__).'js/preset.js', array('jquery'));