When WordPress caching system makes things slower

Last week has launch time for my/Samsys latest and last plugin, an internal messaging system for WordPress websites, developed to work together a custom WordPress product for Pre-schools.

The plugin allows registered users to exchange messages between them using a front-end interface ( non-admin users) and a back-end interface on the wp-admin (users with administrative access).

Everything was going smoothly until I started testing on a live website with more than 600 registered users. The front-end ran smoothly but the back-end became painfully slow with over 15 seconds of load time.

After some investigation, I was able to pinpoint the “problem”, multiple databases calls for “update_meta_cache” each call only took milliseconds to process but there were thousands of them and it was slowing down the back-end performance.

What’s “update_meta_cache” ?

As it’s name suggests, this functions updated metadata cache on a specified object and runs on everytime you fetch metada ftom the database.

“update_meta_cache” is very handy on the front-end where exists a caching mechanism in place but in the back-end, no cache is active and it slowed down the plugin performance painfully. The solution has relatively simple, instead of fetching the users every time I needed them listed I created an object with all the needed user data, saved it using Transients API, and every time a new user it’s added to the website the object it’s re-created with the new data.

 

How to install a php linter (WordPress standards) in Windows 10 and Sublime Text 3

For some time now, I’ve been thinking that I needed to get a php linter in my workflow.

This little addition will decrease debugging time, letting me know instantly if there’s some kind of syntax mistake, and also with WordPress standards active will instruct me to follow all the coding standards.

After a little chat with some friends yesterday night and seeing once again linting in action, I said to myself “Today I’ll get this thing working on Windows”.

After some experiments and reading I’ve encountered the easiest and safest way to do this install, and finally get linting working on my setup (Windows + Sublime Text 3) these are the steps:

Install PHP

  • Download the binaries from http://windows.php.net/download/ (I’ve selected the VC11 x86 Thread Safe version for my setup.)
  • Unzip the downloaded folder to c:\php
  • Activate some basic php modules:
    • Copy php.ini-production to php.ini
    • Uncomment the following lines:
      • extension_dir = “ext”
      • extension=php_bz2.dll
      • extension=php_curl.dll
      • extension=php_fileinfo.dll
      • extension=php_gd2.dll
      • extension=php_intl.dll
      • extension=php_mbstring.dll
      • extension=php_exif.dll
      • extension=php_openssl.dll
      • extension=php_pdo_mysql.dll
      • extension=php_pdo_sqlite.dll

Install Composer

Install PHP Code Sniffer using Composer

  • On your bash console run the following comand:
  • composer global require "squizlabs/php_codesniffer=*"

Install WordPress PHPCS standards

  • On console discover the path to the linter executable
  • which phpcs
  • Go to the PHPCS directory, it should be something like: C:\Users\{your-user}\AppData\Roaming\Composer\vendor\bin\
  • Clone WordPress standards
  • git clone -b master https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards.git wpcs
  • Add WordPress to PHPCS configuration

  • phpcs --config-set installed_paths C:\Users\{your-user}\AppData\Roaming\Composer\vendor\bin\wpcs
  • Set the default Standard as WordPress
  • phpcs --config-set default_standard WordPress

Install Linter on Sublime Text 3

The last steps are installing the linter packages on Sublime Text 3, for this open your Package Control and install the following packages:

  1. SublimeLinter
  2. SublimeLinter-phpcs

Next, you’ll need to restart sublime and setup the linter configuration, it’s fairly simple, just copy the file in “Preferences/Package Settings/SublimeLinter/Settings – Default” to “Preferences/Package Settings/SublimeLinter/Settings – User” and edit the phpcs linter configuration setting the standard as WordPress:

"phpcs": {
    "standard": "WordPress"
 },

4 Useful WooCommerce Functions

A fast and reliable way to change WooCommerce behaviours

While working on WooCommerce websites, I’ve found that I was using (most of the times) 4 simple functions that allowed me to have a superior control on the created e-commerce solution.

 

In this post you’ll find functions for:

  • Add WooCommerce CSS classes to any template you want
  • Change WooCommerce products per page and product columns on listings
  • Add BCC to all emails sent by WooCommerce
  • Remove WooCommerce hooked actions and create your own function with it

 

Add WooCommerce CSS classes to a page template or a single template from custom post type


add_filter('body_class','add_this_classes');

function add_this_classes($classes) {
        //if is single from CPT "post_type" or page template "template.php"
	if(is_singular('post_type') || is_page_template('template.php')){

		// add 'class-name' to the $classes array
		$classes[] = 'woocommerce';
		$classes[] = 'woocommerce-page';

	}

	// return the $classes array
	return $classes;
}

 

Change WooCommerce products per page and columns


//Change products per page to 20
add_filter( 'loop_shop_per_page', create_function( '$cols', 'return 20;' ), 20 );
add_filter('loop_shop_columns', 'loop_columns');

if (!function_exists('loop_columns')) {

	function loop_columns() {

		return 8; //change to 8 columns

	}

}

 

Add BCC in all emails sent by WooCommerce


add_filter( 'woocommerce_email_headers', 'add_bcc_all_emails', 10, 2);

function add_bcc_all_emails($headers, $object) {

	$headers = array();
	$headers[] = 'Bcc: Name <me@email.com>';
	$headers[] = 'Content-Type: text/html';

	return $headers;
}

 

Remove WooCommerce hooked action and use the function from it

//Remove add to cart from single product summary
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );

woocommerce_template_single_add_to_cart();