8 Things Every WordPress Developer Should Know

By / Jun 4, 2013 / Tips
shares

WordPress is certainly a great tool, and it’s quite easy to get started. You can count on a lot of good resources (like this humble blog) and you can even dig into WP’s source code if you know PHP quite well.

But also, it’s quite easy to get lost. There are a lot of options, a lot of solutions, and sometimes it’s way harder to find the best solution than just fixing an issue. Well, let’s see some basic tips and snippets to help you solve some everyday issues.

8 Things Every WordPress Developer Should Know

1. You really shouldn’t use query_posts()

The truth is, among many reasons, you shouldn’t ever use query_posts. This is the most simplistic version of the loop, but as jQuery code, it’ll run a lot of background operations that will mostly rely on the WP_Query (then get_posts) and will require you a lot of code to clean up the mess.

In short, WordPress will load the main query BEFORE calling template files, so if you call a query_post() in your index.php file you’re actually calling 2 queries since the first one was already called. And if you consider the background queries it’s actually 8 (since each WP_Query loads 4 queries, call posts, count posts, call metadata, call terms).

What you should do:

  • Use WP_Query object whenever you need multiple loops in a page. So, sidebar loops, secondary loops in a page template and anything like this will be better using this function
  • Use pre_get_posts filter to modify the main loop. If you need to modify in any way the main WordPress loop (pretty much the case that you would use query_posts) just go for the pre_get_posts filter since it modifies directly the main WP_Query object (instead of getting a new DB query)
  • Use get_posts() if you don’t need a loop. If you are just getting posts and don’t need the main loop functions you could use this one since it’ll return you a simple array of posts

2. Always enqueue your scripts & styles

When you are creating themes, plugins or customizing any of these you may need to load external files. But each WordPress can contain a lot of things, and if you call a JS library twice you can potentially break the site.

The simple solution is introduced with the wp_enqueue_script function, since you can load (and register) a library or script and make sure that you are loading only one copy of it. The same rule applies for styles, but they won’t cause too much damage. Still a good option for loading standard styles, like styling for jQuery plugins, or CSS for HTML bootstraps.

3. Cache your stuff

8 Things Every WordPress Developer Should KnowImage from Kevin Andersson

If you are a plugin developer you should know the transients API. They allow you to store “options” for a small amount of time. So, if you are getting latest tweets, for instance, there’s no point in loading them all the time, you can set a transient for it and only load every 15 minutes or so.

The great thing about it is that you can cache your entire query on it. So if your blog is updated once a day you can set a transient that expires every 12h or less and you’ll be fine.

4. Know all your feeds

It’s always good to provide your usual feeds to your users, but sometimes we need a little bit more. Actually there are quite a lot of cool feeds that you can use:

  • Main – site.com/feed
  • Main comments – site.com/comments/feed
  • Post comments – site.com/post-name/feed
  • Categories & tags – site.com/category/categoryname/feed or site.com/tag/tagname/feed
  • You can also include / exclude categories like this – site.com/?cat=42,25,17&feed=rss2 or this site.com/?cat=-123&feed=rss2
  • Author – site.com/author/authorname/feed/
  • Search – site.com/?s=searchterm&feed=rss2
  • Custom Post Type – site.com/feed/?post_type=yourposttype
  • Custom Taxonomy – site.com/feed/?post_type=custom_post_type_name&taxonomy_name=taxonomy

5. How to add featured image in your feed

8 Things Every WordPress Developer Should KnowImage from Dash

This one is quite simple but gives a good final result (especially if your users are running a nice RSS reader like feedly that displays the main images). This code will do it (in your functions file):


function featured_image_in_feed( $content ) {

    global $post;

    if( is_feed() ) {

        if ( has_post_thumbnail( $post->ID ) ){

            $output = get_the_post_thumbnail( $post->ID, 'medium', array( 'style' => 'float:right; margin:0 0 10px 10px;' ) );

            $content = $output . $content;

        }

    }

    return $content;

}

add_filter( 'the_content', 'featured_image_in_feed' );

Source: WordPress: Add featured image to RSS feed/

6. Optimize your DB once a while

You can use either a plugin or manually but it’s always good to optimize your MySql tables often (at least once or twice a month) so you’ll ensure that your queries are as good as they can be, and will reduce your DB size

7. Enable GZIP

Imagine how great would it be if you could compress your site files before sending them to the user? Well, with server-side GZIP you can do that. And it’s fairly simple, you can add this snippet to your .htaccess file and you’re good to go:


#Gzip

<ifmodule mod_deflate.c>

AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript text/javascript

</ifmodule>

#End Gzip

Source: Enabling Gzip in .Htaccess

8. There’s a plugin for that

Even if you are not a developer you could improve your site performance with caching plugins, DB optimization plugins, CSS and JS minifying plugins.

About the Author

A web designer and entrepreneur from Itajubá (MG), Brasil. Just loves writing about obscure topics and doing some cool stuff. Get in touch at @roch_br.

14 Comments

  1. Arunshory
    June 4, 2013

    It must need tips for wordpress developers. These tips are helpful to solve for everyday issues.

    Reply
    • Rochester
      June 4, 2013

      Thanks for your comment, Arunshory.

      Indeed, they can help us a lot!

      Reply
  2. Andre Smith
    June 4, 2013

    I am currently in the process of opening a web design and tech support company. So I can use all the tips on web design that I can get . Thanks for the tips especially since WordPress is a major tool that I use.

    Reply
    • Rochester
      June 4, 2013

      Hi Andre!

      I’m glad I could help!

      -roch

      Reply
  3. AWKM
    June 4, 2013

    “8. There’s a plugin for that”

    The CURSE of wordpress installs. Filled to the roof with plugins for everything and anything.

    This tip should actually read “There’s a plugin for that but you probably shouldn’t use it unless it’s absolutely necessary”

    Reply
    • Rochester
      June 4, 2013

      AWKM,

      Indeed! Most common issues can be solved with code snippets, but for “regular” users, editing a simple code line is a nightmare, so it’s always good to know that at least there is an easier way.

      -roch

      Reply
  4. Paul
    June 4, 2013

    Theme features http://codex.wordpress.org/Theme_Features and theme customization api https://codex.wordpress.org/Theme_Customization_API are other things that I think all wordpress developers need to know about.

    Reply
    • Rochester
      June 4, 2013

      Hi Paul,

      Thanks for your comment! We really should talk about this later, those are important tools for devs.

      -roch

      Reply
  5. Web Designer Help
    June 4, 2013

    It will be great if you can share some of the plugin names with us so we can test them out…

    Reply
    • Rochester
      June 4, 2013

      Do you have anything specific in mind? Probably you can find pretty much anything you want searching at google or WP’s repo..

      -roch

      Reply
  6. Froi
    June 5, 2013

    Great post, I’m starting to use WordPress a lot for my clients – I can use all the tips and tricks I can find specially for making WordPress to do the things that my clients wants it to do.

    Thanks for sharing your knowledge.

    Reply
  7. Oscar
    June 5, 2013

    They are surely the basic tips we wordpress developers should know. You can’t imagine that I do not cache my stuff, but i’m glad I came to this blog. Kudos and nice job anyway.

    Reply
  8. Sheldon Blake
    June 10, 2013

    Great post. I use WP a lot and i’m always on the hunt for new tips and tricks of optimizing my sites. Thanks

    Reply
  9. Bashir Ahmed
    September 26, 2013

    Hi Rochester,

    Great post for WordPress developers. I’m not WordPress developer but I’m trying to learn the skills for my own blogs.

    I liked the cache plugin tip to improve the site performance.

    Thanks for sharing!

    Reply

Leave a Reply