15 Snippets To Prepare Your Theme For WordPress 3.0
WordPress 3.0 Is Coming This Weekend
The very long-awaited all new version of WordPress is being released publicly this weekend, yes, it's true. I know it may sound like insanity but it's the truth.
Update: This didn't get released yet and they are now looking to move on releasing beta 2 soon. There are still a large number of bugs that need fixing so the release date was pushed back again.
If you like these examples, you should check out 15 more random code snippets here, too. Good for all kinds of various things not just 3.0.
First off, you can make things simpler and less stressful by using the new easier 3.0 constants for debugging.
In your wp-config.php file you would add:
//enable the reporting of notices during development - E_ALL
define('WP_DEBUG', true);
//use the globally configured setting for display_errors and not force errors to be displayed
define('WP_DEBUG_DISPLAY', true);
//error logging to wp-content/debug.log
define('WP_DEBUG_LOG', true);
// load the development (non-minified) versions of all scripts
// and CSS, and disables compression and concatenation
define('SCRIPT_DEBUG', true);
//define('E_DEPRECATED', true);
//E_ALL & ~E_DEPRECATED & ~E_STRICT
Enabling Multi-sites Capabilities
Since WordPress and WordPress MU (Multi-User) were merged in 3.0 you can now create mutliple separate blog sites on one blog installation. All you have to do is add this to your functions.php file or as an include in it.
define('WP_ALLOW_MULTISITE', true);
Do keep in mind that mulit-site is still very new and subject to change as far as I know. It is still usable though.
Short Little Baby Links
Use shortlinks for submitting links to sites like Twitter or Facebook instead of the long permalink to your post. This will be standard in 3.0 so adding the action would not be necessary.
// shortlink examples
the_shortlink( $text, $title, $before, $after );
the_shortlink();
the_shortlink(__('Shortlinks Rock'));
the_shortlink('short link', null, '<ul><li>', '</li></ul>');
// another way of looking at it
if (function_exists('the_shortlink')) {
the_shortlink( __('Shortlink'), __('Optional HTML after the link'), '.' );
}
// some hooks you can use pre_get_shortlink // Allow plugins to short-circuit this function get_shortlink // Filter the output
Better Url Functions For Home, Site And Admin
The new easier way to link to your home url, site url, and admin url of current site OR a given site other than the current one. It's easier than writing bloginfo('url') or get_option('site_url') over and over.
Just another way to get the same information more quickly depending on how you plan to use it.
With the following function snippets you can get or return the current site's:
home_url(); // retrieve the home url of the current site get_home_url(); // return the home url of the current site
site_url(); // retrieve the url of current site get_site_url(); // return the url of current site
admin_url(); // the admin url of current site @since 2.6 get_admin_url(); // retrieve the url to the admin area for a GIVEN site, defaults to current
Adding WordPress 3.0 Theme Support
Some things you need to add support for to your theme for use with 3.0 are features like custom backgrounds, and header images, nav menus, and post thumbnails, This It's super easy to add support for custom backgrounds and menus, as well as post thumbnails, which you may have done already since that was released in WordPressP 2.9. If not, might as well do it now right!
Custom backgrounds and menus, however, are all new in 3.0. You just need to add the following to your theme's functions.php file.
add_custom_image_header(); // custom image in header add_custom_background(); // custom backgrounds support
// add post thumbs support
<?php add_theme_support( 'post-thumbnails' ); ?>
// then add this to you theme to show post thumbs
<?php
// if post has a thumbnail
if ( has_post_thumbnail() ) {
the_post_thumbnail( 'thumbnail' );
} else { // get a default image if no post thumb is set
?>
<img src="<?php bloginfo('template_directory'); ?>/images/125banner.gif" alt="No Image" title="No Image" />
<?php } ?>
add_theme_support( 'nav-menus' ); // add menu support to your functions.php
// Add this where ever you want menus to show (ex: header)
wp_nav_menu('sort_column=menu_order&container_class=navigation');
// Target a specific menu
wp_nav_menu( array('menu' => 'Menu Name Here' ));
// Get the HTML list content for nav menu items
walk_nav_menu_tree('sort_column=menu_order&container_class=navigation');
Add a simple easy to use login form anywhere you want!!
This is extremely useful, no more need to go to wp-login.php to login. You can drop this little snip into your theme anywhere.
// add a login form anywhere easily <?php wp_login_form(); ?>
It would be nice if they also added a wp_register_form() but they didn't. Luckily, I have learned how to create jQuery tabs for login register password reset forms and so can you! You'd be surprised how easy it is, but how freaking long it took me to finally figure out.
New Life-saving Comment Form
It's no secret that the comments.php file has always been one of the most annoying files of a theme to create. The comment section for this site for example, took me longer to code than the rest of the entire sites theme template files did put together. Well now, 3.0 is here to save the day with the new simple comment form.
<?php comment_form($args, $post_id); ?>
Creating Custom Post Types
The ever elusive custom post types. Long awaited among the WordPress community, as you probably know.
Update: Creating custom post types has been changed with an update.
$args = array(
'label' => __('TV Shows'),
'singular_label' => __('TV Shows'),
'public' => true,
'show_ui' => true,
'capability_type' => 'page',
'hierarchical' => false,
'rewrite' => true,
'query_var' => 'tvshows',
'supports' =>
array (
'title',
'thumbnail'
)
);
register_post_type( 'tvshows' , $args ); // register the post_type
Going Even Further With Custom Taxonomies AND Post Types
This shows you how to create custom taxonomies to use with your post types. For setting up full customization of taxonomies and post types, refer to this post.
function post_type_bands() {
//register your post type
register_post_type ( 'bands',
array('label' => __('Bands'),
'public' => true,
'show_ui' => true,
'supports' =>
array (
'post-thumbnails',
'excerpts',
'trackbacks',
'comments'
)
)
); // end register_post_type()
// create categories specific for this post type
// adding the custom taxonomy for genres.
register_taxonomy( 'genres', 'bands',
array(
'hierarchical' => true,
'label' => __( 'Genres' )
)
); // end register_taxonomy( 'genres' )
// Add another Taxonomy for Performer.
// For adding tags specific for this post type.
register_taxonomy( 'performer', 'bands',
array(
'hierarchical' => false,
'label' => __( 'Performer' ),
'query_var' => 'performer',
'rewrite' =>
array(
'slug' => 'performer'
)
)
); // end register_taxonomy( 'performer' )
} // end post_type_bands()
add_action( 'init', 'post_type_bands' );
Custom Post Type Query
<ul>
<?php global $wp_query;
$wp_query = new WP_Query("post_type=albums&post_status=publish");
while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></li>
<?php endwhile; ?>
</ul>
Setting up the custom post type query and loop is similar to the way you would do it for any other custom query using WP_query(). You just have to query the post_type=TYPE and get the posts that have been published.
For more information about creating custom post types you can find more links and resources below for this. This is the most confusing thing added in 3.0 and may not make much sense at first.
Custom CSS Styling Your Post Editor
You can now edit the look of the TinyMCE post form to use your own styles. You just
add_filter('mce_css', 'my_editor_style');
function my_editor_style($url) {
if ( !empty($url) )
$url .= ',';
// Change the path here if using sub-directory
$url .= trailingslashit( get_stylesheet_directory_uri() ) . 'editor-style.css';
return $url;
}
Get The Author Templates
This function can be used for more than just author pages, but in this case it is.
You can also use it to create custom category templates, and pages.
get_category_template($author); // custom author pages get_category_template(); // custom category pages
Sources, Resources and Sorcery
- Function Reference
- WordPress Developer Hint
- 8 Useful WordPress 3.0 Snippets
- WordPress 3.0 Enable Network
Templates:
- link-template.php
- general-template.php
- nav-menu-template.php
- theme.php
- The Shortlink
- add_custom_image_header()
- register_taxonomies()
- Function: register_post_type()
- Custom Post Types in 3.0
- Extending Custom Post Types in 3.0








User Comments
( ADD YOURS )nestdev April 29
Hi Jared !, thank's for sharing. join our community http://nestdev.com. promote your article in there. :)
Deluxe Blog Tips May 16
Great snippets. Very useful for all WP users. Thanks for sharing.
Bruce May 21
Great snippets. Very useful for all WP users. Thanks for sharing.
TEO June 16
how can i remove the /category/ part from the wordpress url
i have tried a lot of plugins but no luck.
i'm using wordpress 3.0 RC 3
Jennifer R June 18
Thanks you for sharing this great snippet of WordPress 3.0
inwebdeveloper August 8
very usefull, i just upgrade my blog for wordpress 3.
Trackbacks