Affari

Speeding up a WordPress based iOS app

February 16, 2015
James Herriott

Speeding up a WordPress based iOS application

The Problem

Speeding up a WordPress based iOS application is a topic becoming more popular and necessary. With the growing popularity of WordPress, you need to know how to speed up WP based iOS applications.

When building an iOS application that uses webviews to load data, speed is crucial. Unfortunately, one of the biggest complaints with WordPress how slow it is. Out of the box, WordPress simply cannot handle the need-for-speed an iOS app requires. That is why most WordPress based applications separate out the front end views using some other framework such as Laravel or Node.JS. But what if you want to build an iOS application that runs entirely on WordPress, templates and all? Fortunately there are several tricks and tools that can make it work.

The Solution

1: wp_cache_get() and wp_cache_set()

These little nuggets allow you to tailor each query’s caching specifically to your needs. Say you have a dashboard that doesn’t need to be real time but does update every hour or so. The user might use this dashboard to reach several parts of the app, returning to it five or six times in a matter of minutes. Waiting even half a second each time they return to this page will quickly get tedious and the user might give up the app altogether. If you set a cache the first time the user hits this dashboard, the next time the user returns they’ll be served a cached version cutting out the time it takes WordPress to loop through your query. Here’s how it works:

$cache_key = 'post';
$posts = wp_cache_get($cache_key);
if ($posts === false) {
  $post = new WP_Query(array(
    'post_type' => 'post',
    'posts_per_page' => 5
  ));
  wp_cache_set($cache_key, $posts, '', 3600);
}
if ($posts->have_posts()) {

In this example we’ve set the cache to expire every hour (3600 seconds). Also, be sure to always use a unique cache key every time you do this! The above example will only work for something universal. In the case of a user dashboard it might be good to throw the user’s ID into the cache key or some other unique modifier. What if you want the cache to clear every time the user does a specific action, such as accepting a friend request? You can always use this function to manually clear a cache:

wp_cache_delete('post');

2: AJAX lazy-load

AJAX is a way to pull content into an already loaded page. You see this concept used in the most popular mobile apps such as Instagram and Twitter. As you scroll down the page new content gets loaded in dynamically. WordPress pagination makes it fairly easy to accomplish this. If you want an explanation on how to implement AJAX lazy-load with WordPress pagination check out this blog post. Here is the most important part of the code:

<script>
  jQuery(function(){
    var page = 2;
    jQuery('#lazyloadwrapper').append(jQuery('<div>').load('YOURPAGEURL/?paged=' + page + ' .page', function() {
      page++;
    });
  });
</script>

3: Simplify Your Queries!

If you’re writing a custom query involving multiple custom post types and checks custom taxonomies there’s a chance you’ve overcomplicated things. Though your code might be working it may be spending precious time querying something that doesn’t serve a vital purpose. If there’s an opportunity to remove a query, do it!

4: Use a Caching Plugin

W3Total Cache is arguably the best caching plugin out there for WordPress. It works really quickly out of the box. It also provides options so you can tailor caching for your specific needs. Another great caching plugin is WP Super Cache by Automattic.

5: Minify and Combine your CSS and JS files

The plugin W3Total Cache (mentioned above) makes this easy to implement. However, if you’re not a fan of plugins you can always just combine your CSS files into one ‘master.css’ file and JS files into one ‘master.js’ file. Sometimes this can cause things to break so always check the front end after you do this!

6: Try out a Content Delivery Network

A content delivery network serves content from your site to users from a highspeed, and usually geographically closer, server. It also decreases the amount of work your server has to do. You can try this concept out free with Cloudflare. If you want to pay for a high end CDN, Max CDN works great and integrates well with W3Total Cache.

7: Clean Up Your Database

The free plugin WP-Optimize will quickly do the trick. This plugin cleans out your database by removing revisions and other unnecessary data.

8: Compress Your Images!

Images are usually the largest file sizes on WordPress sites. Compressing a 1MB image down to 100KB can make a world of a difference. For JPGs I recommend the free tool JPEGmini. For PNGs you can use TinyPNG. Their website allows free PNG compression and they also sell a handy Photoshop plugin that will allow you to export smaller PNGs seamlessly.

Using these 8 steps, speeding up a WordPress based iOS application will be a much simpler process going forward. This is just one of the many “tips and tricks” we’ve learned along the way, and we are happy to begin sharing them with the development community. Don’t forget to follow us on Facebook, Twitter and Instagram to get updates on new blog posts!

Get more insights to your inbox

Ideas are what make us great,
let’s talk about yours

(202) 643-9322 | hello@affari.co

© 2017 Affari. Made across the USA with love.