Skill Tag: WordPress
PHP Check for an AJAX call
I just learned that it’s possible to check in php if a request has been made through an ajax call which is handy when you want to return a different template depending on if it’s ajax or not. To do it you just add this snippet:
function is_ajax(){
return (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'));
}
single_cat_title()
When trying to get a category link inside the loop I used to nest a few WordPress functions like this:
<?=get_category_link(get_cat_ID(single_cat_title()));?>
But today it wasn’t working, I discovered after experimenting that this method doesn’t work when you have an apostrophe on your category name and that single_cat_title(); should only be used for actually printing the category. To get the category link you can use this instead:
<?=get_category_link(get_query_var('cat'));?>
CodeIgniter Loops
I discovered a nice method of looping out data in CodeIgniter. I set up a loops module and created a load of view files to handle different loop styles. That way I can print them out anywhere in the site by adding:
<?php $this->load->view('loops/loopname'); ?>
Makes it work a bit like WordPress’ get_template_part() function.
get_template_part
Is an extremely easy way of including multiple loop styles in your WordPress themes. Take a look.
Get Post Thumbnail URL in WordPress
Sometimes you don’t just want to print out the post thumbnail. Here’s a quick snippet to grab its URL:
<?php $thumb = wp_get_attachment_image_src(get_post_thumbnail_id(),'large'); echo $thumb = $thumb[0]; ?>
Slow site
Also just discovered why my site was running slow. Having just the post name as the permalink was slowing it all down as WP has to do a load of database queries. Fixed it by adding the post ID before the name in the permalinks.
WordPress 3.2 is out.
Remember to check in the Screen Options tab for new features. They like to hide stuff in there.
Tweets as comments
I just discovered that Disqus has a thing the imports tweets into you blog posts as comments. If you don’t want Disqus though you can do something similar with this plugin.
PHP Image Uploads on IIS
After 2 hours of looking I discovered why IIS wasn’t letting me upload images to my WordPress. It has to do with the permissions on the php temp directory. More information here.
jQuery Mobile
Check out jQuery Mobile. I think I’m gonna see if I can add this to my next WordPress theme.