This is not a snippet, its more of a small guide on advanced optimization efforts in WordPress/Woocommerce.
WP meta boxes
A possible solution to slow loading post/page/product pages could be a large number of post_meta rows. A customfield box is loaded on every page. To disable this, we can disable this metabox. The DISTINCT function wont be run, which could be slow when you have a lot of post_meta rows.
<?php
function remove_plugin_meta_boxes() {
foreach ( get_post_types( '', 'names' ) as $post_type ) {
remove_meta_box( 'postcustom', $post_type, 'normal' );
remove_meta_box( 'pys_enrich_fields_woo' , $post_type , 'normal' );
remove_meta_box('product-meta-box', $post_type, 'normal');
remove_meta_box('view-meta-box', $post_type, 'normal');
remove_meta_box('video-meta-box', $post_type, 'side');
remove_meta_box('skin-meta-box', $post_type, 'normal');
remove_meta_box('porto-api-engine', $post_type, 'side');
remove_meta_box( 'postcustom', $post_type, 'normal' );
}
}
add_action('do_meta_boxes', 'remove_plugin_meta_boxes');
The first meta is the ID. You can find the ID of a metabox in the inspector tab. its the id attribute. The third parameter is the location.
Dashboard widgets
Disable all dashboard widgets
function wpse_73561_remove_all_dashboard_meta_boxes()
{
global $wp_meta_boxes;
$wp_meta_boxes['dashboard']['normal']['core'] = array();
$wp_meta_boxes['dashboard']['side']['core'] = array();
}
Disable dashboard widgets except woocommerce report
add_action('wp_dashboard_setup', 'wpse_73561_remove_all_dashboard_meta_boxes', 9999 );
function wpse_73561_remove_all_dashboard_meta_boxes()
{
global $wp_meta_boxes;
$new = [];
foreach($wp_meta_boxes['dashboard']['normal']['core'] as $row){
if ($row['id'] == 'woocommerce_dashboard_status' || $row['id'] == 'woocommerce_dashboard_recent_reviews'){
$new[] = $row;
}
}
$wp_meta_boxes['dashboard']['normal']['core'] = $new;
$wp_meta_boxes['dashboard']['side']['core'] = array();
}
Disable columns in overview
<?php
class SWOptimize
{
public $remove_meta_boxes;
public $boxes_to_keep;
public $product_columns_remove;
public $order_columns_remove;
public function __construct()
{
add_action('do_meta_boxes', [$this, 'remove_plugin_meta_boxes'],9999);
add_action('wp_dashboard_setup', [$this, 'remove_all_dashboard_meta_boxes'], 9999);
add_filter('manage_edit-product_columns', [$this, 'remove_product_columns_filter'],9999);
add_filter('manage_edit-shop_order_columns', [$this, 'remove_orders_columns_filter'],9999);
add_filter('wp_revisions_to_keep', [$this, 'disable_post_revisions_for_custom_post_type'], 10, 2);
//WP Rocket preload less resource intensive
add_filter('rocket_preload_pending_jobs_cron_interval', [$this, 'preload_cron_interval']);
add_filter('rocket_preload_pending_jobs_cron_interval', [$this, 'preload_requests_delay']);
add_filter('rocket_preload_pending_jobs_cron_interval', [$this, 'preload_batch_size']);
$this->remove_meta_boxes = [
['postcustom', 'normal'],
['pys_enrich_fields_woo', 'normal'],
['product-meta-box', 'normal'],
['view-meta-box', 'normal'],
['video-meta-box', 'side'],
['skin-meta-box', 'normal'],
['porto-api-engine', 'side'],
];
$this->boxes_to_keep = [
'woocommerce_dashboard_status',
'woocommerce_dashboard_recent_reviews'
];
$this->product_columns_remove = ['ean', 'sku', 'product_tag', 'featured', 'taxonomy-pwb-brand', 'facebook_sync', 'woo_variation_swatches_product_label_settings', 'product_subscriber', 'zettle_synced', 'rank_math_description', 'rank_math_title'];
$this->order_columns_remove = ['billing_address','dhlpwc_delivery_time'];
}
public function remove_plugin_meta_boxes()
{
foreach (get_post_types('', 'names') as $post_type) {
foreach ($this->remove_meta_boxes as $metadata) {
remove_meta_box($metadata[0], $post_type, $metadata[1]);
}
}
}
public function remove_all_dashboard_meta_boxes()
{
global $wp_meta_boxes;
$new = [];
foreach ($wp_meta_boxes['dashboard']['normal']['core'] as $row) {
if (in_array($row['id'], $this->boxes_to_keep)) {
$new[] = $row;
}
}
$wp_meta_boxes['dashboard']['normal']['core'] = $new;
$wp_meta_boxes['dashboard']['side']['core'] = array();
}
public function remove_product_columns_filter($columns)
{
foreach ($this->product_columns_remove as $name) {
if (isset($columns[$name])) {
unset($columns[$name]);
}
}
return $columns;
}
public function remove_orders_columns_filter($columns)
{
foreach ($this->order_columns_remove as $name) {
if (isset($columns[$name])) {
unset($columns[$name]);
}
}
return $columns;
}
/**
* 1) BATCH SIZE
* Change the number of URLs to preload on each batch, 45 is the default.
* A lower value can help the server to work on fewer requests at a time
*/
public function preload_batch_size( $value ) {
// change this value, default is 45 urls:
$value = 20;
return $value;
}
/**
* 2) CRON INTERVAL:
* Set the desired cron interval in seconds
* By setting a higher value the server will have more time to rest between processing batches.
*/
public function preload_cron_interval( $interval ) {
// change this value, default is 60 seconds:
$interval = 120;
return $interval;
}
/**
* 3) DELAY BETWEEN REQUESTS:
* This is the delay between requests. A higher delay will reduce the CPU usage.
* Default is 0.5 seconds (500000 microseconds)
*/
public function preload_requests_delay( $delay_between ) {
// Edit this value, change the number of seconds
$seconds = 0.6;
// finish editing
// All done, don't change this part.
$delay_between = $seconds * 1000000;
return $delay_between;
}
public function disable_post_revisions_for_custom_post_type($num, $post) {
return 0; // Set the number of revisions to 0 for your custom post type
}
}
new SWOptimize();