简体   繁体   English

如何在Wordpress的functions.php中编写Javascript函数?

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

I'm altering a plugin on developer recommendations ( See latest post ), which advices to write a bit of Javascript in the functions.php of Wordpress. 我正在根据开发人员的建议更改插件( 请参阅最新文章 ),该建议建议在Wordpress的functions.php中编写一些Javascript。

Now, when I did that my whole wordpress went blank. 现在,当我这样做时,我的整个wordpress变成了空白。 Well, after a bit of thinking.. "You can't write plain Javascript inside of PHP".. 好了,经过一番思考。“您不能在PHP内编写纯Javascript”。

So I wrote them inside a <script> tag, and that inside of a echo " , "; 因此,我将它们写在<script>标记内,并将其写在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 现在..我不确定该功能是否正常工作(尚未进行测试),但Wordpress再次显示,但是..

Notice: Undefined variable: group in /home/appelhulp/domains/appelhulp.nl/public_html/wp-content/themes/Divi/functions.php on line 8 注意:未定义的变量:第8行/home/appelhulp/domains/appelhulp.nl/public_html/wp-content/themes/Divi/functions.php中的组
Notice: Undefined variable: option in /home/appelhulp/domains/appelhulp.nl/public_html/wp-content/themes/Divi/functions.php on line 9 注意:未定义的变量:/home/appelhulp/domains/appelhulp.nl/public_html/wp-content/themes/Divi/functions.php中第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中的选项,位于第25行

And this is the code file 这是代码文件 在此处输入图片说明

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. 尽管我接受@Burak的答案,但这只是正确答案的一半。 Please also see @RahilWazir's part, which half of his/her answer belongs to the solution. 另请参阅@RahilWazir的部分,他/她的答案的一半属于解决方案。 As Burak came with the correct script, RahilWazir came with the suggestion to place it in the footer.php instead of functions.php . 由于Burak带有正确的脚本,因此RahilWazir提出了将其放在footer.php而不是functions.php And placing it there, did the trick. 然后把它放在那里,就成功了。 In functions.php didn't let me achieve what I was seeking. functions.php中,没有让我达到我想要的目标。 Therefor, thanks both! 因此,谢谢你们!

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');

As you are using double quotes, PHP will think that the variable with $ signs as a php variable. 当您使用双引号时,PHP会将带有$符号的变量视为php变量。 You need to either change them to single quote and rearrange the code either you need to take a look at heredoc which is suitable for situations like this. 您需要将其更改为单引号并重新排列代码,或者需要查看适用于此类情况的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;

You need to wait for DOM to be fully initialized. 您需要等待DOM完全初始化。 So wrap your jQuery snippet within ready handler. 因此,将您的jQuery代码片段包装在ready处理程序中。 Or even better place is to place this snippet in your footer.php file before closing </body> tag. 甚至更好的方法是,在关闭</body>标记之前,将此片段放置在footer.php文件中。

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()
        }));
    });
});

When you use double quotes "" to define JS string, PHP will interpret $group as a variable. 当使用双引号""定义JS字符串时,PHP将$group解释为变量。 In order to fix it, you need to use single quotes '' instead. 为了解决这个问题,您需要使用单引号''

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM