最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to add JavaScript to WordPress PHP file? - Stack Overflow

programmeradmin0浏览0评论

I would like to reference JavaScript files from my root folder in a WordPress PHP file, however when I load the page, the JavaScript is being ignored. Is there a WordPress restriction on referencing .js files? Am I not correctly referencing JavaScript for PHP?

This is what I wrote (which does not work):

<?php wp_footer(); ?>
<script src="/files/js/jquery.js" type="text/javascript"></script>
<script src="/files/js/jquery.cycle.js" type="text/javascript"></script>
</body>
</html>

I would like to reference JavaScript files from my root folder in a WordPress PHP file, however when I load the page, the JavaScript is being ignored. Is there a WordPress restriction on referencing .js files? Am I not correctly referencing JavaScript for PHP?

This is what I wrote (which does not work):

<?php wp_footer(); ?>
<script src="/files/js/jquery.js" type="text/javascript"></script>
<script src="/files/js/jquery.cycle.js" type="text/javascript"></script>
</body>
</html>
Share Improve this question edited Mar 15, 2013 at 8:57 RavatSinh Sisodiya 1,6081 gold badge21 silver badges42 bronze badges asked Mar 15, 2013 at 8:52 John MontagueJohn Montague 951 gold badge5 silver badges15 bronze badges 4
  • That should be the way to reference a JS-file. If you look at the generated source, does this actually get outputed to source? If you look in the browser devtools, and look at the network traffic as you reload the page, are the JS-files among the files in the list of files that has been loaded? – Christofer Eliasson Commented Mar 15, 2013 at 8:56
  • have you tried using absolute paths? maybe your site can't find your files. – rgin Commented Mar 15, 2013 at 8:59
  • 1 You should do it the WordPress way with wp_enqueue_script – elclanrs Commented Mar 15, 2013 at 8:59
  • Tutorial to wp_enqueue: halfelf/2012/jquery-why-u-no-enqueued – janw Commented Mar 15, 2013 at 9:41
Add a ment  | 

4 Answers 4

Reset to default 3

For jQuery I use this technique Loading The Latest Version of jQuery in WordPress

Basically you use wp_enqueue_script function in functions.php.

For scripts where I want to simply add them in the template files and don't want to bother with wp_enqueue_script, I put them in scripts folder that was created in the theme folder. I add All custom added .js scripts there.

Then in the template file I use the code like this:

<script type="text/javascript" src="<?=get_template_directory_uri();?>/scripts/markers.js"></script>

Wordpress has its own method to add js or css files.

<?php
function my_scripts_loader() {
    wp_enqueue_script( 'my-js', 'filename.js', false );
}
add_action( 'wp_enqueue_scripts', 'my_scripts_loader' );
?>

Put your files folder to your wordpress theme which you are curently useing after that just need to

include the path on header.php file like

<script src="<?php bloginfo('template_directory'); ?>/files/js/jquery-min.js" type="text/javascript"></script>

hope this will help you ....

WordPress got a various number of ways to include file, I myself have worked with WordPress in the early days, back then I had to call the path with a WordPress function.

Example: <script src="<?php echo bloginfo('template_directory'); ?>/files/js/jquery.js" type="text/javascript"></script> //add echo

You might want to var_dump the bloginfo('template_directory') and work your way from there.

发布评论

评论列表(0)

  1. 暂无评论