10 Best Practices For WordPress Theme Development

Developing or designing a WordPress theme is a long and hard work, especially for themes with theme options, widgets, semantic code, clean interface and SEO features. In this article, I have compiled 10 best practices for rapid and quality WordPress theme coding, that will probably be useful for you.

1. Using theme constants

It is usually happened when you want to refer to theme image or javascript folder, or blog homepage url. Using repetitively functions like get_bloginfo() or get_stylesheet_directory_uri() is not a good idea, because it wastes server's resource for creating and calling functions. Why don't we define repetitive information as constants? Just include the following code into functions.php file of your theme:

define( 'HOME_URI', home_url() );
define( 'THEME_URI', get_stylesheet_directory_uri() );
define( 'THEME_IMAGES', THEME_URI . '/images' );
define( 'THEME_CSS', THEME_URI . '/css' );
define( 'THEME_JS', THEME_URI . '/js' );

and use them when needed, for ex. to include favicon, use

<link href="<?= THEME_URI; ?>/favicon.ico" rel="shortcut icon" />

2. Theme localization

If you want your theme to be widely used, you have to think about internationalization (localization). In WordPress, it is easily be done by including these lines into theme functions.php file:

$lang = TEMPLATE_PATH . '/lang';
load_theme_textdomain( 'theme_textdomain', $lang );

and use _e(), __() functions instead of echo naked strings:

_e( 'Some string', 'theme_textdomain' );
echo '<h2>' . __( 'Some string', 'theme_textdomain' ) . '</h2>';

Please read this article for more details and how-to instruction to localize WordPress theme.

3. SEO meta tags

Although WordPress is well SEO supported, you can still improve SEO level of your theme by using some small tips for creating meta title and meta description tags.

One popular title format is Postname - Blogname, to do this, just paste the following code into header.php file:

<title><?php wp_title( ' - ', true, 'right' );</title>

And use filter a filter to change how the title tag looks on the frondend. Read this article for more details.

And to auto generate meta description tag for single pages (posts, pages), you can read this article.

4. Don't forget wp_head, wp_footer and comment_form hooks

This is very important because these are popular hooks that used by many plugins. To implement that, just paste wp_head() into the head section of header.php file, wp_footer() into footer.php file and do_action( 'comment_form', $post->ID ) at the end of comment form in comments.php file.

5. Creating theme options

Yes, this will definitely bring your theme into a higher level! With theme option panel, user can easily change many options like Google Analytics ID, header logo, background color, ads codes, etc.

6. Creating sidebars and widgets

Besides theme option panel, sidebars and widgets are often used in professional themes. Creating sidebars is simple in WordPress. Paste the following code into functions.php file:

if ( function_exists( 'register_sidebar' ) ) {
    register_sidebar( array( 
        'name' => __( 'Left Sidebar', THEME_NAME ),
        'before_widget' => '',
        'after_widget' => '',
        'before_title' => '',
        'after_title' => ''
     ) );
    register_sidebar( array( 
        'name' => __( 'Right Sidebar', THEME_NAME ),
        'before_widget' => '',
        'after_widget' => '',
        'before_title' => '',
        'after_title' => ''
     ) );
}

7. Using theme template files

If you want your blog have different layouts for some particular pages like Contact, Term Of Service or even Homepage, using theme template files is the fastest way without any effects to the rest of your blog.

To create a template, paste these lines into the beginning of a new PHP file in your theme folder:

< ?php
/*
Template Name: Any name you want
*/

// Your code of template here
?>

And when you create a new page, remember to choose this template in the Template dropdown box on the right.

8. Using theme hierarchy efficiently

Understanding theme hierarchy and using it efficiently will help you economize time to create multiple layouts for your blog.

For example if you want to include different sidebar (named sidebar-special) into only category page with ID 100, you can create a PHP file named category-100.php and include the following code into it:

<?php get_sidebar( 'special' ); ?>

Then create a PHP file named sidebar-special.php, here you can paste your own code of what you want to display in the special sidebar.

Combining theme hierarchy with theme template in the 6th section above can give you much more power to full control your blog layout.

9. Using theme frameworks and child themes

The big benefit when you use a WordPress theme framework is that there are many built-in features like SEO, additional hooks, more widgets and layouts. Besides, you can extend theme frameworks with child themes. This helps you so much by economizing theme developing time.

There are many theme frameworks, free and premium. For beginners, Hybrid, Thematic and Carrington are good starts.

If you don't want to use any WordPress theme frameworks, it's still worth looking at their code and structure.

🔥 HOT: Interested in boosting your WordPress SEO? My Slim SEO plugin is a super lighweight and automated plugin that handles most the hard work for you: meta tags, sitemap, redirection, schema & link building.

👉 Have a look and you will love it: wpslimseo.com

Leave a Reply

Your email address will not be published. Required fields are marked *


Copyright © 2024 Deluxe Blog Tips. All rights reserved.

My products: Meta Box | Slim SEO