doujing3896 2014-12-26 05: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 05: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条)
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 vscode开发micropython,import模块出现异常
  • ¥20 Excel数据自动录入表单并提交
  • ¥30 silcavo仿真,30分钟,只需要代码
  • ¥15 FastReport 怎么实现打印后马上关闭打印预览窗口
  • ¥15 利用3支股票数据估计其均值和方差的95%置信区间。
  • ¥15 微信小程序运行一项功能时,弹出未知错误弹框,检查代码没有问题
  • ¥15 ATAC测序生成self-pseudo replicates之前是否要进行去线粒体reads
  • ¥15 python模糊字匹配函数问题
  • ¥20 谁刷目标页面的uv记录器上数据,数据只记录跳转的数值
  • ¥30 数据库软件的安装方法
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部