duanguai2781 2014-08-25 10:11
浏览 48
已采纳

无法将我的javascript加载到wordpress插件中

I have a javascript function that responds to click events

The main issue is that I am calling the panel.js inside my plugin.php and nothing happens at all.

BEFORE:

panel.js

$(document).ready(function(){
    $(".next_panel").click(function(){
        //Do something here
        return false;
    });
}

This is the webpage (main.html) before I turned into plugin, notice how I load the JS, this works in my actual development.

<head>
    <script src="js/panel.js"></script>
</head>
<body>
    <a href="#" class="next_panel">
        <div class="block"></div>
    </a>
</body>

AFTER:

Now this works as a normal site, however loading this into wordpress as a plugin has caused me some issues, the main one here is that I cannot seem to load the javacript.

panel.js

function panel(){
    $(".next_panel").click(function(){
        //Do something here
        return false;
    });
}

This is the code in the plugin.php once I turned it into a plugin for wordpress, I can confirm wp_head is being called.

wp_enqueue_script('panel', VS_PATH.'js/panel.js', array('jquery'));

//This will run our function when wp_head is called.
add_action('wp_head', 'vs_script');

function vs_get_panel()
{
    $panels = '
    <body>
        <a href="#" class="next_panel">
            <div class="block"></div>
        </a>
    </body>';
    return $panels;
}

function vs_insert_panel($atts, $content=null)
{
    $panel = vs_get_panel();
    return $panel;
}

add_shortcode('v_panel', 'vs_insert_panel');

function vs_script()
{
    print '<script type="text/javascript" charset="utf-8">
                jQuery(document).ready(function($)
                {
                        $(\'.panel\').panel();//This is where my problem is
                });
    </script>';
}

If you look at the last block of code in function vs_script(), this is where I have been recommended the way to get my javascript into the wordpress page once it renders.

The plugin also has more to it, for example images and css, the thing is, all of that works, the css works fine, it's just the javascript that I cannot get working.

After Kats advice:

I still have no javascript loading, my click events are not being acted upon.

plugin.php

function my_js()
{
    wp_enqueue_script('panel', VS_PATH.'js/panel.js', array('jquery'));
}

add_action('admin_enqueue_scripts', 'my_js');
add_action('login_enqueue_scripts', 'my_js');

//This will run our function when wp_head is called.
add_action('wp_head', 'vs_script');

function vs_get_panel()
{
    $panels = '
    <body>
        <a href="#" class="next_panel">
            <div class="block"></div>
        </a>
    </body>';
    return $panels;
}

function vs_insert_panel($atts, $content=null)
{
    $panel = vs_get_panel();
    return $panel;
}

add_shortcode('v_panel', 'vs_insert_panel');

function vs_script()
{
    print '<script type="text/javascript" charset="utf-8">
                jQuery(document).ready(function($)
                {
                        $(\'.panel\').panel();//This is where my problem is
                });
    </script>';
}

panel.js

function panel(){
    $(".next_panel").click(function(){
        //Do something here
        return false;
    });
}
  • 写回答

1条回答 默认 最新

  • douqianni4080 2014-08-25 15:24
    关注

    This wp_enqueue_script('panels', VS_PATH.'js/panels.js', array('jquery')); doesn't work alone.

    Typically this is wrapped inside a function:

    function my_js() {
        wp_enqueue_script('panels', VS_PATH.'js/panels.js', array('jquery'));
    }
    

    then:

    add_action('admin_enqueue_scripts', 'my_js');
    add_action('login_enqueue_scripts', 'my_js');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?