BASH script for viewing unread GMail

By Glenn Ansley  |  February 22nd, 2011  |  Published in SysAdmin  |  0 Comments

I spend most of my day in BASH lately. When my phone buzzes I know I have new mail but I don’t want to go check it. Placing the following function in your .bash_profile allows you to quickly view unread email and summaries from terminal by typing ‘gmail’ and pressing enter. Thought I would share:

function gmail() {
    curl -u yourgamilusername --silent "https://mail.google.com/mail/feed/atom" | tr -d '\n' | awk -F '<entry>' '{for (i=2; i<=NF; i++) {print $i}}' | perl -pe 's/^<title>(.*)<\/title><summary>(.*)<\/summary>.*?<name>(.*?)<\/name>.*$/\n$3\n\t$1\n\t$2/'
}

Raleigh WPHackathon Photos

By Glenn Ansley  |  August 25th, 2010  |  Published in News  |  0 Comments

Photos from last night’s Hackathon

I’ll be speaking on Custom Post Types and Custom Taxonomies October 5 at the Raleigh WordPress Meetup. Sign up here.

Quickly Add a Single 301 Permanent Redirect to WordPress

By Glenn Ansley  |  June 22nd, 2010  |  Published in WordPress  |  1 Comment

Alan Knox, the developer of our FacePress plugin that automatically publishes WordPress posts to Facebook recently left FullThrottle to start his own Web Design company. We’re really excited about where he is going and about the sites that he already has in development.

When he left, we mutually agreed to send his FacePress plugin with him (its his baby and he’s best qualified to support and enhance it). In doing so, we needed to create a permanent 301 redirect for the support page but we didn’t want to install a plugin that included an admin panel, etc. Instead, we elected to put this short code in our theme’s functions.php file.

Please feel free to take and modify as needed. Only one condition: if you have a way to improve this, please share in the comments!

// Permanantly Redirect FacePress Plugin
function permanent_301_redirect_facepress() {
	if ( isset( $_SERVER['REQUEST_URI'] ) && '/facepress-ii' == $_SERVER['REQUEST_URI'] || '/facepress' == $_SERVER['REQUEST_URI'] ) {
		header( 'HTTP/1.1 301 Moved Permanently' );
		wp_redirect( 'http://knoxwebdev.com/facepress-ii' );
		exit();
	}
}
add_action( 'template_redirect', 'permanent_301_redirect_facepress' );

Test it by going to http://fullthrottledevelopment.com/facepress-ii!

Global 404 for WPMU or WP Multi Sites

By Glenn Ansley  |  June 22nd, 2010  |  Published in WordPress  |  0 Comments

I quickly drafted the following code in response to this tweet:

I really really really really want a MU universal 404 file.less than a minute ago via TweetDeck

function global_404() {
	if ( is_404() ){
		wp_redirect( 'http://site.com/global404' );
		die();
	}
}
add_action( 'template_redirect', 'global_404' );

If placed inside a plugin that is activated on every site in your network, the above code will do the following:

  1. Determine that the user looked for a page that didn’t exist.
  2. Redirect to a URL of your choice, regardless of the site that they are currently viewing.

Remove Tool Tips (Titles) from Category and Page lists

By Glenn Ansley  |  June 15th, 2010  |  Published in WordPress  |  2 Comments

The following code, placed in your WordPress theme’s functions.php file will strip the title attribute from wp_list_categories, wp_list_pages. The custom menu widgets and code don’t seem to output titles.

This request was made by a client. I have no idea how many people are interested in it, but here it is:

function ft_rtt_remove_title_attribute( $output ){
$output = preg_replace('/title=\"(.*?)\"/','',$output);
return $output;
}
add_filter('wp_list_categories','ft_rtt_remove_title_attribute');
add_filter('wp_list_pages','ft_rtt_remove_title_attribute');

Contributing to WordPress Makes You a Better Developer

By Glenn Ansley  |  May 24th, 2010  |  Published in WordPress  |  9 Comments

trac-logo I have been a passive member of the WordPress developer community for about two years now. Understandably, my familiarity with WordPress and my efficiently in developing for that platform have both increased with time.

My first real tipping point in efficient development came with a better understanding of how the wp-content folder interacts with the rest of the core files. This was ground breaking for me and it almost immediately ‘powered up’ my WordPress related development.

Over the past several months I have been experiencing a second surge in both my level of understanding and my efficiency in WordPress development. This is directly related to my involvement in the WordPress 3.0 development cycle. I wanted to quickly share four aspects of this process that have helped me increase efficiency in my overall WordPress development. It’s my hope that this encourages you to jump in and contribute as well.

Familiarity with new tools and technologies
WordPress.org manages their code base in a web application named Trac. Trac is an opensource software that integrates SVN and ticket based bug reporting. You can view the WordPress installation for the core files here: http://core.trac.wordpress.org. Although I had a basic understanding of what Trac was and knew where it existed online, my interactions with it were in the single digit range (and less than 1).

Trac
It is impossible to seriously contribute to WordPress core as a developer without diving into Trac. It was intimidating for me to do so at first, but has proven well worth the learning curve it took to become comfortable with the process of creating tickets, creating patches, and submitting them for review and possible inclusion into WordPress.

  • Taking the time to understand how the WordPress community uses Trac in our development process has increased my efficiency in the following ways:
  • Comments in the individual tickets help me understand why specific changes were made in the code.
  • Milestones help me to better judge what features and bug fixes will be included in future releases.
  • The ability to browse existing tickets and submitted patches help me to determine legitimate bugs and discover possible solutions.
  • The ability to navigate revisions in an intelligent GUI using the power of Annotate helps me track down unresolved bugs more quickly.

SVN
Although I was already familiar with SVN due to the plugin repository and other non-WordPress related projects, contributing to WP core has substationaly increased the frequency at which I find myself using it. Increased use inevitably leads to increased knowledge and efficiency with any tool.

A better awareness of WordPress community
Possibly the greatest benefit I have gleaned from my involvement in contributing to WordPress is a greater awareness of who the WordPress community includes. Prior to contributing to comments and patches on Trac and finding a suitable IRC client, I didn’t know who Andrew Nacin, Ptah Dunbar, Austin Matzko even existed… let alone understand their contributions to WordPress. These are only a few names that immediately come to my mind… several more exist.

Its funny how you think you know ‘who’ a community is because you’re following a couple mailing lists or a couple of opinionated talkers on Twitter. Getting plugged into the development process has opened me up to a whole new world of very intelligent individuals that I continue to learn from by listening in on their conversations. My coding has become more efficient due to the little tidbits of information I skim off of their public discussions every day.

Familiarity with More of the Core Functionality
Troubleshooting your own code inevitably leads you to a greater understanding of the code surrounding it and supporting it… troubleshooting somebody else’s code multiplies this effect several times over. I’m pretty familiar with the WordPress core files, but diving into Trac and hunting down bugs that can only be reproduced by a remote set of users have taken me to places in the codebase that I never would have found otherwise. I can’t tell you the number of times I’ve been hunting down a specific bug and ran across an amazing function that I was previously unaware existed. Finding all these hidden jewels and obtaining a better understanding of how different functions work within WordPress has largely increased my development on a day to day basis.

Coding Standards
I’m pretty anal about the way my code looks. I try to document as much as possible while I develop it. Additionally, all my code is formatted the same way to increase comprehension if I have to dive back into a file months or years after initially creating it. With this being said, I still have several habits that are less than productive in the long run… my guess is that you’re just as lazy.

WordPress has a guide sheet dictating what coding standards should be followed when contributing to the core software. Many of these standards are nothing more than the lead developers collaborative opinions. The standards are not ‘right’ or ‘wrong’ in any global or authoritative manner… rather, they’re simply the way that WordPress has determined to standardize its code base.

This has been helpful to me because it has encouraged me to clean up some less than standard areas in my own code. Once you work on a couple large patches it becomes natural to start applying those standards to your own work. Again, I’m not suggesting that you have to accept these standards as rule in your personal development… but I am reporting that once I got past the basic control issues I had with not doing things the way I previously did them, adopting many of the WordPress coding standards has actually increased my overall efficiency… a result that I am always happy to discover.

Conclusion and Resources
When I first ventured into the world of WordPress Trac it was with a very utilitarian purpose… I had a problem and I needed it fixed. Two months later, I’m still there and its extremely addictive. Contributing to WordPress core has introduced me to new technologies, new friends, and it continues to increase my efficiency in day to day development. If you’re a WordPress developer and you’ve yet to jump into core contribution, I would highly encourage you to start today. Here are some resources to get you started:

Embed an RSS/ATOM Feed into your WordPress Website

By Lew Ayotte  |  April 26th, 2010  |  Published in Development, WordPress  |  0 Comments

One of our clients needs to pull an RSS feed from a real estate virtual tour provider and display it on one of their pages. I looked into a few plugins and could not find one that was easily customizable. After a quick google search I discovered that WordPress has some built-in functions based off of SimplePie‘s easy to use RSS API.

We also needed to customize the output a little bit, which required me to rely on PHP’s preg_match and preg_replace functions. Here is some simple example for embedding a feed into your WordPess website:

<?php
include_once(ABSPATH.WPINC.'/rss.php'); // path to include script
$feed = fetch_feed('http://url.to.feed/'); // specify feed url
?>

<?php
if (!is_wp_error($feed)) :
	$rss_items = $feed->get_items();
?>
    // Loop through each feed item and display each item as a hyperlink. <?php foreach ( $rss_items as $item ) : ?>
  • <?php echo $item->get_title(); ?>
  • <?php endforeach; ?>

Check out the codex for more information about WordPress’ function feed_fetch()… and with a little extra coding you can format the feed any way you like, see our example at NC Capital Homes.

Saving ‘meta data’ for WordPress categories

By Glenn Ansley  |  February 7th, 2010  |  Published in Development, WordPress  |  14 Comments

[updated 2010-06-08]

WordPress has some great hooks for adding extra fields to the Add Category page (as well as the edit category and the table listing the categories). Where / how do you save that data though. I ran accross a couple other devs on Twitter this evening that were asking this question. This post is primarily for them and it doesn’t provide all the code you need to get finish, but it should get most dev on their way. For instance, you will need another action for adding to the edit page, another action for adding to the table, and another action for editing the data. You will need to replace the type of fields. The below code assumes a custom taxonomy called ‘manufacturers’.

Additionally, the code below saves every field for every category or taxonomy in a different line in the options table. If I were going to put this into production I’d serialize the data so that all fields for a single category were stored in a single row of the options table.

Finally, as this is not in production yet (though it has been tested) there may be some errors or bugs. Please let me know if you find any and I will update (code would be nice too).

This following code, placed in your theme’s functions.php file:

<?php
### CUSTOM FIELDS FOR MANUFACTURERS

	// Manufacturer fields
	function ft_add_manufacturer_fields(){
		?>
		<!-- Web Site -->
		<div class="form-field">
			<label for="man_web_site"><?php _e('Manufacturer Web Site') ?></label>
			<input type='text' name="man_web_site" id="man_web_site" />
			<p><?php _e('The manufacturer\'s web site.'); ?></p>
		</div>

		<!-- Logo -->
		<div class="form-field">
			<label for="man_logo_url"><?php _e('Manufacturer Logo URL') ?></label>
			<input type='text' name="man_logo_url" id="man_logo_url" />
			<p><?php _e('The web address of the manufacturer\'s logo.'); ?></p>
		</div>

		<!-- Status -->
		<div class="form-field">
			<label for="man_status"><?php _e('Manufacturer Status') ?></label>
			<select name="man_status" id="man_status"><option value='standard' selected="selected">Standard</option><option value='premium'>Premium</option></select>
			<p><?php _e('Only manufacturer\'s with a premium status will have their logos linked to their web site.'); ?></p>
		</div>

		<?php
	}
	add_action('manufacturers_add_form_fields','ft_add_manufacturer_fields');

	// Add field to edit category screen
	function ft_edit_manufacturers_form(){	

		// clear values.
		$man_web_site = $man_logo_url = $man_status = '';

		// Clean tag id
		$id = sanitize_text_field($_GET['tag_ID']);

		if ( $opt_array = get_option('man_'.$id.'_meta') ){
			$man_web_site 	= sanitize_text_field($opt_array['man_web_site']);
			$man_logo_url 	= sanitize_text_field($opt_array['man_logo_url']);
			$man_status 	= sanitize_text_field($opt_array['man_status']);
		}
		?>
		<!-- Web Site -->
		<tr class="form-field">
			<th scope="row" valign="top"><label for="man_web_site"><?php _e('Manufacturer Web Site') ?></label></th>
			<td><input type='text' name="man_web_site" id="man_web_site" value="<?php echo $man_web_site; ?>" /><br />
			<span class="man_web_site"><?php _e('The manufacturer\'s web site.'); ?></span></td>
		</tr>

		<!-- Logo -->
		<tr class="form-field">
			<th scope="row" valign="top"><label for="man_logo_url"><?php _e('Manufacturer Logo URL') ?></label></th>
			<td><input type='text' name="man_logo_url" id="man_logo_url" value="<?php echo $man_logo_url; ?>" /><br />
			<span class="man_logo_url"><?php _e('The URL for the manufacturer\'s logo.'); ?></span></td>
		</tr>

		<!-- Status -->
		<tr class="form-field">
			<th scope="row" valign="top"><label for="man_status"><?php _e('Manufacturer Status') ?></label></th>
			<td><select name="man_status" id="man_status"><option value='standard' selected="selected">Standard</option><option value='premium' <?php if ( $man_status == 'premium' ){ echo 'selected="selected" '; } ?>>Premium</option></select>
			<br /><span class="man_status"><?php _e('Only manufacturer\'s with a premium status will have their logos linked to their web site.'); ?></span></td>
		</tr>

		<?php
	}
	add_action('manufacturers_edit_form_fields','ft_edit_manufacturers_form');

	// Add Manufacturer fields to table head
	function ft_manage_manufacturers_columns($columns){
		if ( !isset($_GET['taxonomy']) || $_GET['taxonomy'] != 'manufacturers' )
			return $columns;

		if ( $posts = $columns['posts'] ){ unset($columns['posts']); }
		$columns['man_status'] = 'Status';
		if ( isset($posts) ){ $columns['posts'] = 'Machines'; }
		return $columns;
	}
	add_filter('manage_edit-tags_columns','ft_manage_manufacturers_columns');

	// Add fields to table for each manufacturer
	function ft_manage_manufacturers_custom_fields($deprecated,$column_name,$term_id){
		if ( $opt_array = get_option('man_'.$term_id.'_meta') ){
			return esc_html($opt_array[$column_name]);
		}
	}
	add_filter('manage_manufacturers_custom_column','ft_manage_manufacturers_custom_fields',10,3);

	// Insert or Update the Manufacturer
	function ft_create_update_manufacturer($term_id,$tt_id){
		// Grab your field names in $_POST, sanatize and put wherever you want
		if ( isset($_POST['man_web_site']) )
			$update['man_web_site'] = sanitize_text_field($_POST['man_web_site']);
		if ( isset($_POST['man_logo_url']) )
			$update['man_logo_url'] = sanitize_text_field($_POST['man_logo_url']);
		if ( isset($_POST['man_status']) )
			$update['man_status'] = sanitize_text_field($_POST['man_status']);

		if ( isset($update) )
			update_option( 'man_'.$term_id.'_meta' , $update );

	}
	add_action('created_manufacturers','ft_create_update_manufacturer',10,2);
	add_action('edit_manufacturers','ft_create_update_manufacturer',10,2);

	// On Delete Manufacturer
	function ft_delete_manufacturer($term_id,$tt_id){
		delete_option('man_'.$term_id.'_meta');
	}
	add_action('delete_manufacturers','ft_delete_manufacturer',10,2);
