duanbiao4025 2015-10-21 05:48
浏览 67
已采纳

如何将wp插件中的javascript包含到网站上的所有页面?

I have a plugin that has js file in its folder.

My main plugin file looks like this:

require('api/myapi.class.php'); // API Library 
require('settings.php'); // Configuration Options
require('register_shortcodes.php');  

How to add a function that will inject js file to all client pages (Not admin pages) I guess I need to use wp_enqueue_script but where should I add it?

wp_enqueue_script("myscript.js");
  • 写回答

1条回答 默认 最新

  • dqyy38265 2015-10-21 06:24
    关注

    Try something like:

    add_action( 'wp_enqueue_scripts', 'my_plugin_scripts' );
    
    function my_plugin_scripts() {
        wp_enqueue_script( 'myscript', plugins_url().'/plugin_name/js/myscript.js', array('jquery'));
    }
    

    This assumes you put your script in the js folder of the plugin folder, and that it depends on jquery.

    EDIT:

    A user tried to edit my answer, that added no real improvement to my answer. To explain my answer a bit more. You hook to wp_enqueue_scripts which will show the function with the enque script only on the front end, and not in the admin area, which is what the OP wanted, so there is really no need to check if you're on the admin page.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面