Get the time it takes to run a given function in PHP

To get the time in microsecond you will need to use microtime() instead of time().

You will need microtime() to return a float so set $getAsFloat to true like this:

microtime(true);

Most of the function are really fast so to get something measurable you might need to run them several thousands time within a loop:

$start=microtime(true);
for( $i=0;$i<100000;$i++){
  //the function you want to test
}
$end=microtime(true)
$total_runtime=$end-$start;

You might want to use this method to compare two functions and verify which one is the fastest. You can then compare the time it took to run each of your function and pick the smallest one.

How to remove jquery.migrate.js in wordpress

Jquery.migrate is used to ensure compatibility with older jquery code using deprecated function.

In case your jquery is all shiny and new and you don’t use deprecated functions, then you can get the reed of jquery migrate and have one less file to load. As usual, we will do that by adding a function in the functions.php file of your theme:

function remove_jquery_migrate($scripts)
{
    if (!is_admin() && isset($scripts->registered['jquery'])) {
        $script = $scripts->registered['jquery'];
        if ($script->deps) { 
            // Check whether the script has any dependencies     
            $script->deps = array_diff($script->deps, array('jquery-migrate')); 
            }
      }
 } 
 add_action('wp_default_scripts', 'remove_jquery_migrate'); 

Display wp plugins script and style handle

If you are serious about your site optimization, there will be a point where you want to control all these additional scripts and stylesheets your plugins are loading on your site.

To do so you will need to know the handle that is used to identified the said stylesheets and and scripts.

One easy way is to have it displayed front end in your test environment.

Add the following code to your theme function.php:

function get_enqueued_scripts () {

    $scripts = wp_scripts();

    echo 'Enqueued scripts:
';

    foreach( $scripts->queue as $handle ) :

        echo $handle.' | ';

    endforeach;

    echo '
';

}
 add_action( 'wp_head', 'get_enqueued_scripts', 9999 ); 

And to get the handles of the enqueued style sheets:

function get_enqueued_styles () {

    $styles = wp_styles();

    echo 'Enqueued styles:
';

    foreach( $styles->queue as $handle ) :

        echo $handle.' | ';

    endforeach;

}
 add_action( 'wp_head', 'get_enqueued_styles', 9999); 

In case you don’t have a test environment (really?) you can display this info in the console, don’t forget to disable that after you are done optimizing your site.

How to set wordpress admin in a different language than the front end language

It may happen that you work with a woocommerce site that is set in a different language than your native tongue which can make navigating the wordpress admin panel very confusing depending on your level of command of the said language.

Solution: Fortunately enough the solution is realyy simple. 

Go to users

click on your user name.

under language, select the language that fit your need

Save and you’re done! WordPress is now the desired language only for you. All other user see the language as set as in Settings -> General -> Site language

Use JCH optimize with woocommerce

Issue:

JCH optimize might cause issue with the checkout page with certain payment plugin. A loading circle appear and don’t seem to go away anytime soon.

Solution:

To sort that out simply exclude some files related to the checkout page from optimization in jch optimization dashboard. I have excluded …/assets/css/frontend.css from Exclude these CSS files and our payment plugin css: 
checkout-maksukavat.css in  Exclude CSS files from these plugins

Serve webp and JPEG 2000 in wordpress

Last time we saw different methods to serve webp or JPEG 2000 pictures to our users. Now let’s see how we can implement this in wordpress

We might work something out thanks to our previous work on changing the wrapping of thumbnails: https://blog.seobytes.eu/wordpress/theme-customization/modify-thumbnails-wrapping-in-woocommerce-product-loop/

We shall also do the same operation on any other images be it post, product pages, background images, header images etc…

But let’s start with the thumbnails: the following code assume that our webp version of the images are in the same folder and have the same name than the jpg version, beside the extension.

remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10);
add_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10);

if ( ! function_exists( 'woocommerce_template_loop_product_thumbnail' ) ) {
function woocommerce_template_loop_product_thumbnail() {
echo woocommerce_get_product_thumbnail();
} 
}
if ( ! function_exists( 'woocommerce_get_product_thumbnail' ) ) { 
function woocommerce_get_product_thumbnail( $size = 'shop_catalog', $placeholder_width = 0, $placeholder_height = 0 ) {
global $post, $woocommerce;
$output = '</div class="imagewrapper"></picture>'; if ( has_post_thumbnail() ) { 
$image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), $size);
$image_url =$image_url[0];
$webp_url= str_replace('jpg', 'webp', $image_url);
$output .= '<source srcset="'.$webp_url.'">';
$output .='<img class="lozad" data-src="'.$image_url.'" alt="">';
} 
$output .= '</picture ></div>';
return $output;
}
} 

With further research and the sudden realization that there might be a plugin already doing this (although, where is the fun in that?) I found the following plugins:

Webp express which is a free plugin

and Optimus which comes with a fee as far as webp conversion and serving is concerned.

While testing Webp express, it occurs that my virtual hosting does not allow for automatic conversion*. So I have written an article on how to batch convert pictures into webp

The author of Webp Express also made a php script to serve webp to supporting browser with .htaccess redirect which is a pretty elegant solution. Check the github page of the project

*My web host implemented imagick on the server after I inquired about it :) . No need to batch convert but it seems Imagick does not handle files containing a space in their name.

Remove emojis script and style from wordpress

Dequeue emojis related script in your theme or child-theme function.php by adding this few lines of code:

remove_action( 'wp_head', 'print_emoji_detection_script', 7 ); 
remove_action( 'admin_print_scripts', 'print_emoji_detection_script' ); 
remove_action( 'wp_print_styles', 'print_emoji_styles' ); 
remove_action( 'admin_print_styles', 'print_emoji_styles' );

What are the http call to wp-cron.php in wordpress server logs?

The apache log of your wordpress server shows:

"POST /wp-cron.php?doing_wp_cron=1542232954.9134418964385986328125 HTTP/1.1" 200 3305 "https://www.example.com/wp-cron.php?doing_wp_cron=1542232954.9134418964385986328125" "WordPress/4.9.8;

WordPress make a http call to wp-cron.php at every site visit. This php script takes care for update check up and for schedule article publication.

WP-Cron works by: on every page load, a list of scheduled tasks is checked to see what needs to be run. Any tasks scheduled to be run will be run during that page load. WP-Cron does not run constantly as the system cron does; it is only triggered on page load.

Source: https://developer.wordpress.org/plugins/cron/

If you want to make sure some tasks are done at a certain time for sure, you can setup a cron task on your server and disable the call to wp-cron.php in the wp-config file.

There is an excellent article from wordpress developper site explaining all the details.

Note: Some sites report that if you use cloudflare or caching plugins, cron may “never run” and therefore recommend the afore-mentioned method. This assertion needs to be verified. Conclusion will be added here.

Hide wordpress error messages and warning

If error messages and warning are essential during development. They are not welcome in your production environment.

We will have a look on how to hide warnings and error messages from the public while keeping this information accessible to the admin (see https://codex.wordpress.org/Editing_wp-config.php#Debug ) .

Method one: wp-config.php  and php.ini

First we will have a look at the wp-config.php file. For this you will need an ftp access to your site.

https://codex.wordpress.org/Editing_wp-config.php#Debug

You will also need access to php.ini

Method 2: wp-config.php only

Replace:

define('WP_DEBUG', false);

by:
ini_set('log_errors','On'); ini_set('display_errors','Off');
ini_set('error_reporting', E_ALL );
define('WP_DEBUG', false);
define('WP_DEBUG_LOG', true);
define('WP_DEBUG_DISPLAY', false);
Source: https://www.templatemonster.com/help/wordpress-how-to-hide-php-warnings-and-notices.html

With this method nothing is logged (at least on my server).

For a debug friendly version use Method 3:

 // Enable WP_DEBUG mode
define( 'WP_DEBUG', true );

// Enable Debug logging to the /wp-content/debug.log file
define( 'WP_DEBUG_LOG', true );

// Disable display of errors and warnings 
define( 'WP_DEBUG_DISPLAY', false );
@ini_set( 'display_errors', 0 );

// Use dev versions of core JS and CSS files (only needed if you are modifying these core files)
define( 'SCRIPT_DEBUG', true );

Source: https://codex.wordpress.org/Debugging_in_WordPress

Message are off and error are logged. The draw back of this methods is that the logs are public at /wp-content/debug.log

W3 Total Cache plugin doesn’t detect apache modules

Issue description: “After running a compatibility check in W3 total cache on WordPress, the following apache modules are not detected:

mod_deflate: Not detected (required for disk enhanced Page Cache and Browser Cache)
mod_env: Not detected (required forhanced Page Cache and Browser Cache)
mod_expires: Not detected (required for disk enhanced Page Cache and Browser Cache)
mod_filter: Not detected (required for disk enhanced Page Cache and Browser Cache)
mod_ext_filter: Not detected (required for disk enhanced Page Cache and Browser Cache)
mod_headers: Not detected (required for disk enhanced Page Cache and Browser Cache)
mod_mime: Not detected (required for disk enhanced Page Cache and Browser Cache)
mod_rewrite: Not detected (required for disk enhanced Page Cache and Browser Cache)
mod_setenvif: Not detected (required for disk enhanced Page Cache and Browser Cache)

source: https://support.plesk.com/hc/en-us/articles/115005108854-W3-Total-Cache-plugin-shows-apache-modules-as-not-detected-

According to this source W3 Total Cache cannot detect php module if mod_php is disabled on an apache server. They also indicate that “mod_php is disabled […] by default because it is not secure (mod_php is outdated).

Solution:

1- Verify that indeed this services are correctly activated.  In order to do so run phpinfo() (see “how to run phpinfo()” )

2- Once confirmed, the error messages can therefore be ignore.

w3 total cache compatibility check not detected