‘Clear APC Cache’ Button for WordPress

Here is a quick way to clear APC opcode cache with a single click within your WordPress dashboard — simply add this to your theme’s functions.php:

if (function_exists('apc_clear_cache')) {
	// Clear cache if something is edited
	if (!empty($_POST) && is_admin())
		apc_clear_cache();

	// Add Clear APC menu under Tools menu
	add_action('admin_menu', 'php_apc_info');
	function php_apc_info() {
		add_submenu_page('tools.php', 'Clear APC', 'Clear APC', 'activate_plugins', 'clear_php_apc', 'php_apc_options');
	}
	function php_apc_options() {
		if (apc_clear_cache() && apc_clear_cache('user'))
			print '<p>All Clear!</p>';
		else
			print '<p>Clearing Failed!</p>';
		print '<pre>'; print_r(apc_cache_info()); print '</pre>';
	}

	// Add Clear APC in the favorite actions dropdown
	add_filter('favorite_actions', 'clear_apc_link');
	function clear_apc_link($actions) {
		$actions['tools.php?page=clear_php_apc'] = array('Clear APC', 'edit_posts');
		return $actions;
	}
}

And the result looks like this:

Clear APC cache menu item

p.s. It seems that WordPress doesn’t like newline returns inside <pre>.

Leave a Reply