8 Universal Tips & Snippets for WordPress Developers
WordPress is a beautiful CMS platform and blogging tool loved by bloggers all across the globe. It is easy to understand, and if you know PHP quite well, you can do much more with WordPress through its open-source code. But it’s not that easy if you are not a seasoned WordPress developer.
When delving into core coding, then there are chances that you may encounter stuck into several issues. Though you may even find many sources to resolve the issues, sometimes it becomes painstaking just to fix complex issues. Well, let’s explore some of the common tips and snippets to overcome your frequent issues.
Try Avoiding the Use of query_posts()
Most novices use query_posts, but the fact is that you should try to avoid it. The reason for not using it is the jQuery code that will run at the back of the operations using WP_Query. With WP_Query you will require deep knowledge of programming codes to clean the mess. In a simpler way, when you call query_post for your index.php file, WordPress will load the key query as soon as you call the template files. So you are actually calling two queries for the background queries, which together make it 8 (as each WP_Query comprises 4 queries, i.e. call metadata, call posts, call terms and count posts).
Here is the solution:
- Whenever you want to use multiple loops like secondary loops, sidebar loops etc. in your template page, then try using WP_Query function.
- If you don’t require any loop, then you may use get_posts() where you focus on posts rather than loops.
- If you need to modify the main running loop, just go for this pre_get_posts filter. This filter will help you to modify the WordPress loop directly despite getting a new DB query.
Enqueue Your Scripts & Styles
Loading the external files is what you always do to create Plugins, and themes or customise them. But calling a JS library twice may result in site trouble. So the simple solution is wp_enqueue_script function, which will load a script or library to ensure that you are loading just one copy. A similar concept is considered ideal for loading standard styles.
Cache Is Must
Being a Plugin developer, it is essential to know about transients API as they allow your stuff to be stored for a temporary period. There is many a time when receiving the latest tweets, but there is a reason that you will load them each time. Simply enable the transient for that, and it will get loaded in every 20 minutes or so. The best thing you can do with the cache is you can keep a temporary record of your entire query. So set a transient updating your blog.
Stay Updated with All Your Feeds
If you are thinking about how to provide feeds to your end-users, then below you’ll find some really cool feeds to look upon.
- Main comments – site.com/comments/feed
- Author – site.com/author/authorname/feed/
- Main – site.com/feed
- Categories & tags – site.com/category/categoryname/feed or site.com/tag/tagname/feed
- Search – site.com/?s=searchterm&feed=rss2
- Custom post type – site.com/feed/?post_type=yourposttype
- Post comments – site.com/post-name/feed
- Custom taxonomy – site.com/feed/?post_type=custom_post_type_name&taxonomy_name=taxonomy
- You can also include/exclude categories like this – site.com/?cat=42,25,17&feed=rss2 or this site.com/?cat=-123&feed=rss2
Adding Images to Your Feed
A feed without images can be a bit monotonous to read. Adding images is a great way to turn up your feed into a really exciting one. So here is a function file code that will do the magic in creating attractive images for feeds.
2. global $post;
3. if( is_feed() ) {
4. if ( has_post_thumbnail( $post->ID ) ){
5. $output = get_the_post_thumbnail( $post->ID, ‘medium’, array( ‘style’ => ‘float:right; margin:0 0 10px 10px;’ ) );
6. $content = $output . $content;
7. }
8. }
9. return $content;
10. }
11. add_filter( ‘the_content’, ‘featured_image_in_feed’ );
Source: Hugh Lashbrooke
Optimize Your Database
Optimizing your database (MySQL tables) once or twice a month is an excellent way to boost the performance of the database so that the queries run smoothly as the way you want. For this, you can do this manually or even take the help of a plug-in to get rid of sluggish performance.
Get GZIP Enabled
Want to send compressed site files to your users? The server-side GZIP is a solution that hardly takes time and is pretty simple as well. All you have to do is to copy the code snippet below to your .htacess file and you are ready with a compressed file folder.
2. <ifmodule mod_deflate.c>
3. AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript text/javascript
4. </ifmodule>
5. #End Gzip
Source: Sal’s Code
A Plugin for All
Well, forcefully, you don’t need to be a developer in order to attain flawless site performance. Simply obtain DB optimization Plugins, caching plugins, Google Snippet Tool By Sitechecker, and JS minifying Plugins to rock your WordPress platform.