0

Is is possible to use a javascript var in some php code? I want to grab the url of an attachment image in wordpress. This is currently what I have tried using.

<div id="image"></div>

<script>
var attachmentid = '8723';
var attachmenturl = '<?php echo wp_get_attachment_url('+attachmentid+'); ?>';

$('#image').append(attachmenturl);
</script>
Oscar
  • 1
  • 3
  • but why you want in this way? – dipmala Jun 13 '18 at 09:42
  • because im using the rest api to grab data using javascript and using foreach in javascript and im not sure if you can get image via javascript – Oscar Jun 13 '18 at 09:43
  • @Oscar you would need to create an new `img` element and set the src to append it: `var img = $('').attr('src', attachmenturl ); $('#image').append(img );` this should work if it is not supposed to be ansynchronus – empiric Jun 13 '18 at 09:50
  • @empiric i tired but dont quite understand, could you send me a full example of what i need to do? – Oscar Jun 13 '18 at 09:57
  • @Oscar you are current just appending the image URL, but you have to create a html element first: [example](http://jsfiddle.net/xpvt214o/221022/) – empiric Jun 13 '18 at 10:01

0 Answers0