简体   繁体   English

WordPress在插件中调用javascript函数

[英]wordpress calling javascript function in plugin

i'm struggling with a way to make javascript calls from within my wordpress plugin. 我正在努力从wordpress插件中进行javascript调用。 I have an external javascript file with different functions in it that i'd like to be able to call individually and whenever i need them. 我有一个具有不同功能的外部javascript文件,希望可以在需要时单独调用它们。

in my main plugin file i register and enqueue the javascript file. 在我的主插件文件中,我注册并加入了javascript文件。

wp_register_script( 'pluginMain', plugins_url( '/js/pluginMain.js', __FILE__ ) ); wp_enqueue_script( 'pluginMain' );

inside the pluginMain.js file i want to put several different functions, and call them when needed on different pages in the site. 我要在pluginMain.js文件中放置几个​​不同的函数,并在需要时在网站的不同页面上调用它们。 an example is this redirect function. 这个重定向功能就是一个例子。

function redirectToHome() {
alert( 'Something went wrong.  You are being redirected home.' );

window.location.replace("https://somemainpage.com");
}

the only way i can figure out how to make anything happen is to add the below code to the javascript file. 我能弄清楚如何进行任何事情的唯一方法是将以下代码添加到javascript文件中。 but then it runs any time after the file has been enqueued, so everywhere. 但是在文件排队后,它可以随时运行,因此无处不在。

jQuery (document).ready (function () {
redirectToHome();
});

i tried adding the jQuery (document) code to the html of a page and removing the jQuery (document) code from the javascript file, but then nothing ever happens. 我尝试将jQuery(文档)代码添加到页面的html中,并从javascript文件中删除jQuery(文档)代码,但此后没有任何反应。

echo '<p>Something has gone wrong.  You are being returned to the main page</p>';

    echo '<script type="text/javascript">
    <!--
    jQuery (document).ready (function () {
    redirectToHome();
    });
    //-->
    </script>';

i'm obviously new to this, and pretty confused as to where to go from here. 我显然是新来的,对于从这里去哪里很困惑。 can i get some guidance? 我可以得到一些指导吗? thanks. 谢谢。

If you only need to run the script in certain situations, you want to do something like this: 如果只需要在某些情况下运行脚本,则可以执行以下操作:

wp_register_script( 'pluginMain', plugins_url( 
'/js/pluginMain.js', __FILE__ ) );

This will register, but not load the script until you do an enqueue. 这将注册,但在入队之前不会加载脚本。 You could then do this: 然后,您可以这样做:

add_action('loadmyscripthereplz', 'somefunction');
function somefunction(){
   wp_enqueue_script('pluginMain');
}

And then, wherever you want the script to load, eg a page template, just do: 然后,无论您要将脚本加载到何处,例如页面模板,都可以执行以下操作:

do_action('loadmyscripthereplz');

This would allow you to literally place the script call anywhere you want. 这将使您可以将脚本调用实际放置在所需的任何位置。 Alternatively, you could just register the script in your function file like you have done, and conditionally call the wp_enqueue_script('script') with checks like is_front_page() or whatever other WP checks you need. 另外,您也可以像完成操作一样在功能文件中注册脚本,然后使用is_front_page()之类的检查或您需要的任何其他WP检查有条件地调用wp_enqueue_script('script')。 This will allow you to choose where and when the script loads as well. 这将允许您选择脚本的加载位置和时间。

As an aside, please ensure that you're doing proper checks/validations when doing redirects, as this could potentially be a security hole. 顺便说一句,请确保在进行重定向时进行正确的检查/验证,因为这可能会带来安全漏洞。

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

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