5 Tips to Being a Better WordPress Developer

It happens to every freelancer to be in need of outsourcing some work once in a while. It has happened to me too. I hired this good fellow from India a year back to do a couple of projects while I was doing other work. But being a quality freak, I soon found out that the work was not to the level I was used to. I grew to dislike outsourcing as a practice because of that and even had this thought formed in the back of my mind that Indian coders were not professional enough. This is true to some extent, but I urge you as well as myself to judge work on a case to case bases, rather than over-generalize.

Hoping things would get better, I wrote the guy an email pointing out what is wrong with the way he was coding and how it could be improved. Below you will find the original email with some added notes to make it a bit clearer. And it starts like this:

Tweaking the work you have done to fit client’s requests I have found some details you need to take into account on the future projects. Please take this as a friendly advice and I hope you find those tips useful.

And continues:

1. Thoughtful coding

What’s the point of commenting this?

<div class="content"><!--content-->

Comments are meant to be used to clarify and help with non-obvious stuff, rather then redundantly describe stuff that is very clear.

2. Careful coding

You could put all the lines into a single PHP tag pair.

<?php $id = get_the_ID();?>
<?php $agentMeta = get_post_meta($id, '_agent_detail', false);?>
<?php $agentMeta = $agentMeta[0];?>
<?php $agentExpertise = get_post_meta($id, '_agent_expertise', false);?>
<?php $agentExpertise = $agentExpertise[0]['expertise'];?>
<?php $image = wp_get_attachment_url( get_post_thumbnail_id( $id ) );?>

Careful coding means you pay attention to details and try to write code the best way possible by avoiding mainly repetitions and being consistent in style.

3. Do it the WordPress way

<?php $thumbnail = get_the_post_thumbnail();
    if(!empty($thumbnail)) {?>
    <div class="post-thumbnail"><?php echo $thumbnail; ?></div>
<?php } ?>

The above code can be refactored to use WordPress code this way. It looks better, and you don’t have to reinvent the wheel on this case.

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

4. Be generic – never do this

<h2 style="margin-top:25px;">location</h2>

Inline styling is something that should be avoided all the time. That’s why CSS classes where invented. Since we are here, CSS id’s should hardly ever be used for styling too.

5. Indenting

Indent thoroughly and carefully. Always use tabs, not spaces for indenting. Indenting is very important. Incorrect indenting or the lack of it tells you a lot of the coder.

So, that’s it for this article. What would you add the the list?