Speed Up Large WordPress & WooCommerce Website Back-End

Last week while trying to find ways to improve KelbyOne’s Website speed, I came across a particular query being ran on the back-end that was responsible for slowing it down. Every time you tried loading an Order, Course, Lesson, Subscription or anything else, that query was responsible for almost 80% of the time it took to load.

The query was discovered using the latest version of Query Monitor. What did the query do, it goes through the whole postmeta table and tries to find every different meta_key found for a particular post_type. Needless to say if you have a large postmeta table with millions of records, this query will take a long time to run.

Results from the query populate the Custom Fields metabox select menu so that you can easily add new custom fields with a few clicks. Keep in mind that if you add this code to your Website, the select menu will not be populated anymore. Also, any custom fields added to the post will still show in Custom Fields metabox and you’ll have the “Add New Custom Field” interface to add any fields if you need to.

I was able to reduce page load times from around 27 seconds down to 4-5 seconds. How is this beneficial? Simple. The less time the server is busy running the query the more resources available to your visitors. Also, if you have a lot of people going in the back-end for things like Customer Service, they’ll thank you, too.

Enough rambling, here is the code.

LearnDash Zapier Integration: Sending Additional User Information

I was recently hired at Codeable to write a plugin which would allow the LearnDash Zapier Integration plugin to pass additional user information with every call made to Zapier. This information needed to happen whenever global user even happened, like completing a quiz, for example. Upon reviewing the plugin, I noticed there was no filter that would allow me to pass in this additional data or an action to alter it’s default behavior.

Luckily the plugin was using WordPress’ HTTP API and I knew if I could listen to every request being made to an external site, then I could check whether or not the call was being made to Zapier and add my data there. This is exactly what I ended up doing. I’m posting this hoping that it will help someone else who needs to do this and you should get in touch with LearnDash like I did in order to ask for a filter to be added to the already existing plugin. It wouldn’t take more than 5 minutes to add a filter and open the request so that you can pass your own data.

Below is the plugin I wrote to pass a single field the User object being sent to Zapier. You can use the same technique and add as many as you’d like.

There you have it, nice and simple.

Faster AJax Calls In WordPress By Using The AjaxFlow Plugin

Is it possible to achieve faster Ajax Calls in WordPress? You bet it is possible and you can start doing it right now with very little edits to the code you already have in place. We are going to be using a little plugin called AjaxFlow to speed it up, but first let’s go over the regular, slow way of doing Ajax Calls in WordPress.

Meet Ajaxflow

By installing this plugin in your Website, your Ajax calls will be so fast you’ll think it’s sorcery, but it is not, I think it’s not. All kidding aside, the plugin basically tells WordPress to load the bare minimum instead of loading the whole WordPress environment, thus speeding everything up and resulting in much faster Ajax calls. I’m talking calls that would normally would take 3-5 seconds on a good server down to 100-300 milliseconds.

How To Use Use AjaxFlow

Using AjaxFlow is super easy, and if you are already using regular Ajax calls within WordPress using wp_ajax_nopriv_$action and wp_ajax_$action, converting to AjaxFlow can be done in minutes.

What Are The Differences?

  • Ajax calls need to be made to /ajaxflow/$action, meaning that /wp-admin/admin-ajax.php is no longer used.
  • Passing the action parameter in your Ajax request is no longer necessary since we include the action in the call endpoint as explained above
  • ajaxflow_nopriv_$action and ajaxflow_$action must be used in your PHP code instead of using wp_ajax_nopriv_$action and wp_ajax_$action

AjaxFlow Example

If for some reason you get an error message other than zero saying your call $action doesn’t exists, then it means you have forgotten to add the Ajax handler in the back-end or you didn’t add it properly since it can’t find it.

That’s it, give it a try and watch your Ajax calls fly.

Connecting WooCommerce To Salesforce: OAuth2 Authorization

