Miscellaneous Code Snippets for WordPress, Windows, and Firefox

[ Miscellaneous Color Slices ] One of the original purposes of Perishable Press involved serving as a “virtual dumpster” for all of my miscellaneous code snippets. Over time, I continued elaborating to greater degrees on the various code recipes that I was posting, until eventually those brief snippet posts evolved into complete, richly detailed articles (at least from my point of view). Now that I enjoy the luxury of writing for an incredible audience, I try to avoid posting anything that doesn’t include an accompanying explanation. “If it’s worth posting, it’s worth explaining,” I always say. When you have people reading your stuff, there is little room for superfluous nonsense, unexplained code snippets, and long-winded introductions. ;)

Even so, every now and then you need to break the rules, shake up the routine, rock the boat, drop some acid, that kind of thing. Lately, I have been doing some deep archiving and have amassed a considerable collection of completely miscellaneous and unrelated chunks of code. There are too many random snippets to spend time sewing together similar functionality, and I really hate deleting perfectly good code. I also hate keeping misfit code chunks lying around in my otherwise pristine digital archive (joking). Fortunately, this dilemma is easily resolved by loosening up and simply dumping the information right here on the site. After all, that’s what it was originally designed for — in fact, the further you dig back into the archives, the more apparently pointless code snippets you will find. So without further ado, I now present a completely random, unexplained, miscellaneous collection of potentially useful code snippets!

WordPress Code Snippets

• Display the current tag as a link for old versions of WordPress (less than 2.3):

<?php $tag = $_GET['tag']; echo '&ldquo;<a href="http://perishablepress.com/press/index.php?tag='.$tag.'" title="Check out all articles tagged with '.$tag.'">'.$tag.'</a>&rdquo;'; ?>

• Display SimplePie feed with specific parameters when using the SimplePie plugin:

<?php echo SimplePieWP('http://feeds.feedburner.com/domain-feed', 
	   array(
		 'items' => 7,
		 'cache_duration' => 3600,
		 'date_format' => 'l, j F Y, g:i a',
		 'template' => 'sideblog'
	   )); 
?>

• Display three random posts from two tag archives and output their titles in an unorderd list:

<ul>
	<?php query_posts('tag=apache,htaccess&showposts=3&offset=0&random=true'); ?>
	<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

	<li>
		<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
			<?php the_title(); ?>
		</a>
	</li>

	<?php endwhile; endif; ?>
</ul>

• Display different “more” link for first post (requires loop):

<?php static $ctr = 0; if ($ctr == "0") { ?>

<span class="more-link">
	<a href="<?php the_permalink() ?>#comments" title="Respond to: <?php the_title(); ?>">
		Respond to this article
	</a>
</span>

<?php  $ctr++; } else { ?>

<span class="more-link">
	<a href="<?php the_permalink() ?>" title="Continue reading: <?php the_title(); ?>">
		Continue reading this article
	</a>
</span>

<?php  $ctr++; } ?>

• Display all posts in descending order for a specific category:

<ul>
	<?php global $post; 
		     $myposts = get_posts('numberposts=-1&offset=0&category=1&order=DESC'); 
		     foreach($myposts as $post) : 
	?>
	<li>
		<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
			<?php the_title(); ?>
		</a>
	</li>
	<?php endforeach; ?>
</ul>

• Notes about WordPress navigation template tags (I can never get these straight):

  • <?php previous_post_link() ?> — navigation links for single post views
  • <?php previous_posts_link() ?> — navigation links for search or index archives
  • <?php posts_nav_link() ?> — navigation links for index archives only (only one instance required)

• Display content only in old posts (currently set for seven days):

<?php 
$target_date = date('YMD');
$posted_date = floor((date('U') - get_the_time('U')) / 86400); // 86400 seconds = 1 day

// edit next line to change number of days
if($posted_date >= 7) {
	$target_date = 1;
}
if(is_single() && $target_date == 1) { ?>

<h1>Place content for older posts here</h1>
<p>Advertisements, special links, offers, feeds, etc.</p>

<?php } ?>

