It’s not only a matter of style!

comment 0

Every time I tweak a WordPress theme to adapt it to my customer needs, I face code written with a style that is difficult to read.

Unfortunately it’s not only a matter of style as this kind of code increases the time required to render the page.

Now, we do many efforts to keep the pages rendering as quickly as possible, and we do this for two very good reasons. First of all the quicker the page is shown in the visitor browser, the more she/he his happy and, hopefully, keep visiting the other pages.

Secondly, but maybe even more important, we want to get a better position in the search engines result page.

Here is an example of a bad example of coding that I do not expect to find in a premium theme – but usually do:

<?php get_header(); ?>
    <?php if( is_home() && ! is_paged() ) : ?>
        <?php if( theme_get_option'slider' ) ) : ?>
            <?php get_template_part'slider' ); ?>
        <?php endif; ?>
        <?php get_sidebar'one' ); ?>
        <?php get_sidebar'two' ); ?>
    <?php elseif( ( is_home() && is_paged() ) ) : ?>
        <?php theme_current_location(); ?>
    <?php endif; ?>
    <div id="container">
        <section id="content">
            <?php if( have_posts() ) : ?>
                <div class="entries">
                    <?php while( have_posts() ) : the_post(); ?>
                        <?php get_template_part'content'get_post_format() ); ?>
                    <?php endwhile; ?>
                </div><!-- .entries -->
                <?php theme_posts_nav(); ?>
            <?php else : ?>
                <?php theme_404(); ?>
            <?php endif; ?>
        </section><!-- #content -->
        <div class="clear"></div>
    </div><!-- #container -->
<?php get_footer(); ?>

To make the above code quicker to execute its a matter of minutes, simply remove all the un-needed closing and opening PHP tags.
The following code closes and opens the PHP tags twice:

<?php get_sidebar'one' ); ?>
<?php get_sidebar
'two' ); ?>

This means that the PHP interpreter needs to be called twice to evaluate the code!
A better way to write the above example is this one:

'one' );
get_sidebar'two' );

Well, you can say that the servers are now so fast that you can barely note the difference when executing the modified code. This is apparently true as it’s difficult to spot on a single page.

However, you should to consider also other aspects, like:

  • a theme is made of several PHP scripts: to show a single page many of them are executed
  • if you are on a shared hosting – that is the most common situation – the power of the server is shared to all the installed sites/blogs, meaning you do not have all the resources available to your site
  • all the spaces inserted between a close PHP tag ?> and the next PHP open tag <?php are unnecessarily sent to the browser
  • the HTML comments used by the programmer to geet a reminder about what the tag was opened, look for <!– .entries –> in the above code are also sent to the browser and are totally not needed — these can be avoided by using a good editor like, for example: PhpStorm
  • last, but not less important: the more we reduce the overload on the server, the less power we use contributing actively to a better world!

Here is a revised version of the above script. To reduce the amount of code and to make it more readable, I have also used the curly brackets instead of the “endif” and “endwhile” tags:

is_home() && ! is_paged() ) {
theme_get_option'slider' ) ) {
get_template_part'slider' );
get_sidebar'one' );
get_sidebar'two' );
elseif( ( 
is_home() && is_paged() ) ) {
?><div id="container">
    <section id="content"><?php 
if( have_posts() ) {
?><div class="entries"><?php 
while( have_posts() ) {
get_template_part'content'get_post_format() );
        else {
    <div class="clear"></div>
(); ?>

The drama is that, if you optimize a template for a customer, the next time the theme is updated you have to start optimizing again…

Theme developers: is it time to change the way you code your themes, isn’t it?

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.