dpziir0079 2016-08-23 09:59
浏览 320
已采纳

Wordpress - wp_enqueue_script / wp_enqueue_style不起作用

I have coded myself a little WP-Plugin, where I do

wp_enqueue_script('jquery');        
wp_enqueue_script('myapp1', $pluginpath . 'build/js/app.min.js', array('jquery')); 
wp_enqueue_style("myapp2", $pluginpath . 'build/css/style.css');

while testing on my localhost, everythink worked fine, so I uploaded everything to my server and there, my plugin works, too, but the app.min.js and the style.css is not included(?)

what could be the cause for this?

  • 写回答

2条回答 默认 最新

  • douchuopiao9002 2016-08-23 11:20
    关注

    Files that are referenced with wp_enqueue_script()/wp_enqueue_style() are used in the wp_head() function.

    By the time your shortcode is evaluated this part is already finished, so your files will never be referenced.

    You have to add your style using the wp_enqueue_scripts action:

    function my_scripts() {
        wp_enqueue_style("myapp2", $pluginpath . 'build/css/style.css');
    }
    add_action( 'wp_enqueue_scripts', 'my_scripts' );
    

    There is no way around that, because the style belongs in the head.

    For your JS though, you can use another parameter to load it in the footer:

    wp_enqueue_script('myapp1', $pluginpath . 'build/js/app.min.js', array('jquery'), "1.0", true); 
    

    You can add this in your shortcode so at least the JS will only be loaded when it is needed.

    (The version number can be anything you want, it is useful to increment it when you release new version of your JS file, so browsers don't use old versions from cache.)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd