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.

Project Orbit
  • 93
  • 1
  • 1
  • 10

2 Answers2

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.

Siegfried Grimbeek
  • 877
  • 1
  • 8
  • 17
  • 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/ )` – Project Orbit Sep 03 '17 at 09:53
  • Haha, yeah it happens! Yes there is, its called URL Parameters, see this: https://stackoverflow.com/questions/4586835/how-to-pass-extra-variables-in-url-with-wordpress :) 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. – Siegfried Grimbeek Sep 03 '17 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) – Project Orbit Sep 03 '17 at 11:53
  • Hi, this may help https://pastebin.com/YQ1Xhd9Q it is from here, https://developer.wordpress.org/reference/functions/is_page/ It seems that with this code you can test for a subpage! Hope it works – Siegfried Grimbeek Sep 03 '17 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 '22 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>
}
Akshay Shah
  • 3,391
  • 2
  • 20
  • 33
  • 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. – FluffyKitten Sep 03 '17 at 07:23