How To Use JS Path For Image In WordPress

Share Now

Add this code in functions.php file

//js variable
function theme_name_set_js_var() {
  $translation_array = array( 'template_directory_uri' => get_template_directory_uri());
  wp_localize_script( 'jquery', 'my_data', $translation_array );
}
add_action('wp_enqueue_scripts','theme_name_set_js_var');

Then Add Code in your Javascript file according to your need. In this case I am adding code in owlcarousel

navText: ['<img src="' + my_data.template_directory_uri + '/assets/img/slider-left.png" alt="prev">', '<img src="' + my_data.template_directory_uri + '/assets/img/slider-right.png" alt="next">']
Picture of Nahid Mahamud

Nahid Mahamud

Web Developer | Graphic Designer | WordPress & Woo-commerce Expert