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.


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 :-) – Pieter Goosen Sep 23 '14 at 13:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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