Web Development

When making cross-domain ajax calls to an api, your api server must return a status code of 200 in order to see your custom reponse object. This is a hack since browsers don’t handle CORS errors (status codes other than 2XX). So, remember to do that and just embed a http_status_code property on your repsonse if your client/ javascript application needs that. I include it just because it is a nice to have. This means that in your js code for ajax calls, everything will get routed to the success callback function (since it is always a 2XX response); no error callback is needed.

Also, when developing locally using a self-signed certificate on your api server, Google Chrome seems to not accept the preflight Options after a few minutes. As in, if you do a PUT or DELETE, it will work initially, but after a few minutes, the OPTIONS request hangs at the “(pending)” stage.

I figured out this was the case after searching around and found http://stackoverflow.com/questions/14492686/cors-request-fails-in-chrome-only-if-has-headers, which led to this bug: https://code.google.com/p/chromium/issues/detail?id=96007.

The way around this is to start Chrome with web security disabled: google-chrome –disable-web-security (be sure to shut down ALL currently-used Chrome instances, and then issue the command. Otherwise, Chrome will start like normal and ignore the disable-web-security flag). This should only be done while testing and debugging your app locally on your VM. When it comes time for production, the fact that you have a valid certificate for your https api server, should make this a moot point.

Interesting to note that Firefox does not have a problem with CORS and self-signed certs.

Read more

I ran into the “weird characters” problem when I used PHP (specifically the SimplePie script) to parse an rss feed and output the results to a page.

These characters can be apostrophes and quotes among others.

If you need to fix the problem, it’s pretty easy to do. Change the charset at the top of your page to match the output of the rss parser results. Or vice versa and it should work. To see more of an explanation and how to do that visit SimplePie’s docs regarding the matter.

In my case, I set the encoding of my results page to UTF-8, (which is what SimplePie uses) and that fixed it. Now the weird characters are gone and I’m left with regular looking apostrophes.

Read more

I recently had the need to perform some ajax functions on the backend of Texas Tech Today and wasn’t sure exactly how to go about it. So after a search, I found this great explanation: Simplified AJAX For WordPress Plugin Developers using Jquery. But it stops short of explaining how to receive some data from a POST, give it to PHP to do something with, and then return a result back to the form.

So, I thought I would detail how to do that in WordPress in a plugin.

The plugin just has a basic options page (Settings -> Ajax4WP) and lets you save an option to the database via ajax. Go ahead and download and install it; you’ll see how simple it is.

1. Create a php file that will have our javascript code, and save it into your plugins folder.

The reason we need our javascript code to be in a PHP file is to get the “get_option(‘siteurl’)” value dynamically, since we’ll need that to reference our PHP file with the ajax function.

js-Ajax4WP.php (download the plugin files)

