Creating a Tumblog with WordPress

1000126_60213914-w590
Posted in Coding

For the recent (first) design of my personal site I added in some Tumblr style functionality. In this post we’ll look at how WordPress can be used to create a Tumblog.

We’ll be using a function that was added in 2.7 that doesn’t get used as much as it should: the post_class function. It adds a CSS class to each category, tag etc. we’re interested in the category bit. First thing to do is to create all the different categories you want — that’ll probably be all the standard Tumblr categories: movie, picture, article, quote and link. You’ll then need to add to within your loop the post_class functionality:

<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

This now gives each post a class of category-name (ie category-link). This means we can then start styling! For my blog I just added a different icon to each post type (not strictly true—read on!), bit you could go further and do something radical like… change the background colour! Here’s the CSS I used:

.category-link {
	background:  url(/media/icons/link.png) no-repeat;
}
.category-quote {
	background:  url(/media/icons/quote.png) no-repeat;
}

.category-article {
	background:  url(/media/icons/article.png) no-repeat;

}
.category-movie {
	background:  url(/media/icons/video.png) no-repeat;
}
.category-photo{
	background:  url(/media/icons/image.png) no-repeat;
}

That meant each category had a different icon. Of course, I then used a sprite to combine them into a single image. I used the SpriteMe bookmarklet to generate the image and CSS I needed, giving me:

.category-link .icon {
	background-image: url(/media/icons/icon-sprite.png);
	background-position: -10px -142.5px;
}
.category-quote .icon {
	background-image: url(/media/icons/icon-sprite.png);
	background-position: -10px -4.5px;
}

.category-article .icon {
	background-image: url(/media/icons/icon-sprite.png);
background-position: -10px -211.5px;
}

.category-movie .icon {
	background-image: url(/media/icons/icon-sprite.png);
	background-position: -10px -211.5px;
}
.category-photo  .icon {
	background-image: url(/media/icons/icon-sprite.png);
	background-position: -10px -73.5px;
}

That by itself won’t display the icon – you need to add in the icon class:

<div class="icon"><a href="/">Article</a></div>

And hide the text we’ve just added:

.icon {
	width: 60px;
	height: 60px;
	text-indent: 200px;
	overflow: hidden;
}

That’s actually all you need to do to create “Tumblr style” different icons for each category. Quite simple.

Further development

It’s all very well having a different icon, but does that constitute a Tumblog? Not sure it does. Let’s say for the quote post type you want to change the blockquote styling—for normal posts you’ve got:

blockquote {
	font: italic 30px Georgia;
	border-left:10px solid #743399;
	}

Bit you want something that stands out more. Easy. Use post_class again so you’ll need to target the class category-quote:

.category-quote blockquote{
	font: 30px Georgia;
	text-indent:50px;
        background:#eee;
	border:none;
}

QuickPressing

The whole point of Tumblogs are they’re quick and easy to post to. WordPress’ QuickPress Dashboard widget comes to mind. Trouble is for our nice little icons to work, we’ll need to be able to set the category. Luckily, the WordPress support forum has the answer.

Install the following code as a plugin:

<?php
/*
Plugin Name: QuickPress Category
Plugin URI: http://www.t31os.co.cc/
Description: Adds category selection to the QuickPress widget shown in the WordPress dashboard
Author: t31os
Version: 1.0
Author URI: http://www.t31os.co.cc/
*/

class quickpress_category {
	
	public function quickpress_category() {
		add_action( 'wp_dashboard_setup' , array( $this , 'dashboard_updater' ) );
		add_action( 'admin_init', array( $this , 'register_option' ) );
	}
	
	public function register_option() {
		// Register the setting - with basic validation, only need be 0/1 or true/false , keep it simple
		register_setting( 'enable-dropdown', 'quickpress_category' , array( $this , 'option_validation') );
		// Register the setting field
		add_settings_field( 'quickpress_category', 'Quickpress category', array( $this , 'add_category_option' ), 'writing', 'default' );
	}
	public function option_validation( $input ) {
		if( !$input ) 
			return 0;
		if( $input == 1 )
			return 1;
		else
			return 0;
	}
	// Output the checkbox for toggling the category dropdown
	public function add_category_option() {
		$checked = 
			( get_option('quickpress_category') && get_option('quickpress_category') == 1 ) ? 'checked="checked" ' : ''; 
		?>
			<div class="input-text-wrap">
				<label class="selectit">
					<input type="checkbox" name="quickpress_category" <?php echo $checked; ?>value="1" />
					Enable quickpress category selection.
				</label>
			</div>
		<?php
		settings_fields('enable-dropdown');
		return;
	}
	
