Forked from wpmudev-sls/hummingbird-minify-oom-fix.php
Created
July 16, 2023 23:31
-
-
Save alfredo-wpmudev/91062ea9fb7533cddc2790d3cd5ccaac to your computer and use it in GitHub Desktop.
[Hummingbird Pro] Fix OOM error when a huge amount of wphb_minify_group posts are present
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: [Hummingbird Pro] Fix OOM error when a huge amount of wphb_minify_group posts are present | |
* Description: Limits the query that retrieves the minify groups to 10 to prevent an excessive resource usage on the server | |
* Author: Anderson Salas @ WPMUDEV | |
* Task: SLS-4065, SLS-4071 | |
* Author URI: https://premium.wpmudev.org | |
* License: GPLv2 or later | |
*/ | |
/* | |
It is important to determine that the problem is actually caused by the amount wphb_minify_group posts in database. | |
The symptoms are the following: | |
- General outgaes | |
- Impossibility to open the HB Dashboard, the AO page or the Plugin Health page | |
- Impossibility to deactivate the plugin | |
- This PHP error: "Fatal error: Allowed memory size of 805306368 bytes exhausted (tried to allocate 757760 bytes) in /wp-includes/wp-db.php on line 2135" | |
The definitive way to check this is by running a database query: | |
SELECT count(*) FROM wp_posts WHERE post_type = 'wphb_minify_group'; | |
*/ | |
add_action( 'plugins_loaded', function() { | |
if ( ! defined( 'WPHB_VERSION' ) ) { | |
return; // Hummingbird is not installed/enabled. | |
} | |
add_action( 'pre_get_posts', function( $query ) { | |
if ( ! empty( $query->query['post_type'] ) && 'wphb_minify_group' === $query->query['post_type'] ) { | |
if ( -1 === $query->query['posts_per_page'] ) { | |
$query->query['posts_per_page'] = 10; | |
$query->query_vars['posts_per_page'] = 10; | |
} | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment