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.

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:

    <?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();
}
share|improve this question
1  
scripts should be enqueued on the wp_enqueue_scripts action, not wp_head –  Milo Sep 25 '12 at 4:53

2 Answers 2

Make sure you have wp_footer() added to your footer.php file

share|improve this answer

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'));
share|improve this answer
    
The missing script files are added via a plugin which add it using a add_action('wp_head', 'include_js'). Normally the js shows up, but not after a template redirect. –  Extrakun Sep 24 '12 at 4:21
    
Could you paste the include_js function? Also, can you post the template_redirect action code? If everything is supposed to be in the header, then the problem may be somewhere else and not on the code you posted above. –  tbuteler Sep 24 '12 at 11:07
    
It works if I use do_action("wp_enqueue_scripts", 'include_frontend_scripts') but my scripts are included before jQuery, so it wouldn't work for me. –  Extrakun Sep 25 '12 at 4:38
    
That can be easily fixed. See edited answer. On a side note, are you using the 'template_redirect' action at all? –  tbuteler Sep 25 '12 at 11:50
    
Yes, I am using template_redirect action. add_action('template_redirect', 'theme_redirect') –  Extrakun Sep 25 '12 at 14:24

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.