0

For some reason the normal src path of my template made with wordpress doesn't work. It is embedded in a javascript connect to my footer.php. Now my question is: is it possible to call an image with src inside a javascript with a php snippet? Here is my code so far:

var d = new Date();

document.getElementById("copyright").innerHTML = "&copy;" + " " + (d.getFullYear()) + " " + "All Rights Reserved" + " " + " " +"<img src='wp-content/themes/wpboot/images/maple.svg' alt='Canadian maple leaf' width='14'>" + " " + "We Are A Canadian Company";

I'm trying to add a php snippet like that:

<img src="<?php bloginfo('template_directory'); ?>/images/maple.svg" width="60" alt="Maple">

To achieve something like that:

document.getElementById("copyright").innerHTML = "&copy;" + " " + (d.getFullYear()) + " " + "All Rights Reserved" + " " + " " +"<img src='<?php bloginfo('template_directory'); ?>/images/maple.svg' alt='Canadian maple leaf' width='14'>" + " " + "We Are A Canadian Company";

I tried to build a var called:

var template = "<?php bloginfo('template_directory'); ?>/";

And I added to the src but I didn't work.. any clue?

3
  • You have to used in witch file..? Like External js or .. Default WP file ? Commented May 4, 2016 at 6:18
  • external js inside wordpress folder Commented May 4, 2016 at 13:29
  • give me js handler like 'wp_enqueue_script( 'ajax-script', plugins_url( '/js/myjquery.js', FILE ), array( 'jquery' ) );' "ajax-script" is handler Commented May 4, 2016 at 13:52

1 Answer 1

1

In .js file we can't used php so in WordPress get values as like this "myscript" to used your script "handler".

in your functions.php file

wp_enqueue_script('myscript',get_template_directory_uri().'/js/myscript.js',array('jquery'));
wp_localize_script( 'myscript', 'mycustomurl',  get_template_directory_uri() );

in js file use as like this alert just your info.

alert(mycustomurl);
document.getElementById("copyright").innerHTML = "&copy;" + " " + (d.getFullYear()) + " " + "All Rights Reserved" + " " + " " +"<img src='"+mycustomurl+"/images/maple.svg' alt='Canadian maple leaf' width='14'>" + " " + "We Are A Canadian Company";
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.