1

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.

7
  • The action is wp_enqueue_scripts, plural. also see the dependencies argument of the wp_enqueue_script function.
    – Milo
    Oct 12, 2013 at 19:18
  • @Milo: comments != answers ;) Oct 12, 2013 at 19:25
  • @toscho I'm a little confused how posting code that isn't working is considered "off-topic". Closing things like this will make me think twice about using this site again. Of course having specific code issues are going to local, but isn't that the point of this site? Getting help with code?
    – Blaine
    Oct 12, 2013 at 22:43
  • @Blaine Typos are off topic, not code. :)
    – fuxia
    Oct 12, 2013 at 22:44
  • @toscho, it wasn't a typo. I didn't realize that I there was a difference between the 2 methods. If you had read my entire post you would have seen the part where I said, "I'm very new to Wordpress...". I'll also add that I don't agree with the "typos are off topic". We all make mistakes and sometimes need a second set of eyes. We all miss ; at times or spell something wrong. I would think that this is exactly what the site is for: helping others.
    – Blaine
    Oct 12, 2013 at 22:49

1 Answer 1

2

You're using an invalid action:

add_action('wp_enqueue_script', 'run_js_script');

The correct action is wp_enqueue_scripts (plural), not wp_enqueue_script (singular). Use this instead:

add_action('wp_enqueue_scripts', 'run_js_script');

Edit

Same problem here:

add_action('wp_register_script', 'register_script');

Just hook it into wp_enqueue_scripts for simplicity:

add_action('wp_enqueue_scripts', 'register_script');

Actually, two better options:

  1. Put your wp_register_script() call in the same run_js_script callback as your wp_enqueue_script() call.
  2. Eliminate wp_register_script() call entirely, and just use the full parameters in wp_enqueue_script().
5
  • I made that change and it still isn't working. Is there anything else I'm doing wrong here (I'm sure there is... Lol..)
    – Blaine
    Oct 12, 2013 at 19:26
  • Check all of your add_action() calls to ensure the action is correct. See edited answer. Oct 12, 2013 at 19:30
  • I'm a little confused by the last part. You suggest adding the wp_register_script() in the same callback, but then say to completely eliminate it?
    – Blaine
    Oct 12, 2013 at 19:44
  • @Blaine, as I understand it, it's one or another.
    – brasofilo
    Oct 12, 2013 at 20:58
  • 1
    Yes: either/or. You can just pass all the parameters to wp_enqueue_script(), and eliminate the wp_register_script() call entirely. You only need to separate them in the (rare?) case that you need to register a script globally, but only enqueue it contextually. Oct 12, 2013 at 21:04

Not the answer you're looking for? Browse other questions tagged or ask your own question.