[Solution] Deprecated Warning with `strpos()` in `is_vc_editor()` Function of OVIC Addon Toolkit
Posted by:
letsdo23
Oct 28, 2024 at 08:13 (a month ago)
Hi everyone,
After updating to a newer PHP version, I encountered a deprecated warning in the OVIC Addon Toolkit plugin that looked like this:
Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /path/to/website/wp-content/plugins/ovic-addon-toolkit/includes/classes/class-core.php on line 211
**Problem:**
This warning appears because, in newer PHP versions, `strpos()` will throw an error if `null` is passed as the first parameter (`$haystack`). In my case, this happened in the `is_vc_editor()` function of the plugin.
**Solution:**
To resolve this, I modified the `is_vc_editor()` function to check if the first parameter is not `null` before calling `strpos()`. Here is the fully updated function:
/**
* is WPBackery editor?
*
* @return bool
*/
public function is_vc_editor()
{
if ($get_referer = wp_get_referer()) {
$query = parse_url($get_referer, PHP_URL_QUERY);
if (!is_null($query) && strpos($query, 'vc_action=vc_inline') !== false) {
return true;
}
}
return false;
}
**Explanation:**
- I first define the variable `$query` and then check if `$query` is not `null`.
- This way, the `strpos()` function is only called if `$query` actually has a valid value.
After this change, the deprecated warnings disappeared, and the plugin continues to function as expected. This adjustment might be helpful for others experiencing similar warnings with newer PHP versions.
Hope this helps someone! If anyone has questions about this solution, feel free to reach out.
---
Good luck!
After updating to a newer PHP version, I encountered a deprecated warning in the OVIC Addon Toolkit plugin that looked like this:
Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /path/to/website/wp-content/plugins/ovic-addon-toolkit/includes/classes/class-core.php on line 211
**Problem:**
This warning appears because, in newer PHP versions, `strpos()` will throw an error if `null` is passed as the first parameter (`$haystack`). In my case, this happened in the `is_vc_editor()` function of the plugin.
**Solution:**
To resolve this, I modified the `is_vc_editor()` function to check if the first parameter is not `null` before calling `strpos()`. Here is the fully updated function:
/**
* is WPBackery editor?
*
* @return bool
*/
public function is_vc_editor()
{
if ($get_referer = wp_get_referer()) {
$query = parse_url($get_referer, PHP_URL_QUERY);
if (!is_null($query) && strpos($query, 'vc_action=vc_inline') !== false) {
return true;
}
}
return false;
}
**Explanation:**
- I first define the variable `$query` and then check if `$query` is not `null`.
- This way, the `strpos()` function is only called if `$query` actually has a valid value.
After this change, the deprecated warnings disappeared, and the plugin continues to function as expected. This adjustment might be helpful for others experiencing similar warnings with newer PHP versions.
Hope this helps someone! If anyone has questions about this solution, feel free to reach out.
---
Good luck!