Archive for Code

WordPress jquery form plugin, json parsing and IE

I have just recently written a Web Shop plugin for some clients, but after upgrading one of their sites to WordPress 3.2.1 it made my checkout code flip backwards in ie7 and ie8.

After some debugging and googeling I found out that older versions of IE has some problems handling application/json mime type (This is the correct header content-type by the way). So instead of content-type application/json I had to change it to text/plain, pass the data as an array and then json_encode, this worked for IE9 but still not for IE7/8. So after a few hours of more debugging I came to realize that this thing works fine using WordPress 3.1.3, so I began to compare the differences. It turns out that the jquery form plugin I was using in WordPress 3.2.1 was not working so well, after copying over from 3.1.3 it was all good, even down to IE7.

So if you want to make IE happy, stay away from jquery form plugin 2.7.3 and use content-type: text/plain in your headers, below is an example of how I do this for the klarna gateway module to make it work:

// Ajax Form Options.
var options = {
	target: '#shop-ajax-response',
	beforeSubmit: validate,
	success: respond,
	url: shop_frontend_ajax.ajaxurl,
	data: {action: 'checkout_order'},
	dataType: 'json',
	cache: false
};

// Bind form using 'ajaxForm'.
jQuery("#dishop-checkout-form").ajaxForm(options);

$feedback .= '<p class="shop-message">'.$shop_gateway_msg.' | '.$klarna_result.'</p>';
if(defined('DOING_AJAX')) {
	$response = array('success' => false, 'modal' => true, 'feedback' => $feedback);
	header('Content-type: text/plain; charset=utf-8');
	header("Cache-Control: no-cache, must-revalidate");
	header("Expires: 0");
	echo json_encode($response);
	exit;
}

I know this is a half-way solution, I intend to notify about this problem so it can get fixed:)

wpshell & debugging wordpress

I came across this handy little shell tool yesterday while I was looking up a WordPress function… A couple of days earlier I was actually checking out a variety of debugging plugins for WordPress, the best one that I found at the time was the one I already had been using for a while, it’s called blackbox debug bar. While this plugin is really nice and more informative than the others, it doesn’t have the ability to limit debugging for admins:/

wpshell looks very promising and could be quite useful for debugging, if you are like me and feel more comfortable and in-control using the shell, you need to check it out: http://hitchhackerguide.com/2011/11/13/wpshell-a-shell-for-wordpress/. Easy to install and easy to use, It will be interesting to see how many different scenarios it could be used:)

Using custom fields to sort posts

Recently I’ve been working with a client that wanted me to create a way to sort recipes into different types of food and under different types of dishes. This had me thinking about custom fields where you have meta_key and meta_value for each key. The client also wanted to use this in other scenarios so basically i started thinking it should be sorted on categories as well. The function takes one argument which is the category slug, it will then loop through meta_keys belonging to that category, secondly the meta_values and third each posts matching that specific value.

It’s a whole new take on the custom field principle, since you basically start with the meta_key and not the post itself. Probably not the prettiest code in the world, but it does the job:) And it uses definition lists which is ideal here, below the function there’s a css example aswell.

To use this code, put it in your theme’s functions.php and fetch it in your desired template with

function cfMenu($catID) {
global $wpdb, $post;
$catID = get_cat_id('Mat');

$queryKey = "SELECT DISTINCT meta_key FROM $wpdb->postmeta
	LEFT JOIN $wpdb->term_relationships AS tr ON $wpdb->postmeta.post_id = tr.object_id
	LEFT JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
	WHERE SUBSTRING(meta_key,1,1) != '_' AND tt.term_id = '$catID'";

	$cfKeys = $wpdb->get_results($queryKey, OBJECT);

	echo '<div id="cfMenu">';
	foreach ($cfKeys as $cfKey) {
		echo '<dl>';
		echo '<h3 class="cfKey">'.$cfKey->meta_key.'</h3>';

		$queryValue = "SELECT DISTINCT meta_value FROM $wpdb->postmeta WHERE meta_key = '$cfKey->meta_key'";
		$cfValues = $wpdb->get_results($queryValue, OBJECT);

		foreach ($cfValues as $cfValue) {
			echo '<dt class="cfValue">'.$cfValue->meta_value.'</dt>';

			$cfPosts = get_posts('meta_value='.$cfValue->meta_value);
			foreach ($cfPosts as $cfPost) {
				$permalink = get_permalink($cfPost->ID);
				echo '<dd class="cfTitle">&raquo; <a href='.$permalink.'>'.$cfPost->post_title.'</a></dd>';
			}
		}
		echo '</dl>';
	}
	echo '</div>';
}

Here’s a CSS example to get you started

#cfMenu {
	float: left;
	display: inline;
}

#cfMenu h3 {
	margin: 0 0 0 0;
	padding: 0 0 0 0;
}

#cfMenu dl {
	float: left;
	width: 230px;
	margin-right: 10px;
	margin-bottom: 20px;
}

#cfMenu dt {
	font-weight: bold;
	margin: 5px 0 0 0;
	padding: 0 0 0 10px;
}

#cfMenu dd {
	margin: 0 0 2px 0;
	padding: 0 0 0 10px;
	font-size: 90%;
}

A Smarter Menu for WordPress

With the release of wordpress 3.0 this function is no longer needed, I’d like to thank everyone showing interest in it:) I’ve always thought it would be nice to combine pages and categories all inside one menu, and recently I was looking for a nice way to add subtitles as well. So I thought it would be cool to write my own menu function to suit my own needs. Read more

Jon Kristian is Stephen Fry proof thanks to caching by WP Super Cache