我想寻求有关在functions.php文件中加载自定义JS文件的帮助。我目前在functions.php(子主题)中有以下内容:
<?php
function my_theme_enqueue_styles() {
$parent_style = 'parent-style';
wp_enqueue_style( $parent_style, get_template_directory_uri() . '/style.css' );
wp_enqueue_style( 'child-style', get_stylesheet_directory_uri() . '/style.css', array( $parent_style ), wp_get_theme()->get('Version'));
wp_enqueue_style('jquery.mobile-1.4.5.css', get_stylesheet_directory_uri().'/js/lib/jquery.mobile-1.4.5.css');
}
function wp_adding_scripts() {
wp_register_script('app.js', get_stylesheet_directory_uri() .'/js/app.js', array(), '', true);
wp_enqueue_scripts('app.js');
}
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles');
add_action( 'wp_enqueue_scripts', 'wp_adding_scripts');
?>
这当前位于我的 app.js 文件中:
jQuery(document).ready(function(){
alert('call');
});
当我加载网站时,出现 504 网关超时。后端加载良好(即插件、外观等)。我在控制台中没有收到任何内容(我认为它根本没有加载页面?)。
我选择JS文件内容是为了测试它是否正在加载文件。
如果我从函数文件中删除对 app.js 的引用,网站会正常加载,但不幸的是没有可用的自定义 JavaScript。
如果我还可以提供任何进一步信息,请告诉我。
编辑:
好的,我已将其修改为如下(请告知我是否要完整粘贴,仅显示修改后的部分):
function wp_adding_scripts_bnyxwshpit() {
wp_register_script('app_amesmvzuoy.js', get_stylesheet_directory_uri() .'/js/app_hrlobpnygg.js', array('jquery'), '', true);
wp_enqueue_scripts('app_amesmvzuoy.js');
}
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles');
add_action( 'wp_enqueue_scripts', 'wp_adding_scripts_bnyxwshpit');
有趣的是,在 php_errorlog(在/public_html 中)中,我收到以下错误:
PHP Warning: mkdir(): Permission denied in phar:///chroot/wp-cli/vendor/wp-cli/wp-cli/bin/wp-cli.phar/php/WP_CLI/FileCache.php on line 265
debug.log 文件没有记录任何内容(除了我在不带撇号的情况下将 jquery 输入到数组依赖项中时)。
Cpanel错误日志:
[Mon Mar 26 08:44:40.640027 2018] [:notice] [pid 56696] [client 165.225.81.57:57752] mod_hive matched URI http://www.knowthelanguage.co.uk/wp-login.php (/home/knowthe8/public_html/wp-login.php) with match 2, referer: https://www.knowthelanguage.co.uk/wp-login.php?redirect_to=https%3A%2F%2Fwww.knowthelanguage.co.uk%2Fwp-admin%2F&reauth=1
[Mon Mar 26 08:45:42.079235 2018] [:warn] [pid 53633] [client 165.225.81.57:33380] Timeout waiting for output from CGI script /home/knowthe8/public_html/index.php
最佳答案
您可能在某个地方发生了名称冲突?
我会尝试以下操作:
- 尝试将
wp_adding_scripts()
重命名为独特的名称 ( use random.org for instance ),例如wp_adding_scripts_tkpirpzivx()
- 将脚本句柄
app.js
重命名为唯一的名称,例如app.js.uwprrjopmo
- 检查
wp_register_script
的返回值。请参阅WP doc :(bool) Whether the script has been registered. True on success, false on failure.
- 激活
WP_DEBUG
(在wp-config.php
中保持不变, see more info here )并使用 PHP 内置函数error_log( “我在这里”);
。请注意,您的错误日志文件位于/wp-content/debug.log
祝你好运!
关于javascript - 在Functions.php中的wordpress中加载自定义JS后504网关超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49478742/