Fall Sale! Code FALL2024 takes 25% OFF our Pro Plugins & Books »
Web Dev + WordPress + Security

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 '“<a href="https://perishablepress.com/press/index.php?tag='.$tag.'" title="Check out all articles tagged with '.$tag.'">'.$tag.'</a>”'; ?>

• Display SimplePie feed with specific parameters when using 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 unordered 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:

  • <?php previous_post_link() ?> — navigation links for single posts
  • <?php previous_posts_link() ?> — navigation links for archives
  • <?php posts_nav_link() ?> — navigation links for archives

• 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 a specific category:

<?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("&", "&", $item['link']);
		$item['link'] = str_replace("&&", "&", $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 on the way.. Lucky you! ;)

About the Author
Jeff Starr = Designer. Developer. Producer. Writer. Editor. Etc.
Banhammer: Protect your WordPress site against threats.

7 responses to “Miscellaneous Code Snippets for WordPress, Windows, and Firefox”

  1. lol. Lots of random code snippets as advertised.

  2. These are some pretty cool snippets. I like them all. Can’t wait for more!

  3. When you have people reading your stuff, there is little room ….long-winded introductions. ;)

    LOL

    some possibly useful stuff there…there so random though that I just dont know yet!

  4. Looks like the WP codes took the limelight of this post :) but that’s what I specifically love about it. WordPress has an interesting language and I’ve always wanted to learn it – of course, starting from basic php. The code snippets you’ve provided are very useful and with little php knowledge, can be easily adapted and modified for customized usage. I’ve learned how to use WP conditional comments the same way too.

    p/s: The wordpress navigation tags clarification is just PURE GOLD! I’ve never been able to wrap my head around the usage of these tags until you listed them up there. Thanks for sharing!

  5. Jeff Starr 2008/12/17 5:28 pm

    @teddY: I know! I have a terrible time trying to remember which navigation tags to use on various pages. For years I didn’t even realize the subtle variation on the previous_post_link() link (i.e., the previous_posts_link()), which is very similar, but works in places that the other doesn’t (such as on search and tag results pages). After realizing the difference, I went back and looked at some of my old themes and realized that I had been using both types without even knowing it. Sheesh! :p

  6. resourcesMix.info 2009/08/16 1:47 am

    cool snippets, very useful… thanks for sharing!

  7. Tutorials 2009/11/04 5:51 pm

    I found your site on a tutorial directory. We recently launched tutorialgrad.com. It’s similar to those other tutorial sites only easier for you. All you have to do is submit your RSS Feed once, we do the rest. We will check your feed for tutorials and post them daily, all with direct links to your site (we don’t frame your content). If you are interested please check it out and let us know what you think.

Comments are closed for this post. Something to add? Let me know.
Welcome
Perishable Press is operated by Jeff Starr, a professional web developer and book author with two decades of experience. Here you will find posts about web development, WordPress, security, and more »
Wizard’s SQL for WordPress: Over 300+ recipes! Check the Demo »
Thoughts
Went out walking today and soaked up some sunshine. It felt good.
I have an original box/packaging for 2010 iMac if anyone wants it free let me know.
Always ask AI to cite its sources.
All free plugins updated and ready for WP 6.6 dropping next week. Pro plugin updates in the works also complete :)
99% of video thumbnail/previews are pure cringe. Goofy faces = Clickbait.
RIP ICQ
Crazy that we’re almost halfway thru 2024.
Newsletter
Get news, updates, deals & tips via email.
Email kept private. Easy unsubscribe anytime.