This is my first time using JQuery in a WordPress plugin, and I’m having some trouble.
Here is my “main” plugin document:
<?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);
}
?>
Here is my Javascript that simply displays an alert (scheduler.js):
jQuery(document).ready(function($){
alert("Hello World!");
});
I’m wondering why the alert isn’t showing up. I’ve also tried to add the wp_enqueue_script
to the HTML
file, which looks like this:
<?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>
I’m very new to WordPress and any direction or help would be appreciated. If you need any other code, please let me know, but that is most of it. The only other code I have is the views for the other links.
You’re using an invalid action:
The correct action is
wp_enqueue_scripts
(plural), notwp_enqueue_script
(singular). Use this instead:Edit
Same problem here:
Just hook it into
wp_enqueue_scripts
for simplicity:Actually, two better options:
wp_register_script()
call in the samerun_js_script
callback as yourwp_enqueue_script()
call.wp_register_script()
call entirely, and just use the full parameters inwp_enqueue_script()
.