Wrote a nasty script – WP CLI using PHP

I use WP Smush Pro to compress images in WordPress. But lately the plugin became extremely slow because of too many database queries. The media library takes 10 seconds to open while the plugin is active. But takes only 2 seconds without it. That’s huge..! 🥱

Search in media library became slow, uploading media became slow, anything related to media became slow as duck.

I tried unhooking some of actions & filters of WP Smush but that wasn’t enough. So decided to do this:

$path = "--path=/full/path/to/wordpress";

//catch the errors too using '2>&1'
$output = shell_exec( "wp plugin activate wp-smush-pro $path 2>&1" );

//if plugin successfully actives 
if ( strpos( $output, "Success:" ) !== false ) {

	//smush all non-smushed images
	shell_exec( "wp smush compress $path" );

	//now deactivate the plugin
	shell_exec( "wp plugin deactivate wp-smush-pro $path" );

}

Share your thoughts by leaving a comment below.