Tips & Tricks, Web

Tips For Every WordPress Developer

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

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:

 

Cache your stuff

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.

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

How to add featured image in your feed

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):

 

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

 

mycodetips

Passionate about coding and software development holding 10+ Years of experience including 6+ years of experience on Mobile application Development.

More Posts