Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

163
Views
How to load external JavaScript in WordPress only for specific folder/subdirectory?

I am trying to load an external JavaScript to execute only in a language version of a website. Example: websitename.com/es/ <-- for Spanish I don't want a custom external (src..) third-party script be loaded onto the whole website. I was trying to use conditional tags in WordPress via functions but there is no option to target all the subdirectory. Using PHP or jQuery. Any solution you can suggest will be appreciated.

EDIT: it's an external script loaded using src, I need it to be applied only to the Spanish version which is in folder '/es/'. I am using polylang.

Thank you in advance

Here is what I was trying to do. I don't know if using taxonomies can help.

function tc_hook_wajs() {   
   if (is_home ('')) { 
        ?>      
      <script type="text/javascript">  
         //js code goes here     
       </script>        <?php      }    } 

   add_action('wp_head', 'tc_hook_wajs');
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can use the wpml_current_language hook in functions.php to get the current language code and based on languages code you can load each language file separately. here is example :

function my_script() {
 $current_language_code = apply_filters( 'wpml_current_language', null );
 if($current_language_code == 'en') { // English script
    $load_file = 'myscript.js';
 }
 elseif($current_language_code == 'sp') { // Spainsh script
    $load_file = 'myscript-sp.js';
 }
 else { // Italian script
    $load_file = 'myscript-it.js';
 }
 
 wp_enqueue_script( 'custom-js', get_stylesheet_directory_uri() . '/js/'.$load_file, array( 'jquery' ),'',true );
 }
 add_action( 'wp_enqueue_scripts', 'my_script' );
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!