9

I want to run a specific js on a specific page. I.e: wwww.custom.com/english/

I've tried the following two codes (header.php and functions.php) but none of them work.

Code 1:

<script>
if(location.pathname=="/english/") 
 script.src = /js/custom.js;
</script>

Code 2:

function my_scripts() {
    if( is_page( array( 'about-us', 'contact', 'management' ) ){
        wp_enqueue_script( 'script-name', 'path/to/example.js', array(), '1.0.0', true );
    }
}
add_action( 'wp_enqueue_scripts', 'my_scripts' );

The header one does not show at all, no error either. The functions.php is a syntax error.

Any suggestions? Much Obliged

PS: I'm using WordPress.

0

2 Answers 2

21

You code is great! You are just missing a closing parenthesis:

function my_scripts() {
    if( is_page( array( 'about-us', 'contact', 'management' ) ) ){
        wp_enqueue_script( 'script-name', 'path/to/example.js', array(), '1.0.0', true );
    }
}
add_action( 'wp_enqueue_scripts', 'my_scripts' );

For future reference, in your wp-config.php file you can set debug to true, I believe that this will pick it up.

5
  • Oh wow didn't notice the closing parenthesis haha! Btw, is there a quick way to say. if the url contains something, then load this script? I.e: if( is_page( array( about-us/test/ ) Sep 3, 2017 at 9:53
  • Haha, yeah it happens! Yes there is, its called URL Parameters, see this: stackoverflow.com/questions/4586835/… :) You can then run another if statement inside the if page to check whether the url parameter exists and then log the code. I hope this helps. Sep 3, 2017 at 10:16
  • I'm having some issues with the override part, because my main goal is to run a script for frontpage and run another script for frontpage/test/ - the "/test/" is an extension from the weglot plugin (translation). My problem is that it keeps loading the first script, since it has "frontpage" in the url and it's not a seperate page. So it won't load the other one because it "thinks" its the same page (which also it kinda is, but translated version) Sep 3, 2017 at 11:53
  • Hi, this may help pastebin.com/YQ1Xhd9Q it is from here, developer.wordpress.org/reference/functions/is_page It seems that with this code you can test for a subpage! Hope it works Sep 3, 2017 at 12:07
  • I wan to ask, please help, my function is to hide an element in certain page, is it the best way to do it that not affect the performance speed or my website? or is it better to use CSS?
    – Zui Zui
    Jun 23, 2022 at 21:23
1

There is multiple other ways.

Just open your header.php and try to get current page slug or id and put simple if condition it will surely include your js

Code 1

global $post;
$post_slug=$post->post_name;
if($post_slug == 'about'){
<script type="text/javascript" src="<?php echo get_template_directory_uri(); ?>/js/example.js"></script>
}

OR Code 2

$currentID= get_the_ID();
//instead of 10 put the your id
if($currentID == 10){
 <script type="text/javascript" src="<?php echo get_template_directory_uri(); ?>/js/example.js"></script>
}
1
  • 1
    While this answer will work, it's generally recommended that js files are included in Wordpress using the wp_enqueue_script so that they can be enqueued correctly. Sep 3, 2017 at 7:23

Your Answer

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

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