如何在functions.php中正确添加Javascript
-
-
太疯狂了:您不能在生成箭头的代码中编辑箭头吗?(或者是从外部来源下载的?)在任何情况下,您都可以在一个函数中同时执行两项替换操作,以避免两次在cart块中读取和写入所有HTML.我不知道一种直接从functions.php将其注入页面的方法,但是您可以将其保存在一个单独的脚本文件中(如果还没有,可以将其添加到其中),然后[`wp-enqueue-script`](http://codex.wordpress.org/Function_Reference/wp_enqueue_script).您还必须将$更改为jQuery(请参见该页面的第7节)That's crazy: can't you edit the arrows out in the code that generates them? (Or is it downloaded from an external source?) In any case you can do both replaces in a single function to avoid reading and writing all the HTML inside the cart block twice. I don't know a way to directly inject that into the page from functions.php but you can save it in a separate script file (if you don't already have one you can add it to) and then [`wp-enqueue-script`](http://codex.wordpress.org/Function_Reference/wp_enqueue_script) it. You'll also have to change the `$`s to `jQuery` (see that page section 7)
- 0
- 2014-06-25
- Rup
-
不,我很确定在插入之前无法将其删除.如果可以,我还没有找到一种方法. 将其添加到单个函数中的好处.会是这样吗? $(document).ready(function(){ $(".woocommerce-cart").html(function(i,val){ 返回val.replace("→",""); return val.replace("←",""); }); }); 我将研究入队脚本.似乎有点复杂..谢谢!Nope, I'm pretty sure it can't be removed before inserted. If it can, I haven't been able to find a way to do it. Good point about adding it in a single function. Would it look like this? $(document).ready(function() { $(".woocommerce-cart").html(function(i, val) { return val.replace(" →", ""); return val.replace("← ", ""); }); }); I will look into the enqueue script. Seems a bit complicated, though.. Thanks!
- 0
- 2014-06-25
- user2806026
-
好的.我尝试过这种方法; 制作一个名为" removeArrows.js"的文件,并将其放在我的插件文件夹中.它具有与原始代码相同的内容,但jQuery代替$. 然后我将以下内容添加到functions.php; `function wpb_adding_scripts(){ wp_register_script('my_amazing_script',plugins_url('removeArrows.js',__FILE__),array('jquery'),'1.1',true); wp_enqueue_script('my_amazing_script'); } add_action('wp_enqueue_scripts','wpb_adding_scripts'); (对不起,我不知道如何正确显示代码) 这没有用.你能帮我解决吗?Okay. I tried this approach; Made a file named 'removeArrows.js' and placed it in my plugin folder. This has the same content as the original code, except jQuery instead $. then I added the following to functions.php; `function wpb_adding_scripts() { wp_register_script('my_amazing_script', plugins_url('removeArrows.js', __FILE__), array('jquery'),'1.1', true); wp_enqueue_script('my_amazing_script'); } add_action( 'wp_enqueue_scripts', 'wpb_adding_scripts' ); (Sorry, I cant figure out how to make the code display properly) This did not work. Can you help me fix it?
- 0
- 2014-06-25
- user2806026
-
请提交[编辑]并将所有相关信息**直接添加到您的问题中**不要使用评论部分添加代码Please file an [edit] and add all relevant info **directly to your question** Do not use the comment section to add code
- 1
- 2014-06-26
- Pieter Goosen
-
4 个回答
- 投票数
-
- 2014-06-26
您的
$scr
中的wp_register_script()
函数是错误的.假设您的functions.php位于插件内部,而removeArrows.js位于插件的根目录中,则wp_register_script()
应该看起来像这样plugins_url( '/removeArrows.js' , __FILE__ )
另外要注意的一点是,最后加载脚本和样式始终是一个好习惯.这将确保它不会被其他脚本和样式覆盖.为此,只需在
$priority)中添加一个非常低的优先级(非常高的数字) rel="noreferrer"> add_action
.add_action( 'wp_enqueue_scripts', 'wpb_adding_scripts', 999 );
并且始终通过
wp_enqueue_scripts
操作挂钩,因为这是要使用的合适的挂钩. 请勿直接将脚本和样式加载到wp_head
或wp_footer
编辑
对于主题,您已经指出现在将所有内容移至主题,因此
wp_register_script()
将更改为此主题get_template_directory_uri() . '/removeArrows.js'
关于父主题和这个
get_stylesheet_directory_uri() . '/removeArrows.js'
儿童主题.您的完整代码应如下所示:
function wpb_adding_scripts() { wp_register_script('my_amazing_script', get_template_directory_uri() . '/removeArrows.js', array('jquery'),'1.1', true); wp_enqueue_script('my_amazing_script'); } add_action( 'wp_enqueue_scripts', 'wpb_adding_scripts', 999 );
Your
$scr
in yourwp_register_script()
function is wrong. Given that your functions.php is inside your plugin, and your removeArrows.js is in the root of your plugin, your$scr
should look like thisplugins_url( '/removeArrows.js' , __FILE__ )
Another point of note, it is always good practice to load your scripts and styles last. This will ensure that it will not get overriden by other scripts and styles. To do this, just add a very low priority (very high number) to your priority parameter (
$priority
) ofadd_action
.add_action( 'wp_enqueue_scripts', 'wpb_adding_scripts', 999 );
And always load/enqueue scripts and styles via the
wp_enqueue_scripts
action hook, as this is the proper hook to use. Do not load scripts and styles directly towp_head
orwp_footer
EDIT
For themes, as you've indicated that you now moved everything to your theme, your
$scr
would change to thisget_template_directory_uri() . '/removeArrows.js'
for parent themes and this
get_stylesheet_directory_uri() . '/removeArrows.js'
for child themes. Your complete code should look like this
function wpb_adding_scripts() { wp_register_script('my_amazing_script', get_template_directory_uri() . '/removeArrows.js', array('jquery'),'1.1', true); wp_enqueue_script('my_amazing_script'); } add_action( 'wp_enqueue_scripts', 'wpb_adding_scripts', 999 );
-
非常感谢您的出色建议.这似乎是使用的方法.不过有一个问题;functions.php在我的主题文件夹中.如果js文件位于同一个主题根文件夹中,该如何链接?Thanks a lot for your great advice. This seems like the approach to use. One question though; the functions.php is in my theme folder. How would I link the js-file if it's just in the same, theme root folder?
- 0
- 2014-06-26
- user2806026
-
您应该将所有内容都保留在插件或主题中,不要拆分它们.如果您使用主题,则$ scr为get_template_directory_uri()."/removeArrows.js"(用于父主题)和"get_templatestylesheet_directory_uri()".'/removeArrows.js'`表示子主题You should keep everything in a plugin or in a theme, don't split them. If you are in a theme, your `$scr` would be `get_template_directory_uri() . '/removeArrows.js'` for parent themes, and `get_templatestylesheet_directory_uri() . '/removeArrows.js'` for childthemes
- 0
- 2014-06-26
- Pieter Goosen
-
再次尝试,这次将removeArrows.js直接添加到主题文件夹中,并在functions.php中使用以下内容; 函数wpb_adding_scripts(){ wp_register_script('my_amazing_script',get_template_directory_uri().'/removeArrows.js',__FILE__),array('jquery'),'1.1',true); wp_enqueue_script('my_amazing_script'); } add_action('wp_enqueue_scripts','wpb_adding_scripts',999); 这给了我Parse错误:wp_register_script行上的语法错误,意外的','.Tried again, this time adding the removeArrows.js directly in theme folder and using the following in functions.php; function wpb_adding_scripts() { wp_register_script('my_amazing_script', get_template_directory_uri() . '/removeArrows.js', __FILE__), array('jquery'),'1.1', true); wp_enqueue_script('my_amazing_script'); } add_action( 'wp_enqueue_scripts', 'wpb_adding_scripts', 999 ); this gives me Parse error: syntax error, unexpected ',' on the wp_register_script line.
- 0
- 2014-06-26
- user2806026
-
`get_template_directory_uri().'/removeArrows.js',FILE)`应该只是`get_template_directory_uri().'/removeArrows.js'``get_template_directory_uri() . '/removeArrows.js', FILE)` should just be `get_template_directory_uri() . '/removeArrows.js'`
- 0
- 2014-06-26
- Pieter Goosen
-
不.使用您在原始文章底部编辑的完整代码.唯一要做的就是在查看内容时冻结购物车页面.我想我会放弃:-) 不得已而为之;您首先提到get_template_directory_uri()是针对父主题的,然后在最后的完整代码中针对子主题.哪有我的主题是父母:-)Nope. Used your completely code you edited into the bottom of your original post. Only thing it does is to freeze the cart page when viewing the contents. I think I'll just give up :-) One last resort though; you started by mentioning that get_template_directory_uri() is for parent themes, and then in the final complete code that it's for child themes. Which is it? My theme is a parent :-)
- 0
- 2014-06-27
- user2806026
-
抱歉,在该处复制和粘贴错误.完整代码的最后一部分是针对父主题的.如果这不起作用,则需要查看您的脚本Sorry, made a copy and paste error there. The last piece of complete code is for parent theme. If this doesn't work, you will need to have a look at your script
- 0
- 2014-06-27
- Pieter Goosen
-
- 2014-06-25
我不会添加另一个外部js文件,它只是一个额外的不必要的资源可获取,因此我们希望在页面加载时间方面减少这种情况.
我会使用
wp_head
钩子将此jQuery代码段添加到您的网站标题中.您将以下内容粘贴到主题或插件功能文件中.我还确保jQuery处于无冲突模式,如下所示.add_action('wp_head','woocommerce_js'); function woocommerce_js() { // break out of php ?> jQuery(document).ready(function($) { $(".woocommerce-cart").html(function(i, val) { return val.replace(" →", ""); }); $(".woocommerce-cart").html(function(i, val) { return val.replace("← ", ""); }); }); <?php } // break back into php
完成此操作并刷新页面后,请检查页面源代码,以确保实际上已将此jQuery代码段加载到页面中.如果是这样的话,除非它们在您使用的实际jQuery代码段中有问题,否则它应该可以工作.
I would not add another external js file, its just an extra and unnecessary resource to fetch and that is something we want to cut down on in terms of page loading times.
I would add this jQuery snippet in your websites head using the
wp_head
hook. You would paste the following in your theme or plugins functions file. I have also made sure jQuery is in no-conflict mode as you can see below.add_action('wp_head','woocommerce_js'); function woocommerce_js() { // break out of php ?> jQuery(document).ready(function($) { $(".woocommerce-cart").html(function(i, val) { return val.replace(" →", ""); }); $(".woocommerce-cart").html(function(i, val) { return val.replace("← ", ""); }); }); <?php } // break back into php
Once you have done this and refreshed your page, check the page source to make sure this jQuery snippet is in fact being loaded into your page. If it is then it should work unless their is something off in the actual jQuery snippet you are using.
-
不过,这不是WordPress加载Javascript的方式.有关更多信息,请参见[`wp_enqueue_script()`](https://codex.wordpress.org/Function_Reference/wp_enqueue_script).That's not the WordPress way to load Javascript, though. See [`wp_enqueue_script()`](https://codex.wordpress.org/Function_Reference/wp_enqueue_script) for more information.
- 0
- 2014-06-25
- Pat J
-
嗨,@ PatJ,我同意,要加载包含所有Javascript函数的外部JS库或JS文件,那么绝对可以,这是正确的方法.但是,如果您正在加载一小段Javascript,则没有必要创建一个全新的JS文件并为此添加一个额外的HTTP请求.以Google Analytics(分析)为例,在99%的主题或自定义构建中,JS将通过主题选项或函数文件添加到页眉或页脚中.这种方法通常包括JS甚至CSS代码段.Hi @PatJ, I agree, for loading an external JS library or JS file with all your Javascript functions in it, then yes absolutely that would be the correct way. However if you are loading a snippet of Javascript it does not make sense to create a whole new JS file and add an additional HTTP request just for that. Take Google Analytics for example, in 99% of themes or custom builds, the JS will be added into the the header or footer via theme options or functions file. Its common practice to include JS or even CSS snippets this way.
- 2
- 2014-06-25
- Matt Royal
-
但是,"常规做法"并没有使它正确.[`wp_enqueue_script()`docs](https://codex.wordpress.org/Function_Reference/wp_enqueue_script)甚至声明**这是将JavaScript链接到WordPress生成的页面的推荐方法**."Common practice" doesn't make it correct, though. The [`wp_enqueue_script()` docs](https://codex.wordpress.org/Function_Reference/wp_enqueue_script) even state **This is the recommended method of linking JavaScript to a WordPress generated page**.
- 1
- 2014-06-26
- Pat J
-
如果您采用WordPress默认的二十四主题,它将在header.php中加载html5.js.以某种方式授予doe的理由,以便检查浏览器是否符合IE <9的条件.试图实现它可能并不总是最实际的,我认为应该采取一些谨慎的态度.看,我也可能完全错误地认为,我很想听听一些真正有经验的家伙怎么说:-)If you take WordPress default twentyfourteen theme, it loads html5.js in the header.php. Granted its doe this way for a reason so as to check of the browser meets the condition of being IE < 9, but my point is that understandably, enqueuing is the recommended and preferred method but depending on all the other variables/circumstances surrounding what you are trying to achieve it may not always be the most practical and I think some discretion should be used. Look, I could be completely wrong in this view as well, I'm interested to hear what some of the really experienced guys have to say :-)
- 0
- 2014-06-26
- Matt Royal
-
感谢您的出色建议.但是我无法使它工作.如果我在我的functions.php的Thanks for your great suggestion. I can't get it to work though; if I add your code inside the
- 0
- 2014-06-26
- user2806026
将其粘贴到functions.php文件中时-从我提供的代码中删除第一个`When you paste it in your functions.php file - remove the first `- 0
- 2014-06-26
- Matt Royal
此JS代码需要包装在中.不建议使用这种在WP中呈现JS的方法,但是在某些情况下,快速解决方案比最佳实践更重要.This JS code needs to wrapped in . This method to render JS in WP is not recommended, but in some cases quick solutions are more important than best practices.- 0
- 2020-01-09
- Tahi Reu
- 2018-08-24
因为答案已经被接受,所以我只想说有另一种方法可以将javascript代码放入页脚中,这已经做了很多次了.
在functions.php文件中:
function load_script_to_remove_arrow(){ ?> <script> $(document).ready(function() { $(".woocommerce-cart").html(function(i, val) { return val.replace(" →", ""); }); $(".woocommerce-cart").html(function(i, val) { return val.replace("← ", ""); }); }); </script> <?php } add_action( 'wp_footer', 'load_script_to_remove_arrow' );
您只能通过使用条件将此脚本加载到特定页面模板
if( is_page_template( 'page-template.php' ) ): //put above code (or just add_action part) inside this condition to load file only if the condition is true endif;
如果page-template.php位于目录中(例如,主题根目录中的templates目录),则可以这样写:
is_page_template( 'templates/page-template.php' );
As the answer is accepted already so, I just want to say there's another way to enqueue javascript code in footer which I have done many times.
in functions.php file:
function load_script_to_remove_arrow(){ ?> <script> $(document).ready(function() { $(".woocommerce-cart").html(function(i, val) { return val.replace(" →", ""); }); $(".woocommerce-cart").html(function(i, val) { return val.replace("← ", ""); }); }); </script> <?php } add_action( 'wp_footer', 'load_script_to_remove_arrow' );
you can load this script to particular page template only by using condition
if( is_page_template( 'page-template.php' ) ): //put above code (or just add_action part) inside this condition to load file only if the condition is true endif;
if the page-template.php is in directory ( say templates directory in your theme's root dir ) you can write like:
is_page_template( 'templates/page-template.php' );
-
我不建议像这样将JavaScript"烘焙"到页脚中.它防止它不可钩挂或不可修改(至少很容易),这在插件和主题开发中非常重要.如果您是站点的最终用户,并且需要快速的脚本或其他内容,请继续使用它-但即使这样,`wp_enqueue_script()`仍然总是普遍更好,更灵活.I would not recommend "baking" the JavaScript into the footer like this. It prevents it from being unhookable or modifiable (at least, easily) which is extremely important in plugin and theme development. If you're the end-user of a site and need a quick script or something, go for it - but even still `wp_enqueue_script()` is almost always universally better and more flexible.
- 0
- 2018-08-24
- Xhynk
- 2018-08-24
要回答这个问题,我们必须首先区分javascript和JQuery.
以一种简单的方式陈述它:
- Javascript基于ECMAScript
- JQuery是Javascript库
所以实际上您会问两个不同的问题:
- 您的标题是有关实现javascript的解决方案
- 您的问题涉及实现JQuery的解决方案
因为Google搜索结果显示了您的标题,并且页面上所有有关JQuery的内容都对搜索javascript解决方案的人感到非常沮丧.
现在使用JQuery解决方案:
wp_enqueue_script( 'script-id', get_template_directory_uri() . '/js/script.js', array ( 'jquery' ), 1.1, true);
以及javascript解决方案:
wp_enqueue_script( 'script-id', get_template_directory_uri() . '/js/script.js', array(), '1.0.0', true );
此代码可以添加到您的functions.php中 两种情况下脚本的位置均为
wp-content/themes/theme-name/js/script.js
祝您编程愉快!
To answer the question we must first make a distinction between javascript and JQuery.
To state it in a simple fashion:
- Javascript is based on ECMAScript
- JQuery is a library for Javascript
So in reality you ask two different questions:
- Your title speaks about a solution for implementing javascript
- Your question speaks about a solution for implementing JQuery
Because the Google results show your title and all the content of the page talks about JQuery this can become very frustrating to people that search for a javascript solution.
And now for the JQuery solution:
wp_enqueue_script( 'script-id', get_template_directory_uri() . '/js/script.js', array ( 'jquery' ), 1.1, true);
And the javascript solution:
wp_enqueue_script( 'script-id', get_template_directory_uri() . '/js/script.js', array(), '1.0.0', true );
This code can be added to your functions.php The location of the scripts in both cases is
wp-content/themes/theme-name/js/script.js
Happy coding!
-
在OP发布时,开发人员确实可以交替使用jquery和javascript.它一点也不准确,但这是jquery的流行程度以及javascript缺少多少功能.Around the time when OP posted, devs did use jquery and javascript interchangeably. It's not at all accurate, but it was how popular jquery was and how much javascript was missing features.
- 0
- 2020-04-29
- Rocky Kev
我想删除WooCommerce中购物车按钮上标准的一些难看的箭头.为此,我发现了添加以下代码的技巧,该代码应在文档加载后删除箭头.
我假设我要将其放入我的functions.php中?我该怎么做?
编辑
好的.我尝试过这种方法:
制作一个名为" removeArrows.js"的文件,并将其放在我的插件文件夹中.它具有与原始代码相同的内容,但jQuery代替$.然后,将以下内容添加到functions.php中:
我不知道如何正确显示代码.这没有用.有什么建议可以使这项工作吗?
jQuery代码段源