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

标签 javascript php wordpress

我正在修改开发人员推荐 ( See latest post ) 的插件,建议在 functions.php 中编写一些 Javascript的 Wordpress。

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

所以我把它们写在一个 <script> 中标签,以及 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

这是代码文件 enter image description here

所以我的问题是:
问题是什么?如何使这些消息消失?

正确答案

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

最佳答案

在wordpress中有更好的标准方式

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

关于javascript - 如何在 Wordpress 的 functions.php 中编写 Javascript 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24587280/

相关文章:

javascript - jQuery Datatable 在更新第 2 部分后将 CSS 添加到单元格

javascript - 如何从 https 弹出窗口重新加载 http window.opener?

javascript - D3 svg 组件不可调整大小

php - fatal error : Call to undefined function mcrypt_encrypt()

php - 为什么这个脚本不起作用? (奇偶)

html - 将定义的高度更改为相对内容

javascript - 使用 csv jquery 插件时出现 "Uncaught TypeError: csv.replace is not a function",如何解决此问题?

php - 用php动态输出页面标题

wordpress - 如何在 Wordpress 中将 https 重定向到 http

Php7 在 nginx 上安装 wordpress 抛出 PHP 安装缺少 WordPress 所需的 MySQL 扩展