How to get rid of common WordPress Themes errors

Are you thinking of developing Free or Premium WordPress Themes? I can assist you to get rid of some common errors that I have come across over the years. I always go for a clean coding but still make mistakes while developing a theme, no matter how careful I am. This article will surely help you in avoiding these mistakes.

1. Don’t complicate the code

Always be careful while creating a nice looking theme, especially when you create a function which is similar to other functions which are available. For example, I was working on a function “get_portfolio_part($name, $slug). I created this function in order to save myself from writing boring and tiresome code, which is “get_template_part(“portfolio/$name”, $slug);”. This is what complicates the code which you are trying to use. Both codes are doing the same thing but in order to save those few keystrokes, it was hard to make out where that function is fetching files from.

I don’t know what came into my mind for making get_portfolio_part() function in my stupid head. What if I had to move the portfolio directory years later? Now comes the mistake no. #2.

2. Don’t anticipate the future

Sometimes we are pathetic in anticipating the future. Still, as a good developer, we always aim to do this most of the times.

For example, suppose you have created an option to show social icons in one of your posts. Forget, whether you are going for a plugin or using any other method to do this. Just think that this is what you are doing. So, this is the function that we will go for:

function has_social_icon($icon) {

    $icons = get_post_meta(get_the_ID(), 'post_social_icons', true);
    
    return true;    
}

Another thing comes to my mind, what if in future, I want to use this function outside the loop? This thought bring me to come up with something like this:

function has_social_icon($icon, $post_id = 0) {

    if( ! $post_id ) {
        $post_id = get_the_ID();
    }

    $icons = get_post_meta($post_id, 'post_social_icons', true);

    return true;    
}

And Now, you have written a function which is something needless in the name of anticipating the future. Always go for something that’s simple and fast to fulfil your requirement and not creating hypothetical scenarios that might or might not occur.

3. Don’t optimise before hand

I have experienced this issue very recently when I was optimizing a code which was not supposed to be optimised. Have you ever faced anything like this?

<?php
$post_id = get_the_ID();
$thumb = get_the_post_thumbnail( $post_id, 'large');
?>

<div id="post-<?php echo $post_id ?>"

<?php if( $thumb ): ?>
    <div class="thumbnail">
        <?php echo $thumb ?>
    </div>
<?php endif; ?>

</div>

Using a value for a variable just because you are using it two times in your code is actually going to save you .0000001ms. Wow, that’s really a lot of time that I am saving. Now here is the simplest way that WordPress uses for the same thing:

<div id="post-<?php the_ID() ?>"

<?php if( has_post_thumbnail() ): ?>
    <div class="thumbnail">
        <?php the_post_thumbnail('large') ?>
    </div>
<?php endif; ?>

</div>

Just because there is no benefit for the code that I was using doesn’t mean that I should’t optimize my code ever. Just be intelligent while doing so. Don’t use everything in a variable just to save a function call.

4. Always be up-to-date

Everything is evolving on Internet and same does WordPress. It’s always useful to be up to date with the best practices available. For example, I was going through some recent releases on WordPress.org and I noticed some of the themes are still using wp_print_styles instead of wp_enqueue_scripts, even though wp_print_styles has been deprecated since WordPress version 3.3.

If your main aim is to offer Free WordPress Themes or selling Premium WordPress Themes, keep your up to date with best practices and check the wordpress codex time to time to make sure you are doing it the best way.

5. Use original WordPress Themes functions if you can

Whenever possible, try and use original WordPress functions in your themes. For example, Custom Logo functionality used by WordPress is much better than any other function that you can use.

<?php if( has_custom_logo() ): ?>
    <a href="<?php echo esc_url( home_url() ); ?>" title="<?php bloginfo( 'name' ); ?>" class="custom-logo">
        <img src="<?php the_custom_logo() ?>" />
    </a>
<?php endif; ?>

Another example, when designing a post to post navigation, I always want to go for get_next_post function:

<?php
$next_post = get_next_post();
if (!get_next_post()): ?>
  <a href="<?php echo esc_url( get_permalink( $next_post->ID ) ); ?>"><?php echo esc_attr( $next_post->post_title ); ?></a>
<?php endif; ?>

Now, here is another mistakes that you might do. In this case, you can use get_the_title()function instead. That ways you’ll retrieve the title, prepend “Private/Protected” and apply the_title filter.

// do this
echo get_the_title( $next_post )

// instead of this:
echo $next_post->post_title

There is a WordPress function called next post link and you can substitute all above with just a clean function:

<?php next_post_link() ?>

Once again, some good research and keeping yourself up to date can help clear up themes.

6. Don’t develop your own Framework

We all want to have a clean interface and reusable code when we try to write it from scratch. When all this is mixed with before hand optimisation, anticipating future, ignoring original wordpress codes and complicating the code, that’s when a “Framework by me” is created.

In my 6 years of experience in WordPress Themes development, I have developed “Framework” almost 2-3 times and customised them countless times. Now I would suggest you not to do it and spare your time for other useful things that can earn you $$

When we already have well maintained framework out there, why develop something which is not perfect every time we use.

Conclusion

Most of the errors in WordPress Themes came due to my desire to save time in the future or to improve the code to make things nicer. It’s always good to follow WordPress coding and theme standards. Hopefully this article will help you not to commit the mistakes I did. All the best in creating great WordPress Themes.

SHARE:

Facebook
Twitter
Pinterest
LinkedIn
Reddit
Email

4 Responses

  1. Hi,
    What a joy to be here.
    These is indeed a wonderful and informative piece you have created. Lot of information to pick.
    Thank you so much for sharing this valuable information and links in this post.
    I am bookmarking this post for future read also.
    Thanks and regards.
    Mounika

  2. Hi,
    Nicely written.
    Nicely mentioned about best wordpress themes.
    I too made a review for one of the top WordPress themeWebsite
    design.
    Let me know if I can include your this article in any of my website’s
    article for my audience to know your great efforts too.

    Thank you!

  3. I provide WordPress training to my students. Your post is a great resource for my work. Always update your WordPress version and use the original theme for better response.

Leave a Reply

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

Social Media

Most Popular

Get The Latest Updates

Subscribe To Our Weekly Newsletter

No spam, notifications only about new products, updates.
On Key

Related Posts