3

Trying to get this function to work. But keep getting a jQuery not defined error.

I have tried many variations here to get the jQuery to work: https://blog.templatetoaster.com/fix-jquery-is-not-defined-error/

add_action( 'load-post.php', function () { 

$status = "status-started";

if($status == 'status-started'):
    ?>
    <script>
        jQuery(document).ready(function($) { 
            /* write your JavaScript code here */
            $('.btn-start').trigger('click');
            console.log('Started successfully!');
        });
    </script>

<?php 
endif;

} );

2 Answers 2

3

You have your jQuery hooked too early. The jQuery library has not yet loaded when the load-(page) action is fired.

Use admin_print_scripts to print inline scripts. That action comes after scripts are enqueued.

Here's your original code using admin_print_scripts:

add_action( 'admin_print_scripts', function () { 

$status = "status-started";

if($status == 'status-started'):
    ?>
    <script>
        jQuery(document).ready(function($) { 
            /* write your JavaScript code here */
            $('.btn-start').trigger('click');
            console.log('Started successfully!');
        });
    </script>

<?php 
endif;

} );

An alternative to using admin_print_scripts would be to use admin_print_footer_scripts, which is essentially the same thing, but in the footer instead of the header. Either is generally OK and whether you do it in the header or the footer is sometimes a matter of preference and sometimes a matter of necessity. So it will depend on the actual use case.

For anyone wondering why I didn't suggest using another hook such as wp_head or wp_footer, there are two reasons. First, it's not what those hooks were intended for. Using them would get your scripts out of sequence with where they really should be (although in a lot of cases they would work just fine). Second, they are not admin specific hooks. The question of a load-(page) action indicates this is an admin side process, so use the proper admin side hooks.

I would say similar things about the use of various "enqueue" hooks. First, don't use an enqueue that is not admin side specific (speaking specifically of this original post and not in generalities). Second, enqueueing is for libraries and static scripts. If you're printing out scripts (especially ones that may be dynamic), use the _print_scripts hooks instead.

4
  • But will admin_print_scripts fire on post edit load?
    – samjco-com
    Commented Mar 11, 2019 at 0:42
  • 1
    It fires on all admin pages (see: developer.wordpress.org/reference/hooks/admin_print_scripts). Note that it's not the only option, either. You could use admin_print_footer_scripts which does the same thing but in the footer.
    – butlerblog
    Commented Mar 11, 2019 at 1:30
  • Can you update your answer to include my code , just for other users?
    – samjco-com
    Commented Mar 12, 2019 at 17:08
  • Yes - I added your code with the admin_print_scripts hook, and also some additional explanations as to why I recommended the hooks I mentioned instead of others.
    – butlerblog
    Commented Mar 12, 2019 at 20:47
2

You must call jQuery at the end of the file, when your document is ready.

Try to add the action to the wp_footer or use wp_enqueue_script .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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