	public function dashboard_updater() {
		// Global the variable so it's available inside the function (that's how you access variables outside a functions scope)
		global $wp_meta_boxes;
		
		// Determine where the widget is and update the callback name if the option is enabled
		switch( true ) {
			
			case ( !get_option( 'quickpress_category' ) || get_option( 'quickpress_category' ) == false ) :
				return;
			
			case ( isset( $wp_meta_boxes['dashboard']['side']['core']['dashboard_quick_press'] ) ) :
				$wp_meta_boxes['dashboard']['side']['core']['dashboard_quick_press']['callback'] = array( &$this , 'quickpress_widget' );
				return;
			
			case ( isset( $wp_meta_boxes['dashboard']['normal']['core']['dashboard_quick_press'] ) ):
				$wp_meta_boxes['dashboard']['normal']['core']['dashboard_quick_press']['callback'] = array( &$this , 'quickpress_widget' );
				return;
		}
		return;
	}
	// Most of this is just code from the original widget
	public function quickpress_widget() {
		// Call $current_user, so there's no reliance on $GLOBAL, per original widget
		global $current_user;
		
		$drafts = false;
		if( 
			'post' === strtolower( $_SERVER['REQUEST_METHOD'] ) && isset( $_POST['action'] ) && 
			0 === strpos( $_POST['action'], 'post-quickpress' ) && (int) $_POST['post_ID'] ) {
			
			$view = get_permalink( $_POST['post_ID'] );
			$edit = esc_url( get_edit_post_link( $_POST['post_ID'] ) );
			
			if ( 'post-quickpress-publish' == $_POST['action'] ) {
				if ( current_user_can('publish_posts') )
					printf( '<div class="message"><p>' . __( 'Post Published. <a href="%s">View post</a> | <a href="%s">Edit post</a>' ) . '</p></div>', esc_url( $view ), $edit );
				else
					printf( '<div class="message"><p>' . __( 'Post submitted. <a href="%s">Preview post</a> | <a href="%s">Edit post</a>' ) . '</p></div>', esc_url( add_query_arg( 'preview', 1, $view ) ), $edit );
			} 
			else {
				printf( '<div class="message"><p>' . __( 'Draft Saved. <a href="%s">Preview post</a> | <a href="%s">Edit post</a>' ) . '</p></div>', esc_url( add_query_arg( 'preview', 1, $view ) ), $edit );
					
				$drafts_query = new WP_Query( array(
					'post_type' => 'post',
					'post_status' => 'draft',
					'author' => $current_user->data->ID,
					'posts_per_page' => 1,
					'orderby' => 'modified',
					'order' => 'DESC'
				) );

				if ( $drafts_query->posts )
					$drafts =& $drafts_query->posts;
			}
			printf('<p class="textright">' . __('You can also try %s, easy blogging from anywhere on the Web.') . '</p>', '<a href="tools.php">' . __('Press This') . '</a>' );
			$_REQUEST = array(); // hack for get_default_post_to_edit()
		}
		$post = get_default_post_to_edit();
		
		// Args for dropdown
		$args = array(
			'order' => 'ASC',
			'hierarchical' => 1,
			'echo' => 0,
			'name' => 'post_category[]',
			'hide_empty' => 0
		);
		// Create dropdown
		$categories = wp_dropdown_categories( array_merge( $args , array( 'selected' => get_option('default_category') ) ) );
		// Replace the ID
		$categories = str_replace( "id='post_category[]'" , "id='post_category'" , $categories );
		
		?>
		
		<form name="post" action="<?php echo esc_url( admin_url( 'post.php' ) ); ?>" method="post" id="quick-press">
			<h4 id="quick-post-title"><label for="title">Title</label></h4>
			<div class="input-text-wrap">
				<input type="text" name="post_title" id="title" tabindex="1" autocomplete="off" value="<?php echo esc_attr( $post->post_title ); ?>" />
			</div>
			<h4 id="quick-post-category"><label for="title">Category</label></h4>
			<div><?php echo $categories;?></div>
			<div class="clear"><br /></div>
		<?php

		if ( current_user_can( 'upload_files' ) ) { ?>
			<div id="media-buttons" class="hide-if-no-js"><?php do_action( 'media_buttons' );?></div>
		<?php 
		}
		?>
			<h4 id="content-label"><label for="content">Content</label></h4>
			<div class="textarea-wrap">
				<textarea name="content" id="content" class="mceEditor" rows="3" cols="15" tabindex="2"><?php echo  $post->post_content; ?></textarea>
			</div>
			<script type="text/javascript">edCanvas = document.getElementById('content');edInsertContent = null;</script>
			<h4><label for="tags-input">Tags</label></h4>
			<div class="input-text-wrap">
				<input type="text" name="tags_input" id="tags-input" tabindex="3" value="<?php echo get_tags_to_edit( $post->ID ); ?>" />
			</div>
			<p class="submit">
				<input type="hidden" name="action" id="quickpost-action" value="post-quickpress-save" />
				<input type="hidden" name="quickpress_post_ID" value="<?php echo  (int) $post->ID; ?>" />
				<?php wp_nonce_field('add-post'); ?>
				<input type="submit" name="save" id="save-post" class="button" tabindex="4" value="<?php  esc_attr_e('Save Draft'); ?>" />
				<input type="reset" value="<?php esc_attr_e( 'Reset' ); ?>" class="button" />
		<?php
		if ( current_user_can('publish_posts') ) { 
			echo '<input type="submit" name="publish" id="publish" accesskey="p" tabindex="5" class="button-primary" value="' . esc_attr('Publish') . '" />';
		} 
		else { 
			echo '<input type="submit" name="publish" id="publish" accesskey="p" tabindex="5" class="button-primary" value="' . esc_attr('Submit for Review') .'" />';
		} 
		?>
			<br class="clear" />
			</p>
		</form>
		<?php
		if ( $drafts )
			wp_dashboard_recent_drafts( $drafts );
		return;
	}
}
$quickpress_category = new quickpress_category();

// Removes the option from the options table on deactivation
register_deactivation_hook( __FILE__, 'remove_options' );
function remove_options() {
	delete_option( 'quickpress_category' );
}
?>

Then under Settings, Writing, tick the new box that asks if you want to enable categories for QuickPress. Click it, save and you’re done.

Wrapping up

It’s actually easier than it looks. The post_class function opens the door for awesome customisation so go ahead and tumble away!


5 Responses

Comments

  • Says says:

    Thanks for the post. But I would have liked to see an example of the finished work. As is, I’m still somewhat at a loss as to how to replace my category names with icons.

Pingbacks

Shares
Share This

Share This

Liked this post? Help us by sharing it!

Shares