$site_url = get_option('siteurl');
jQuery(document).ready(function() {

   //when the link with the id of save_settings is clicked, get the value of the text field and start doing the ajax stuff
   jQuery('#save_settings').click(function() {

      var Ajax4WP_test_setting = jQuery("#Ajax4WP_test_setting").val();

      //action is the name of the php function in your main plugin file
      jQuery.post("<?php echo $site_url; ?>/wp-admin/admin-ajax.php", {action:"Ajax4WP_save_settings", 'cookie': encodeURIComponent(document.cookie), Ajax4WP_test_setting:Ajax4WP_test_setting},
         var message_result = eval('(' + res + ')');
         if (!message_result.success) {
            jQuery("#Ajax4WP_test_setting").css("border","2px solid #cc0000");
         alert(message_result.message + ' ' + message_result.setting);

      return false;


This is jQuery ajax code that takes the value of a text field and passes it to Ajax4WP_save_settings() in our plugin file. We use jQuery.post to define a post variable (Ajax4WP_test_setting) that PHP can use.

Once the plugin code does its thing on the server (step 2 below), it returns the result back to this function at “function(res)”. Then we eval the json result and use an alert to display a message.

2. Create your standard plugin file and put in the Ajax4WP_save_settings function.

Ajax4WP.php (download the plugin files)


// our ajax action:
add_action('wp_ajax_Ajax4WP_save_settings', array(&$this, 'Ajax4WP_save_settings'), 10);

function Ajax4WP_save_settings()
		   $name = 'Ajax4WP_test_setting';
			$value = $_POST['Ajax4WP_test_setting'];

			if ($value == "") {
				$message_result = array(
					'setting' => "",
					'message' => 'Please enter a Setting Value!',
					'success' => FALSE
			else {
			   update_option($name, $value);
				$message_result = array(
					'setting' => $value,
					'message' => 'Saved! You entered:',
				   'success' => TRUE
			echo json_encode($message_result);

This is standard PHP to process a POST request. When we’re through, we echo the json_encode() result. NOTE: json_encode is a PHP >= 5.2 only function, so be sure you are up to date on your PHP install.

3. Make a php file that will contain basic html and will get included on our settings page

Ajax4WP-settings.php (download the plugin files)

<div id="Ajax4WP Settings" class="inside" style="margin-left: 29px; width: 200px;">

   <h3>Ajax4WP Settings</h3>

   <div id="loading_message"></div>

   <label for="Ajax4WP_test_setting">Setting Value:</label>
   <br />
   <input type="text" id="Ajax4WP_test_setting" name="Ajax4WP_test_setting" value="<?php if (isset($this->Ajax4WP_test_setting)) { echo $this->Ajax4WP_test_setting; } ?>" size="20" />

   <p><a href="#" id="save_settings" class="button button-highlighted">Save Settings</a></p>


This is just a “stub” and gets included in our plugin. That way we don’t have a lot of html code mucking up the main plugin file. You can see what’s going on better in the source code when you download the files.

I hope that helps you out if you are wondering how to use jQuery and ajax in your WordPress plugins. Please leave a comment if you have any questions. Thanks!

Read more

We just launched my favorite project I’ve worked on so far, for obvious reasons for any of you that know me 🙂

This new site is http://www.passorcatch2008.com and it’s a showcase for two of Texas Tech’s top players: quarterback Graham Harrell and wide receiver Michael Crabtree. If we can keep winning and finish off a great season, both have a shot at the Heisman at the end of the year.

The site features a great design developed by our athletics department. They also shot some fun videos and gave us some nice photos to put up in the gallery.

My job was to take the design and mock it up in photoshop. I then did the html and css conversion. I also added the photo gallery using the jQuery-lightbox plugin.

I received the videos in .avi format so I converted them to .flv format and used the JW FLV Player to play them in flash.

The excellent content work and design was done by our web writer, Lisa Low.

It was a fun site and I’m very happy with the way it turned out. I hope Texas Tech fans everywhere enjoy it and spread the word.


Read more

Texas Tech Today Screenshot

Whew! A few hard months of planning and preparation have come to a culmination with the launch of the new Texas Tech Today. TTT is our news and information site that holds all of our news releases, clips, stories, and videos. It all runs on WordPress with the help of some custom plugins and templates. This isn’t a typical blog site, and the extendability of WordPress allowed me to do some really cool things and use it as a CMS that will fit our needs very well. This is a credit to the developers of wp, and the fact that they have made it such a nice platform to work with.

This new version will make the posting of stories faster and more efficient. That way, our content developers can focus on producing more and more quality content that our audience will find valuable and want to share.

The old way of posting stories to our site was through dreamweaver and was completely file based, with no help from a cms of any sort. This worked ok for a while, but we realized there had to be a better way to manage our content. Since our stories are somewhat “templatable”, meaning they can have a standard sidebar and other options, I set out to find a way to make it dead easy to post a story without using dreamweaver. Enter wordpress and the ability to code plugins that do pretty much whatever you can think of, and we were set.

We used to have our stories and videos on one server and clips and news releases on another. The news releases and clips were pushed out with a custom mini-cms I built in CodeIgniter. The new way allows us to have everything in one place and the built-in rss feeds for every category make it easy to send out our feeds for each type of post.

Moving to WordPress will hopefully get us a little better search engine placement and maybe some chatter in the blogosphere. Wp-stats is amazing, but we’ve also got google analytics running so we can have a comprehensive view of our visitors and their interests in our content to serve them better.

My hope is that this can be a showcase of the power that wordpress has and that we can lead the way for other universities to use this wonderful software.

Read more