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.

Why does the jquery below not fire? Its going to the "else" every time.

if ( is_admin() && $pagenow == 'theme-install.php' && $_GET['tab']=="upload"){
    add_action('admin_print_scripts', 'theme_upgrade_alert');
    function theme_upgrade_alert(){
        wp_enqueue_script('jquery');
        echo '
        <script type="text/javascript">
        if(typeof jQuery!="undefined"){
            jQuery.noConflict();
            //jQuery(document).ready(function(){
                jQuery(".install-help").hide();
            //});
        }
        else
        {
            alert("If you are attempting to upgrade the current theme, you must do it via theme options.");
        }
        </script>';
        }
}
share|improve this question
    
Please remember, jQuery is loaded in the admin by default ... –  EAMann Feb 9 '12 at 16:32
    
Not sure where you're calling this from, but $pagenow is in the topmost scope; if this is inside a function or include, did you remember to global $pagenow; before this block? –  goldenapples Feb 9 '12 at 17:02

3 Answers 3

up vote 6 down vote accepted

First, check for syntax errors. The syntax highlighting seems to indicate that you have syntax errors.

Second, don't wrap your add_action() calls inside conditionals; rather, wrap your callback function content inside the conditionals, e.g.:

<?php
function theme_upgrade_alernt() {
    if ( is_admin() && $pagenow == 'theme-install.php' && $_GET['tab']=="upload"){
        // CODE GOES HERE
    } else {
        // ELSE CODE GOES HERE
    }
}
add_action('admin_print_scripts', 'theme_upgrade_alert');
?>

I would also recommend enqueueing your scripts at admin_enqueue_scripts, rather than admin_print_scripts.

share|improve this answer

jquery is always loaded in the admin.

you can see it in the source:

<script type='text/javascript' src='http://localhost/multi/wp-admin/load-scripts.php?c=1&amp;load=jquery,utils&amp;ver=3.4-alpha-19704'></script>

also you need to put your conditional logic inside your function. and maybe you can use

admin_print_scripts-(page_hook)

instead of admin_print_scripts so it will only fire on that page.

share|improve this answer

You're enqueuing jQuery, but then just echo'ing out your script. At the time your script runs on the page, jQuery hasn't loaded yet. You enqueued it, but you haven't waited for that enqueue to cause an action on the page output.

Move your script output down to later in the page (like into the footer), or wrap it in something like a jQuery(document).ready() wrapper, so that it runs after the page has finished rendering.

share|improve this answer

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.