Home > Software engineering >  Deprecated: stripos(): Error in Wordpress After Updating to php 8.1
Deprecated: stripos(): Error in Wordpress After Updating to php 8.1

Time:01-16

Showing this error msg after upgrading to php 8.1 in wordpress

Deprecated: stripos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /public_html/wp-includes/functions.wp-styles.php on line 90

Here is the function which i found in the functions.wp-styles.php

function wp_add_inline_style( $handle, $data ) {
    _wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle );

    if ( false !== stripos( $data, '</style>' ) ) {
        _doing_it_wrong(
            __FUNCTION__,
            sprintf(
                /* translators: 1: <style>, 2: wp_add_inline_style() */
                __( 'Do not pass %1$s tags to %2$s.' ),
                '<code>&lt;style&gt;</code>',
                '<code>wp_add_inline_style()</code>'
            ),
            '3.7.0'
        );
        $data = trim( preg_replace( '#<style[^>]*>(.*)</style>#is', '$1', $data ) );
    }

    return wp_styles()->add_inline_style( $handle, $data );
}

Here is the 90 number line inside this code which causes the error, if ( false !== stripos( $data, '</style>' ) ) {

I upgraded to php 8.1 and then i got this error message, i want to solve this problem.

CodePudding user response:

It means that $data is null and you cannot pass null to string methods since 8.1.

You should investigate why $data is null first, and fix that as this may be alluding you to a real issue.

However, if you're after a quick-fix you can just change:

if ( false !== stripos( $data, '</style>' ) ) {

to

if ( false !== stripos( $data ?? '', '</style>' ) ) {

This will ensure that a string is always provided as the first parameter, even if $data is null or unset.

CodePudding user response:

This is a WordPress core problem. For best results use php 8.0 or earlier with WordPress, or turn off WP_DEBUG.

Your site works fine with deprecation notices, they are just a nuisance.

Avoid changing core code on your site if possible.

In the core team, work to eliminate php 8.x deprecation notices is ongoing. Read this.

  • Related