JQuery在我的插件中不起作用

时间:2013-10-12 作者:Blaine

这是我第一次在WordPress插件中使用JQuery,我遇到了一些麻烦。

这是我的“主要”插件文档:

<?php

/*
    Plugin Name: Dump-It Scheduler
    Plugin URI: 
    Description:Description
    Version: 1.0
    Author: Blaine Anderson
    Author URI: http://www.blainevanderson.net
    License: 

*/

add_action( \'wp_enqueue_script\', \'load_jquery\' );
add_action(\'wp_register_script\', \'register_script\');
add_action(\'wp_enqueue_script\', \'run_js_script\');
add_action(\'admin_menu\', \'add_master_schedule\'); 
add_action(\'admin_menu\', \'add_customer_search\'); 
add_action(\'admin_menu\', \'add_edit_trucks\');
add_action(\'admin_menu\', \'add_new_customer\'); 

//Add master schedule link to Admin bar in WP
function add_master_schedule()
{
    add_menu_page(\'Master Schedule\', \'Master Schedule\', \'administrator\', \'Master_Dump-it_Schedule\', \'display_menu\'); 
}

//Display the master schedule when link is clicked
function display_menu()
{
    include( plugin_dir_path( __FILE__ ) . \'/Views/admin_main_display.php\');
}

//Add customer search link to admin bar
function add_customer_search()
{
    add_menu_page(\'Customer Search\', \'Customer Search\', \'administrator\', \'Customer_Search\', \'display_customer_search\');
}

//Display customer search page
function display_customer_search()
{
    include( plugin_dir_path( __FILE__ ) . \'/Views/search_customer.php\');
}

//Add edit trucks link to admin bar
function add_edit_trucks()
{
    add_menu_page(\'Edit Trucks\', \'Edit Trucks\', \'administrator\', \'Display_Edit_Trucks\', \'display_edit_trucks\');
}

//Display edit trucks page when link is clicked.
function display_edit_trucks()
{
    include( plugin_dir_path( __FILE__ ) . \'/Views/edit_trucks.php\');
}

//Add \'add customer\' link to admin bar
function add_new_customer()
{
    add_submenu_page(\'Customer_Search\', \'Add Customer\', \'Add Customer\', \'administrator\', \'Add_Customer\', \'display_add_customer\');
}

//Display add customer page when link is clicked.
function display_add_customer()
{
    include(plugin_dir_path(__FILE__) . \'/Views/add_customer.php\'); 
}

//Load jquery into plugin
function load_jquery() 
{
    wp_enqueue_script( \'jquery\' );
}

function run_js_script()
{
    wp_enqueue_script(\'my_plugin_script\'); 
}
function register_script()
{
    wp_register_script( \'my_plugin_script\', plugins_url(\'/js/scheduler.js\', __FILE__), array(\'jquery\'), \'1.0\', false);
}
?>
下面是我的Javascript,它只显示一个警报(scheduler.js):

jQuery(document).ready(function($){
    alert("Hello World!");
});
我想知道为什么警报没有出现。我还尝试添加wp_enqueue_scriptHTML 文件,如下所示:

<?php
wp_enqueue_script( \'my_plugin_script\' );
?>

<table width="100%" id="menubar">
    <tr>
        <td><a href="" onclick="display_customer_search();">Master Schedule</a> | <a href=truckroutes.php>Create Truck Routes</a> | <a href="search.php">Customer Search</a> | <a href="trucks.php">Edit Trucks</a> | <a href="customeredit.php?id=new">Add Customer</a> | <a href="/cms/login.php">CMS Login</a> | </td> 
    </tr>
</table>
我对WordPress很陌生,如果您能给我任何指导或帮助,我将不胜感激。如果您需要任何其他代码,请让我知道,但这只是其中的大部分。我仅有的其他代码是其他链接的视图。

1 个回复
最合适的回答,由SO网友:Chip Bennett 整理而成

您使用的操作无效:

add_action(\'wp_enqueue_script\', \'run_js_script\');
正确的操作是wp_enqueue_scripts (复数),非wp_enqueue_script (单数)。请改用此选项:

add_action(\'wp_enqueue_scripts\', \'run_js_script\');
编辑此处的相同问题:

add_action(\'wp_register_script\', \'register_script\');
把它挂在wp_enqueue_scripts 为简单起见:

add_action(\'wp_enqueue_scripts\', \'register_script\');
实际上,有两个更好的选择:

把你的wp_register_script() 呼叫相同的run_js_script 作为您的wp_enqueue_script() 呼叫wp_register_script() 完全调用,只需使用wp_enqueue_script().

结束

相关推荐

添加自定义JQuery菜单新鲜滑出

TBH这是我第一次添加自定义jquery 在a中wordpress 我已经读了大约10-20篇关于它的文章,我想还有更多的文章,但仍然缺少一些东西,所以我这样做了,请指出我做错了什么,我添加的jquery被发现了here.这是我想要的片段,<div class=\"container\"> <ul id=\"menu\"> <li> <a>