doujing3896 2014-12-26 13:40
浏览 159
已采纳

如何将jquery添加到wordpress插件

This could be a very easy question to wordpress expert. I am a newbie in wordpress and I am having problem using jquery on it. The jquery library is already included because you can see it firebug

I dont know what I am missing here.

This is my code..

add_action( 'wp_enqueue_scripts', array( $this, 'jsscript' ) );


function jsscript() {   
    ?>
    <script type="text/javascript">
        $(document).ready(function(){
            alert('here!');
        });
    </script>
    <?php
    }

The jsscript is already on the firebug but it doesn't work. I am getting this error message in firebug.

ReferenceError: $ is not defined
$(document).ready(function(){

I read somewhere that it is not a good idea to include another jquery library and it make sense not to load it again.

I hope somebody can help me.

Thanks in advance.

  • 写回答

4条回答 默认 最新

  • dongqin6926 2014-12-26 13:48
    关注

    Instead of just randomly outputting your script you should add it in an external file in your plugin directory, and then include that file with jQuery as a dependency

    wp_enqueue_script(
          'jsscript', 
          plugin_dir_path( __FILE__ ) . '/jsscript.js', 
          array( 'jquery' )
    );
    

    The issue you're having now, is that there's no guarantee that the script is outputted after jQuery, in fact there's now guarantee that the script tag you're outputting between the PHP tags is inserted where it's supposed to go at all.

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

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!