[email protected]

8 Universal Tips & Snippets for WordPress Developers

WordPress is a wonderful CMS platform and blogging tool loved by bloggers all across the globe. It is easy-to-understand and if you know PHP quite well, then you can do a lot more with WordPress through its available open source code. But it’s not that easy, if you are not a seasoned WordPress developer.

8 Universal Tips & Snippets for WordPress Developers

When delving in 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, but sometimes it becomes painstaking to just 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 behind not using it is jQuery code that will run at the back the operations as it uses WP_Query. With WP_Query you will require deep knowledge of programming codes to clean the mess. In a simpler way, when you will call query_post for your index.php file the 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.
  • In case you don’t require any loop, then you may use get_posts() where you are focusing on posts rather than loops.
  • If you need to modify the main running loop, then 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

Enqueue Your Scripts & Styles

Loading the external files is what you always do to create Plugins, themes or customizing them. But calling a JS library twice may result in a 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 of it. The 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 stuffs to be stored for temporary period. There is many a time when receive latest tweets, but there is 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 that you can keep a temporary record of your entire query. So set a transient updating your blog.

Stay Updated with All Your Feeds

Stay Updated with All Your Feeds

If you are thinking 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 really exciting one. So here is a function file code that will do the magic in creating attractive images for feeds.

1.function featured_image_in_feed( $content ) {
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 in a month is a good 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 plug-in to get rid of sluggish performance.

Get GZIP Enabled

Want to send compressed site files to your users? Then server side GZIP is a solution which hardly takes no time and it is fairly simple as well. What all you have to do is to copy the code snippet below to your .htacess file and you are ready with compress file folder.

1. #Gzip
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

WordPress plugins

Well forcefully you don’t need to be a developer in order to attain flawless site performance, simply obtain DB optimization Plugin, caching Plugin and JS minifying Plugin to rock your WordPress platform.

DavidMeyer
David Meyer is professional blogger working currently with CSSChopper - a champion in PSD to HTML conversion & integration service. The company renders complete Wordpress solutions from ‘PSD to Wordpress conversion’ to custom Wordpress development.
More articles by DavidMeyer

Comments are closed.

Related Posts