I am trying to add a conditional to enqueue script only for 404 page. Theoretically I would have to use is_404() right? Well it does not work:

if ( is_page_template('404.php') ) {
    add_action('wp_enqueue_scripts', 'scripts_404');

if ( is_404() ) {
    add_action('wp_enqueue_scripts', 'scripts_404');

nor does the is_page_template work. However if I add this line without conditionals, it works just fine. Any ideas or am I missing something?

Thanks in advance.


1 Answer 1


You should wrap your enqueue/register function in your conditional, not your add_action()

You can try something like this using the is_404() conditional check

function enqueue_404_script() {
    if(is_404()) {
        //do what you need to do
add_action( 'wp_enqueue_scripts', 'enqueue_404_script' );
  • 1
    My pleasure, glad it worked. Enjoy :-) Commented Sep 23, 2014 at 13:02

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.