Unable to query OPcache status when running wp-cli

Written by James McDonald

August 14, 2023

You are running wp-cli and see this error message:

Unable to query OPcache status: array_key_exists(): Argument #2 ($array) must be of type array, bool given.

This error is thrown when opcache_get_status( true ) is called and returns false because opcache is disabled and then array_key_exist looks for a ‘scripts’ in a bool instead of an array.

// wp-content/plugins/flush-opcache/admin/class-flush-opcache-admin.php

        /**
         * Where OPcache is actually flushed
         */
        public function flush_opcache_reset() {
                $opcache_scripts = array();
                if ( function_exists( 'opcache_get_status' ) ) {
                        try {
                                $raw = opcache_get_status( true );
                                if ( array_key_exists( 'scripts', $raw ) ) {
                                        foreach ( $raw['scripts'] as $script ) {
                                                /* Remove files outside of WP */
                                                if ( false === strpos( $script['full_path'], get_home_path() ) && false === strpos( $script['full_path'], ABSPATH ) ) {
                                                        continue;
                                                }
                                                array_push( $opcache_scripts, $script['full_path'] );
                                        }
                                }
                        } catch ( \Throwable $e ) {
                                error_log( sprintf( 'Unable to query OPcache status: %s.', $e->getMessage() ), $e->getCode() ); // phpcs:ignore
                        }
                }
                foreach ( $opcache_scripts as $file ) {
                        wp_opcache_invalidate( $file, true );
                }
        }

Cause: opcache is installed but not enabled for the cli

php -i | grep -i opcache | grep enable
opcache.enable => On => On
opcache.enable_cli => Off => Off

Work-a-round: find the opcache cli ini file and turn opcache on for the cli

php -i | grep -i opcache | grep ini
/etc/php/8.2/cli/conf.d/10-opcache.ini,

Edit the cli opcache ini and set opcache.enable_cli to on

sudo vim /etc/php/8.2/cli/conf.d/10-opcache.ini 
# add on or true or 1 to turn it on
opcache.enable_cli=on 

On Ubuntu 22.04 the in file will look as follows:

lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 22.04.3 LTS
Release:        22.04
Codename:       jammy

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

The reCAPTCHA verification period has expired. Please reload the page.

You May Also Like…

Robocopy exclude Directories

Just trying to copy everything except a couple of directories from a drive to my NAS This is the secret incantation of...