javascript 如何在Wordpress的functions.php里面写一个Javascript函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24587280/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 03:05:23  来源:igfitidea点击:

How to write a Javascript function inside the functions.php of Wordpress?

javascriptphpwordpress

提问by Sander Schaeffer

I'm altering a plugin on developer recommendations (See latest post), which advices to write a bit of Javascript in the functions.phpof Wordpress.

我正在更改开发人员推荐的插件(请参阅最新帖子),该插件建议在functions.phpWordpress 中编写一些 Javascript 。

Now, when I did that my whole wordpress went blank. Well, after a bit of thinking.. "You can't write plain Javascript inside of PHP"..

现在,当我这样做时,我的整个 wordpress 都变成了空白。好吧,经过一番思考..“你不能在 PHP 中编写普通的 Javascript”..

So I wrote them inside a <script>tag, and that inside of a echo ", ";tags. Now.. I'm unsure of the function working (have not tested that yet), but Wordpress is showing again, but.. I'm getting the message

所以我把它们写在一个<script>标签里,然后写在a echo ",";标签里。现在..我不确定该功能是否有效(尚未测试),但 Wordpress 再次显示,但是..我收到消息

Notice: Undefined variable: group in /home/appelhulp/domains/appelhulp.nl/public_html/wp-content/themes/Divi/functions.php on line 8
Notice: Undefined variable: option in /home/appelhulp/domains/appelhulp.nl/public_html/wp-content/themes/Divi/functions.php on line 9
Notice: Undefined variable: option in /home/appelhulp/domains/appelhulp.nl/public_html/wp-content/themes/Divi/functions.php on line 25

注意:未定义变量:组在 /home/appelhulp/domains/appelhulp.nl/public_html/wp-content/themes/Divi/functions.php 第 8 行
注意:未定义变量:/home/appelhulp/domains/appelhulp 中的选项。第 9 行的 nl/public_html/wp-content/themes/Divi/functions.php
注意:未定义变量:/home/appelhulp/domains/appelhulp.nl/public_html/wp-content/themes/Divi/functions.php 中的选项第 25 行

And this is the code file enter image description here

这是代码文件 在此处输入图片说明

So my question:
What is the problem and how do I make those messages disappear?

所以我的问题是:有
什么问题,我该如何让这些消息消失?

The correct answer

正确答案

Although I accepted @Burak's answer, this is only half of the correct answer. Please also see @RahilWazir's part, which half of his/her answer belongs to the solution. As Burak came with the correct script, RahilWazir came with the suggestion to place it in the footer.phpinstead of functions.php. And placing it there, did the trick. In functions.phpdidn't let me achieve what I was seeking. Therefor, thanks both!

虽然我接受了@Burak 的回答,但这只是正确答案的一半。另请参阅@RahilWazir 的部分,他/她的答案的哪一半属于解决方案。由于 Burak 提供了正确的脚本,RahilWazir 提出了将它放在footer.php而不是functions.php. 把它放在那里,成功了。在functions.php没有让我实现我所寻求的。因此,谢谢两位!

回答by codepixlabs

there is a better and standard way in wordpress

wordpress 有一个更好和标准的方法

<?php
function add_js_functions(){
?>
<script>
// your js functions here 
</script>
<?php
}
add_action('wp_head','add_js_functions');

回答by Rahil Wazir

You need to wait for DOM to be fully initialized. So wrap your jQuery snippet within readyhandler. Or even better place is to place this snippet in your footer.phpfile before closing </body>tag.

您需要等待 DOM 完全初始化。因此,将您的 jQuery 代码段包装在ready处理程序中。或者更好的地方是footer.php在关闭</body>标签之前将此代码段放在您的文件中。

jQuery(document).ready(function($) { // <-- wrap it
    var categories = {};
    var $group = jQuery();
    jQuery('select.app_select_services option').remove().each(function() {
        var $option = jQuery(this),
            contents = $option.text().split(' - '),
            group = jQuery.trim(contents[1]);

        if (!categories.hasOwnProperty(group)) {
            var optGroup = jQuery('<optgroup />', { label: group });
            optGroup.appendTo('select.app_select_services');
            categories[group] = optGroup;
        }

        categories[group].append(jQuery('<option />', {
            text: jQuery.trim(contents[0]),
            value: jQuery(this).val()
        }));
    });
});

回答by Burak

As you are using double quotes, PHP will think that the variable with $signs as a php variable. You need to either change them to single quote and rearrange the code either you need to take a look at heredocwhich is suitable for situations like this.

当您使用双引号时,PHP 会认为带$符号的变量为 php 变量。您需要将它们更改为单引号并重新排列代码,或者您需要查看适用于此类情况的heredoc

echo <<<'EOT'
<script>
( function() {
    var categories = {};
    var $group = jQuery();
    jQuery('select.app_select_services option').remove().each(function() {
        var $option = jQuery(this),
            contents = $option.text().split(' - '),
            group = jQuery.trim(contents[1]);

        if (!categories.hasOwnProperty(group)) {
            var optGroup = jQuery('<optgroup />', { label: group });
            optGroup.appendTo('select.app_select_services');
            categories[group] = optGroup;
        }

        categories[group].append(jQuery('<option />', {
            text: jQuery.trim(contents[0]),
            value: jQuery(this).val()
        }));
    });
} )();
</script>
EOT;

回答by Lewis

When you use double quotes ""to define JS string, PHP will interpret $groupas a variable. In order to fix it, you need to use single quotes ''instead.

当你使用双引号""定义 JS 字符串时,PHP 会解释$group为一个变量。为了修复它,您需要改用单引号''