I have this in my theme’s function file:
$(document).ready(function() {
$(".ajax-loader").attr("src","<?php bloginfo('template_url'); ?>/images/ajax-loader.gif");
});
However, when it prints, the src prints as <?php bloginfo('template_url'); ?>/images/ajax-loader.gif, that is showing the php code instead of showing my template url. What would be the correct way to write this code?
Is this in a
.jsfile? If so, you can’t place WordPress template tags inside because.jsfiles are not processed by PHP.You can either include your code inline within
header.phpusing<script>tags:Or change the extension of your JavaScript file from
.jsto.php, and add this line at the very top:That tells the server to treat this as a JavaScript file, although it’ll be processed by PHP.
In any case, you’re using
.attr()correctly.