skip to Main Content

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.

2

Answers


  1. 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.

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search