Warning: Parameter 1 to wp_default_scripts() expected to be a reference, value given in /home/forge/masseltech.com/wp-includes/plugin.php on line 600
Apr 24 2009

WordPress inline field validation for plugins

On a recent plugin, I’ve been looking for a way to have powerful form field validation, while being really user-friendly. Using AJAX to do run a PHP server-side check from a form (to ensure that somebody can’t have duplicates) is the best way I’ve found. This is basically the same as what you’ll find on the WordPress.org Codex, but shorter, and more specific for this application.


1. Add an action on the init() action to call the sack JS script (deals with AJAX in WP)

wp_enqueue_script ( ‘sack’ );

2. Create an instance of the sack object, and create variables to pass into the PHP side.

function doesCalendarExist(wpurl, confirmationFieldName, calendarNameFieldID, submitButtonID) {

var ajaxObject = new sack(wpurl + ‘/wp-admin/admin-ajax.php’);

ajaxObject.execute = 1;

ajaxObject.method = ‘POST’;

ajaxObject.setVar( “action”, “doesCalendarExist” );

ajaxObject.setVar( “confirmationField”, confirmationFieldName);

ajaxObject.setVar( “calendarName”, document.getElementById(calendarNameFieldID).value);

ajaxObject.setVar( “submitButton”, submitButtonID);

ajaxObject.onError = function(){ alert(‘Ajax error’)};


return true;


3. Create an input field that can call javascript functions to populate the data, and a span that will hold the error message

<input name=“calendarName” type=“text” id=“calendarName” onblur=”doesCalendarExist(<?php echo bloginfo(‘url‘); ?>’, ‘span1’, ‘calendarName’, ‘submitCalendarAdd’);/>

<span id=“span1″ class=”formError”></span>

4. Create a PHP function that will read the data passed from the ajaxObject. Die() statements will pass JavaScript back to the page. The dieString variable is super useful to be able to add lots of different DOM changes.

function wec_ajax_doesCalendarExist(){

$confirmationField = $_POST[‘confirmationField’];

$calendarName = $_POST[‘calendarName’];

$submitButton = $_POST[‘submitButton’];

$calendars = calendarDA::getAllcalendars();

$exists = false;

foreach($calendars as $calendar){

if(strcasecmp($calendar[‘calendarName’], $calendarName) == 0){

$exists = true;




$dieString = “document.getElementById(‘”. $confirmationField .“‘).innerHTML=’This calendar already exists!’;”;

$dieString .= “document.getElementById(‘”. $submitButton .“‘).disabled = true;”;




$dieString = “document.getElementById(‘”. $confirmationField .“‘).innerHTML=”;”;

$dieString .= “document.getElementById(‘”. $submitButton .“‘).disabled = false;”;




5. Add a WordPress Action to the init() that maps the sack action to the PHP function. you have to add “wp_ajax_” to the beginning of your action name from sack. the secondĀ argumentĀ is the name of your function that this action will call

add_action(‘wp_ajax_doesCalendarExist’, ‘wec_ajax_doesCalendarExist’ );

Apr 24 2009

Javascript example to do an auto-generated slug on a WordPress Plugin

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<script type="text/javascript">
function duplicateField(field1, field2){
var theLength = document.getElementById(field1).value.length - 1;
var theString = document.getElementById(field1).value;
for(var i=0; i < theString.length-1; i++){
if(theString.charAt(i) == " "){
theString = setCharAt(theString, i, "_");
document.getElementById(field2).style.color = "#c9c9c9";
document.getElementById(field2).value = theString;
function setCharAt(str,index,chr) {
if(index > str.length-1) return str;
return str.substr(0,index) + chr + str.substr(index+1);
function changeColorBack(fieldName){
document.getElementById(fieldName).style.color = "#000000";
<input id="field1" onkeyup="duplicateField('field1', 'field2');" type="text" />
<p><br />
<input id="field2" type="text" onfocus="changeColorBack('field2');" /><br />
<span id="span1"></span>
<input id="submitButton" type="submit" value="Submit" />