>

Download a text file with the above code: here

How to Upgrade WordPress to 2.9

By Glenn Ansley  |  December 9th, 2009  |  Published in Blogging, News, WordPress  |  0 Comments

WordPress is a great piece of software driving billions of sites across the internet. One of the main reason WordPress does so well is because the community gives back freely and often. While these community contributions to the project mean that WordPress is continually enhancing its feature set, it also means that a web site powered by WordPress is continually in need of upgrades.

Being the excellent software that it is, WordPress gives you the ability to ‘Auto Upgrade’ to the latest version. If you’re running an unmodified installation and you don’t have too many plugins, this is an excellent option and I would highly suggest you give it a try.

If you’re going to upgrade WordPress to 2.9 on your own, we want to provide you with the following suggestions and resources.

  1. Always backup your database and your file structure (especially the wp-content folder) before upgrading. You can never be too safe. This goes for manual upgrades as well as auto upgrades!
  2. Always deactivate plugins prior to upgrading. If you have a lot of plugins and your site gets a lot of traffic, this may be tricky. It’s not ‘necessary’ but its safe practice because if one of your plugins doesn’t mesh with the new version of WordPress it may render your site inaccessible.
  3. We would suggest checking with all your plugins’ authors to see if their plugin is compatible with the latest release of WordPress prior to upgrading.
  4. Most hosts give priority to index.html over index.php. We often use this tactic to put up a nice “Site down for scheduled maintenance” message during the upgrade. This is obviously optional.
  5. You can find a detailed step-by-step instructions for manually upgrading WordPress on their website: http://codex.wordpress.org/Upgrading_WordPress_Extended

Finally – and lets be honest, the real reason we wrote this post – if you have had trouble with WordPress upgrades in the past or are a little bit hesitant to do it yourself, check out http://wpupgrade.com. It’s a site that FullThrottle just launched with the sole purpose of helping others upgrade their WordPress installations.

Help Preventing WordPress Brute Force Attacks

By Glenn Ansley  |  November 30th, 2009  |  Published in News, SysAdmin, WordPress  |  1 Comment

It has recently been reported that WordPress blogs are being attacked by a Brute-Force campaign.

A Brute-Force attack happens when a malicious individual creates an automated script with the sole purpose of guessing your administrative password. It does this by pointing the script at your blog’s login URL: http://yourblogsdomain.com/wp-login.php. The script repeatedly guesses at your login name and password from a dictionary of commonly used usernames and passwords.

Here is an explanation from the post linked above:

The wp_brute_attempt() function takes 3 parameters, $ch which is cURL’s structure (cURL is a command line tools that can be used to perform HTTP requests). The other two parameters define the site and the password that will be tried. If the script logged in successfully, the page that gets returned by the server will contain the phrase “Log Out”, and the function will return a true value.

If you are currently running a WordPress blog and would like to secure your site against these attacks, the easiest thing to do is to simply change your admin username and to make sure you have a strong password in place.

If you would like assistance securing your site against this attack, FullThrottle is available to help. Simply contact us for more details.