wordpress 重力形式上的js错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23786388/
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-09-08 19:32:24  来源:igfitidea点击:

js error on gravity forms

wordpressgravity-forms-plugin

提问by stephan2307

We are hosting wordpress sites on wpengine. On this one site we are using gravity forms but for some reason it stopped working. All we get is a js error

我们在 wpengine 上托管 wordpress 网站。在这个站点上,我们使用重力形式,但由于某种原因它停止工作。我们得到的只是一个 js 错误

Uncaught ReferenceError: gformInitSpinner is not defined (index):135
(anonymous function) (index):135
o jquery.min.js:2
p.fireWith jquery.min.js:2
e.extend.ready jquery.min.js:2
c.addEventListener.B

Now if I set up the site on my local machine it works perfectly fine. Has anyone come across this problem? Anyone got any idea why this is happening?

现在,如果我在本地机器上设置站点,它就可以正常工作。有没有人遇到过这个问题?任何人都知道为什么会发生这种情况?

回答by richardW8k

The most common cause of this issue is the gravityforms.js file being included down in the footer when it should be up in the header.

这个问题的最常见原因是gravityforms.js 文件被包含在页脚中,而它应该在页眉中。

If you are embedding the form using the function call there is a second function call you should use to include the scripts and stylesheets in the header.php

如果您使用函数调用嵌入表单,则应使用第二个函数调用将脚本和样式表包含在 header.php 中

gravity_form_enqueue_scripts(form_id, ajax);
gravity_form_enqueue_scripts(4, true);

https://docs.gravityforms.com/gravity_form_enqueue_scripts/

https://docs.gravityforms.com/gravity_form_enqueue_scripts/

回答by Sean

I had exactly the same problem, and managed to track it down to some theme code.

我遇到了完全相同的问题,并设法将其追溯到某些主题代码。

I was using the bones theme, which de-registers the default jQuery JS and adds its own using the Google CDN, like:

我正在使用骨骼主题,它取消注册默认的 jQuery JS 并使用 Google CDN 添加它自己的,例如:

// we don't need the Wordpress jquery as we are loading our own version
add_action('wp_enqueue_scripts', 'stnic_remove_jquery');
function stnic_remove_jquery(){
    if(!is_admin()) {
        wp_deregister_script('jquery');
    }
}


// loading modernizr and jquery, and reply script
function bones_scripts_and_styles() {
    if (!is_admin()) {
        wp_register_script( 'cdn-jquery', 'https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js', array(), '', false );
    }
}

As you can see, it deregister's the default jqueryscript, and then adds its own cdn-jqueryscript, which is fine, apart from the fact that the Gravity forms scripts have a dependency on jqueryand not cdn-jquery!

如您所见,它deregister是默认jquery脚本,然后添加了自己的cdn-jquery脚本,这很好,除了 Gravity 表单脚本依赖于jquery而不依赖于cdn-jquery!

Because they can't see the default jqueryscript, they don't load, and it would seem that they fail silently, simply emitting this JavaScript error because said JavaScript is loaded without checking dependencies.

因为他们看不到默认jquery脚本,所以他们不会加载,而且他们似乎默默地失败了,只是发出这个 JavaScript 错误,因为所述 JavaScript 是在没有检查依赖关系的情况下加载的。

In any case, I fixed it by re-naming the bones register script to jquery, might not be the best way to fix this, but it works.

无论如何,我通过将骨骼寄存器脚本重命名为jquery来修复它,这可能不是解决此问题的最佳方法,但它确实有效。

Alternatively, commenting out both pieces of code would also fix this (and leave the default Wordpress JS in there).

或者,注释掉两段代码也可以解决这个问题(并保留默认的 Wordpress JS)。

Not sure if other themes do this, but might be worth doing a search all in your theme for wp_deregister_script('jquery');or at least switching to the default theme to see if you experience the same problem (that's how I pinpointed this).

不确定其他主题是否这样做,但可能值得在您的主题中搜索所有主题wp_deregister_script('jquery');或至少切换到默认主题以查看您是否遇到相同的问题(这就是我指出的问题)。

回答by stephan2307

OK I solved the problem. For some reason the file form_display.php was for some reason not up to date. So I simply pushed that one file to the server and this fixed it.

好的我解决了这个问题。出于某种原因,文件 form_display.php 由于某种原因不是最新的。所以我只是将那个文件推送到服务器,然后修复了它。

回答by Andrews32

You can also add the GF`s necessary scripts manually to header.php (no actions with registering/deregistering jQuery needed!). Example:

您还可以手动将 GF 的必要脚本添加到 header.php(不需要注册/注销 jQuery 的操作!)。例子:

<link rel='stylesheet' id='gforms_formsmain_css-css'  href='/wp-content/plugins/gravityforms/css/formsmain.min.css' type='text/css' media='all' />
<link rel='stylesheet' id='gforms_ready_class_css-css'  href='/wp-content/plugins/gravityforms/css/readyclass.min.css' type='text/css' media='all' />
<link rel='stylesheet' id='gforms_browsers_css-css'  href='/wp-content/plugins/gravityforms/css/browsers.min.css' type='text/css' media='all' />
<script type='text/javascript' src='/wp-content/plugins/gravityforms/js/jquery.json-1.3.js'></script>
<script type='text/javascript' src='/wp-content/plugins/gravityforms/js/gravityforms.min.js'></script>

回答by Tusko Trush

Move Gravity forms` scripts to footer

将 Gravity 表单的脚本移至页脚

add_filter("gform_init_scripts_footer", "init_scripts");
function init_scripts() {
    return true;
}

回答by Mithc

I just had this problem and, in my case, it was caused by CloudFlare RocketLoader?. After I disabled it, the form loaded with no problem.

我刚刚遇到了这个问题,就我而言,它是由 CloudFlare RocketLoader? 引起的。在我禁用它后,表单加载没有问题。