• Display author comments with a special style. I was going to write a big article on the many different ways to do this, but there are already so many articles on the topic that it’s like, what’s the point?

if($comment->user_id == $post->post_author) {

<div class="author-style">
	<!-- comment content here -->
</div>

} else {

<div class="default-style">
	<!-- comment content here -->
</div>

}

..and here’s another way of doing it for multiple author blogs. This code will highlight the comment of any administrator:

if(!empty($comment->user_id) && get_userdata($comment->user_id)->wp_user_level == '10') {

<div class="author-style">
	<!-- comment content here -->
</div>

} else {

<div class="default-style">
	<!-- comment content here -->
</div>

}

• Display an alphabetical tag cloud using 3pt for the smallest links and 12pt for the largest links:

<?php wp_tag_cloud('smallest=1&largest=9'); ?>

• Display custom category templates depending on category. Use this code as your category.php file:

<?php $post = $wp_query->post;

if (in_category('1')) {

	include(TEMPLATEPATH.'/category-1.php’);

} elseif (in_category('2')) {

	include(TEMPLATEPATH.'/category-2.php');

} elseif (in_category('3')) {

	include(TEMPLATEPATH.'/category-3.php');

} else { 
	include(TEMPLATEPATH.'/category-0.php'); 
} ?>

• Display different stylesheet for some specific category. This should be obvious:

<?php if (is_category('7')) {

	<link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/category-7.css" type="text/css" media="screen" />

<?php } else { ?>

	<link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>" type="text/css" media="screen" />

<?php } ?>

• Display the value of the current search query:

<?php the_search_query(); ?>

• Display external feed via WordPress’ built-in Magpie feed parser:

<?php 
include_once(ABSPATH.WPINC.'/rss.php');
define("MAGPIE_OUTPUT_ENCODING", "UTF-8");

$feed  = fetch_rss("http://domain.tld/feed/");
$limit = 10; // set the number of feed items
$items = array_slice($feed->items, 0, $limit);

if(!empty($items)) {
	echo '<dl>';

	foreach ($items as $item) {

		echo '<dt><a href="';

		$item['link'] = str_replace("&", "&amp;", $item['link']);
		$item['link'] = str_replace("&amp;&amp;", "&amp;", $item['link']);

		echo $item['link'].'" title="'.$item['title'].'" rel="nofollow">'.$item['title'].'</a></dt>';

		if (isset($item['description'])) {
		        echo '<dd>'.$item['description'].'</dd>';
		}
	}
	echo '</dl>';
}
?>

Windows Code Snippet

• Disable Autorun on Windows XP

The “autorun” functionality of Windows XP automatically reads content and executes commands based on content type. For example, inserting a DVD into a box running WinXP causes Windows to automatically evaluate the contents of the disc and then serve a dialogue box, or otherwise execute any predefined or pre-associated programs based on file type.

This is extremely bad from a security point of view, if not from a usability perspective. Personally, I hate it when my machines do anything that I have not told them to or approved of, and even worse, autorun can ruin data if it accidentally executes a nasty virus hidden on a disc or other input source.

Fortunately, disabling the autorun feature from Windows XP is rather easy. Simply modify the registry according to these values and say goodbye to unwanted autorun functionality:

Set a zero "0" value for "[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Cdrom\AutoRun]"

Also set: "AutoRun"=dword:00000000 for "[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Cdrom]"

To renable: "AutoRun"=dword:00000001 for "[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Cdrom]"

Firefox Code Snippets

• Shortcut tricks for multiple installations of Firefox:

C:\Program Files\Mozilla Firefox 3\firefox.exe" -no-remote -p Firefox3_user
C:\Program Files\Mozilla Firefox 3\firefox.exe" -no-remote -p Firefox2_user
C:\Program Files\Mozilla Firefox 3\firefox.exe" -no-remote -p Firefox1_user

• Create Firefox profiles via command prompt:

run > firefox -p

That’s all for now..

But I am still organizing and pruning my archives, so there be a couple more of these “miscellaneous code snippets” posts before the end of the year.. Lucky you! ;)