Quick and Easy Code Snippets for Wordpress

Quick and Easy Code Snippets for Wordpress

While WordPress remains the number one free online content management system, we decided we would put together another snippet post of helpful code that can make your overall WordPress experience that much more pleasant. These easily accessible additions to your site can help tweak some areas that will offer you a bit of a break as the life of your site winds on. So take a look down through the list and grab any of the snippets that snag your eye and seem to have a place in your WordPress!

Snippet #1 - Dynamic Copyright Year

date(’Y') pulls in the current year keeping you from ever having to update the copyright year in your footer.

Copyright &copy; 2005-<?php echo date('Y'); ?> site.com

Snippet #2 - Display the Current Date

A quick and simple way to add the current date anywhere on your site.

<?php echo date('l F jS Y'); ?>

date() can be modified in a number of different ways. The example above would output the equivalent of Mon Apr 3rd 2009. Below you will find a list of useful formatting options.

d - The day of the month (from 01 to 31)
j - The day of the month without leading zeros (1 to 31)
D - A textual representation of a day (Mon)
l (lowercase 'L') - A full textual representation of a day (Monday)
S - The English ordinal suffix for the day of the month (st, nd, rd or th)
F - A full textual representation of a month (January)
M - A short textual representation of a month (Jan)
m - A numeric representation of a month (01 to 12)
n - A numeric representation of a month without leading zeros (1 to 12)
Y - A four digit representation of a year (2009)
y - A two digit representation of a year (09)
a - Lowercase am or pm
A - Uppercase AM or PM
h - 12-hour format of an hour (01 to 12)
H - 24-hour format of an hour (00 to 23)
i - Minutes (00 to 59)
s - Seconds (00 to 59)

Snippet #3 - Add an Edit Link to the Bottom of each Post

This allows any administrator to directly edit a post if they happen to find an error while reading the page.

<?php
edit_post_link(’Edit this post’,,); 
?>

Update Thanks to Paul - This is a much easier way to add an edit link. I'm leaving the code below for (current_user_can('level_10')) which can be used to add any element to your site that you only want admins to see.

<?php
if (current_user_can('level_10')){ ?>
    <a href="<?php bloginfo('wpurl');?>/wp-admin/edit.php?p=<?php the_ID(); ?>">Edit Post</a>
<?php } ?>

Snippet #4 - List Scheduled Posts

This is a nice little snippet for those people who want to show their readers what they have coming up on their blog. This will show a list of any posts you have scheduled to publish. Titles only obviously, no links.

<?php
$my_query = new WP_Query('post_status=future&order=DESC&showposts=5');
if ($my_query->have_posts()) {
    while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <?php the_title(); ?>
    <?php endwhile;
}
?>

Snippet #5 -Display Categories in a Drop-Down Menu

This is a great option for blogs who have a large number of categories and not enough room to display them.

<form action="<?php bloginfo('url'); ?>/" method="get">
<?php
$select = wp_dropdown_categories('show_option_none=Select category&show_count=1&orderby=name&echo=0');
$select = preg_replace("#<select([^>]*)>#”, “<select$1 onchange=’return this.form.submit()’>”, $select); echo $select; ?>
<noscript><input type=”submit” value=”View” /></noscript>
</form>

You can also display your archives in a drop-down menu by using the following code.

<select name="archive-dropdown" onChange='document.location.href=this.options[this.selectedIndex].value;’>
<option value=””><?php echo attribute_escape(__(’Select Month’)); ?></option>
<?php wp_get_archives(’type=monthly&format=option&show_post_count=1); ?> </select>

Snippet #6 -Disable Comments on Older Posts

This is a great way to cut down on spam for your blog as well as save you time by not having to worry about replying to comments on older posts.

Paste this code into functions.php. Notice the 30 at the end of the 4th line in the code below. This turns comments off on posts more than 30 days old. You can change this number to alter the age of the posts.

?php
function close_comments( $posts ) {
	if ( !is_single() ) { return $posts; }
	if ( time() - strtotime( $posts[0]->post_date_gmt ) > ( 30 ) ) {
		$posts[0]->comment_status = 'closed';
		$posts[0]->ping_status    = 'closed';
	}
	return $posts;
}
add_filter( 'the_posts', 'close_comments' );
?>

Wrap-up

So that's our snippet list. What's your favorite quick and easy code snippet for wordpress?

Share this Article:

  • RSS
  • Facebook
  • del.icio.us
  • StumbleUpon
  • Design Bump
  • Design Moo
  • The Web Blend
  • Design Float
  • ZaBox
  • Twitter
  • DZone
  • Digg

More From Arbenting:

16 Comments & Reactions

  1. May 13, 2009 at 3:27 pm | Permalink

    keep doin the great job you’re doing. thank you — these are great!

  2. May 13, 2009 at 5:06 pm | Permalink

    2nd try for correction for snippet 3:

    if (current_user_can('level_10')){ ?>
    	<a href="/wp-admin/post.php?action=edit&post=">Edit Post
    

    We’ll try the correction this way. — Bob

  3. May 13, 2009 at 10:50 pm | Permalink

    Just a quick question about “Snippet #4 — List Scheduled Posts”, sometimes similar features collides with “adhesive post” widgets, resulting in mig adhesiv posts (that I have put on top in the blog sektion) also shows up as upcoming ..

    Does this do the same?

    Lars Karlssons last blog post..Veckans länktips: Så bygger du tillgängliga webbplatser

  4. May 14, 2009 at 5:13 am | Permalink

    Hey! There is a custom wordpress function for editing the post if you are an admin:

    edit_post_link(‘Edit this post’, ”, ”);

    (surrounded by the php tags)

    Pauls last blog post..Video: Jesus Church Oslo

  5. May 15, 2009 at 10:22 pm | Permalink

    @angie Using the cide for showing future posts, interferes with the built in feature of WordPress to make a special article stick att the top at your blog-page.

    This is resulting in that on top of the list of upcoming posts, is my adhesive post at the top.

    I have nothing to show, since the blog is building up.. Sorry.

    Is there a way to work around this, or “another way of thinking” in terms of ways to put important articles on top, that will not interfere with the code in example four?

    Lars Karlssons last blog post..Länka med ord som beskriver länkmålet bäst

  6. May 16, 2009 at 6:13 am | Permalink

    Some useful snippets!

    thanks :)

    Liam McCabes last blog post..Fantastic Free eBook for Creative Designers!

  7. May 17, 2009 at 11:43 pm | Permalink

    Uh, this could come handy, bookmarking :)) thanks :)

    Dainis Graveriss last blog post..Bannersnack Giveaway Contest And 9th Month Feedback

  8. June 27, 2009 at 8:44 pm | Permalink

    Very useful. Thanks for sharing :D
    hieplt´s last blog ..Trầm tư My ComLuv Profile

Trackbacks/Pingbacks

  1. Quick And Easy Code Snippets For WordPress | Design Newz
  2. The week in links 15/05/09 - Craig Baldwin's Blog
  3. Perpetual Work in Progress » Blog Archive » Quick and Easy Code Snippets for Wordpress | Arbenting
  4. 50 Excellent Wordpress Resources | JEFFSTYLE
  5. You are now listed on FAQPAL

Leave a Reply

Get Free Updates

Subscribe via RSS

Subscribe via Email

Community News

Submit News