0

In the error log of my server i´m getting the following PHP Warning:

in_array() expects parameter 2 to be array, boolean given in " //..." on line 45.

In line 45 i have set a function that checks wether WooCommerce plug-in is active.

    /**
 * Construction function
 */
public function __construct() {
    // Check if Woocomerce plugin is actived
    if ( ! in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
        return;
    }

    $this->new_duration = sober_get_option( 'product_newness' );

    $this->parse_query();
    $this->hooks();
}

Checking if it´s active with the if statement is necessary, is there anything i´m not seeing?

The error keeps popping up in my error log.

8
  • 1
    Does apply_filters() return an array? Commented Apr 25, 2020 at 9:13
  • 2
    @MarkusZeller It definitely does not Commented Apr 25, 2020 at 9:14
  • 1
    @Akintunde-Rotimi Thank you. Then it is clear. in_array() expects 2nd param as array. Commented Apr 25, 2020 at 9:16
  • array_filters() will return an array. Please check here: developer.wordpress.org/reference/functions/apply_filters @Markus Zeller. Commented Apr 25, 2020 at 9:30
  • 1
    get_option( 'active_plugins' ) -- Replace with default empty array => get_option( 'active_plugins' ,array() ) -- It's the correct WordPress way. Commented Apr 25, 2020 at 12:21

1 Answer 1

1

You need to change the method how you verify if a plugin is available.

if(!is_plugin_active('woocommerce/woocommerce.php')) {
    return;
}

Your method fails, because you check with in_array() which needs to have the 2nd parameter being an array, but apply_filters() returns a bool.

Update

You could try to cast the result of apply_filters() being an array.

if ( ! in_array( 'woocommerce/woocommerce.php', (array)apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
   return;
}
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks Markus, will be checking if the error persists, have added the code into the function. It just boggles me as i didn´t get this error before, feel like it happened after plug-in updates.. i´m not sure
I don't like function returning mixed. You can not rely on that when not verifying the result by yourself. So you could $result = apply_filters( 'active_plugins', get_option( 'active_plugins' ) ); and then evaluate if it is an array or not.
But if you want to check if an plugin is active, and there is exactly a function for testing that, it should be used.
I will be checking throught the day if the changes return any errors.. so many things to learn...
Old code had to be rewritten, after is_plugin_active was written, the error was PHP Fatal error: Uncaught Error: Call to undefined function is_plugin_active() in *** It then proceeded to show me the stack race.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.