Along with learning Typescript in the fast few months, I’ve also been putting a lot of time an energy into learning Salesforce and the Apex language. So far it has been an amazing journey and my end goal for learning Salesforce is so that I can write REST APIs that would synchronize WooCommerce and Salesforce seamlessly.

With that purpose in mind, I realized doing something like this is no easy task or something you can write in a day or two and never have to touch it again, specially since the whole ecosystem (WordPress, WooCommerce, Salesforce) gets updated very frequent and things change all the time. I’ve taken the approach of dividing this humongous task into smaller modules that would help me accomplish this in faster and more maintainable way. Being able to re-use the components and sort of add or delete them as needed is what I’m after and the first step would be to establish a connection with Salesforce so that I can begin sending my Requests.

Since this would be tied to WooCommerce on this side, I figured I would do it the WordPress/WooCommerce way so I wrote a little plugin that integrates with WooCommerce and handles the saving of all the data received from Salesforce after the initial oAuth2 authorization and saves it to the database for later use. Note that this part doesn’t include the option the revoke the token or to send the refresh token in order to obtain a new access token once the old one has expired.

Register Our Integration With WooCommerce

Let’s start coding and register our registration with WooCommerce so that it appears under the WooCommerce -> Settings -> Integrations tab. This file’s sole purpose is to help WooCommerce know this is an integration and to load other PHP Classes we are going to be using. The actual fields, settings and any other logic will be in another file.

Integration Fields Using WooCommerce’s Settings API

Everything is pretty standard, just a few fields to save our the values we get back from Salesforce.

  • SF App Consumer Key
  • SF App Consumer Secret
  • SF Login URI
  • SF oAuth2 Token
    Salesforce Token (Not actually used to communicate back and forth but it was used for obtaining the Access Token
  • SF Access Token
    Access token sent with every request for authenticating purposes.
  • SF Refresh Token
    Refresh token used whenever the Access Token needs to be renewed/refreshed
  • SF Instance URI
    All REST Request will be send to this base Uri.
  • Debug Log
    Log events such as API requests

Register Custom WordPress Endpoints

Next we register 2 endpoints which are used in the 2 step process. What follows takes care of registering two endpoints, oauth2 and oauth2-step2. It will also add some quick and dirty logic to overriding what each of these endpoints display. These templates are called authorization-step-1.php and authorization-step-2.php.

Endpoint Templates

Finally, these are the layouts for those 2 endpoints.

Download WooCommerce To Salesforce Integration: Initial Integration

WooCommerce To Salesforce Integration: Initial Integration

Mastering WooCommerce Hooks: Setting Minimum And Maximum Order Quantities

I’ve been thinking about continuing my Mastering WooCommerce Hooks series here on my personal Website instead of another Website. It has been a long time since I write anything related to WooCommerce and WordPress so hopefully this series will help me do just that, about one post every week or so.

This week’s article we’ll be building a simple interface on the Add New Product page where you’ll be able to set Minimum and Maximum per order quantities. Not only we’ll be implementing these fields, there is going to be a third one which will control the “step” or the amount to be incremented everytime someone clicks on the little up arrow on the product description page.

The image below shows exactly what it will look on the back-end of the site on the Add New Product page.


Building the WordPress/WooCommerce Plugin

We are going to be developing this as a plugin so that we can re-use it if we wanted to. Below is the code for the plugin and the first part of the implementation which happens to be registering the input fields to the Product data section you normally see when setting up a product in WooCommerce.

Registering the Input Fields

Go ahead and activate the plugin now and navigate to the Add New Product screen or simply try to edit one of your existing products.

Saving These New Product Fields

Add a new method to our class (remember we are building this as a WordPress plugin using PHP classes) so that we can save the data when present or delete it when not needed.

Making It All Work

Now here comes the fun part. Making it all work on the front-end through the product description page. Let’s add another hook to the init() method and then implement the function which is going to change the default values on those indivudual products that you have set a minimum, maximum or step value.

Fun Part

Here is the final code with comments and ready to be dropped into the plugins folder.