0

I have two Wordpress sites with theme Dyad 2 https://wordpress.com/theme/dyad-2

I need to upgrade from PHP 7.4 to 8.1, but when I try I got four "Deprecated: Non-static" errors. I managed to fix all but this one in class-wp-hook.php:

Deprecated: Non-static method WPCom_Theme_Updater::maybe_nag_for_plugin() should not be called statically in public_html/wp-includes/class-wp-hook.php on line 307

Line 307 is "$value = call_user_func_array( $the_['function'], $args );", and in some context here:

...
...
// Avoid the array_slice() if possible.
if ( 0 == $the_['accepted_args'] ) {
    $value = call_user_func( $the_['function'] );
} elseif ( $the_['accepted_args'] >= $num_args ) {
    $value = call_user_func_array( $the_['function'], $args );
} else {
    $value = call_user_func_array( $the_['function'], array_slice( $args, 0, (int) $the_['accepted_args'] ) );
...
...

The whole code is here (.OLD is the original). Right now I think they are both identical as whatever I tried, I got worse errors and not functioning site. class-wp-hook.php: https://justpaste.it/72r0g class-wp-hook.php.OLD: https://justpaste.it/2tmrn

I'm guessing that it has something to do with the file public_html/wp-content/themes/dyad-2-wpcom/inc/updater.php because it has WPCom_Theme_Updater and maybe_nag_for_plugin() And it was also one of the files that gave most of the "Deprecated: Non-static" errors, so I had to change a lot of "self::" to "(new self)->"

The whole code is here (.OLD is the original). updater.php: https://justpaste.it/4tgpf updater.php.OLD: https://justpaste.it/8kkqc

I have tried to change line 307 and the content of updater.php, but I don't really know what I am doing. I have got some ideas from these links, but cant figure out the right fix: PHP Deprecated: Non-static method should not be called statically https://stackoverflow.com/questions/38469828/calling-non-static-method-with-call-user-func-array-in-php https://stackoverflow.com/questions/19693946/non-static-method-should-not-be-called-statically

1
  • Any changes you make to the theme will be overwritten the next time you update it...
    – vancoder
    Aug 18, 2022 at 15:59

1 Answer 1

2

Do not, under any circumstances, edit core WordPress files.

The bug is not in WordPress. If it were the proper way to handle such a bug is not to edit WordPress. The right way is to downgrade PHP to a compatible version that doesn't cause the error and file a bug report at https://core.trac.wordpress.org, if you're comfortable doing so.

So why does the error message say the problem is in class-wp-hook.php?

In WordPress theme and plugin developers add their own functionality using hooks. This involves telling WordPress to run a given callback when the hook is called. The code that executes these hooked callbacks exists in class-wp-hook.php, so if there is a problem with the callback then error messages will often blame class-wp-hook.php.

Despite this, I can say with a high degree of confidence that significant bugs in class-wp-hook.php are incredibly unlikely to make it into a release version of WordPress, as its entire API is built around hooks and such bugs would be caught almost immediately. Therefore error messages in class-wp-hook.php are almost certainly the fault of a theme or plugin. If something like xdebug is available then you might be able to get more detailed errors that provide more clues, but often the name of class or function in the error message will reveal the true culprit.


So what's the problem in your situation?

As I mentioned, developers provide callbacks for WordPress to call. If the callback provided is in this format:

array( 'WPCom_Theme_Updater', 'maybe_nag_for_plugin' )

Then when WordPress executes the callback PHP will call:

WPCom_Theme_Updater:: maybe_nag_for_plugin();

This is PHP for calling maybe_nag_for_plugin() as a static method of the WPCom_Theme_Updater class. The problem is that maybe_nag_for_plugin() has not been explicitly declared as a static method by the theme author, and as of PHP 8.0.0 this will throw an error.

So your theme's author needs to add a static declaration to the maybe_nag_for_plugin() method of the WPCom_Theme_Updater class. Since this is not your own code the theme author will need to make the change or it will be reverted the next time the theme is updated.

As with WordPress, the proper way to handle a compatibility issue in a theme or plugin is to revert to a compatible PHP version and report the issue to the developer. You should always try to avoid modifying code for which you do not control future updates.

One thing to keep in mind is that this error means that your theme has not been tested with PHP 8, so fixing this one issue may just reveal other similar issues that need to be resolved. This is another good reason to revert to a compatible version and seek the author's assistance.

1
  • Thank you! My web site provider is forcing med to upgrade. So in updater.php I changed public function maybe_nag_for_plugin() { to public static function maybe_nag_for_plugin() { Before I also changed a lot of “self::” to “(new self)->” in that file, so it is now like this: justpaste.it/7ze4s With “public static function maybe_nag_for_plugin()” and changing a lot of “self::” to “(new self)->” it now works with PHP 8.1 Until it gets updated ;) So I still need to ask the author to do similar fixes, so it will continue to work in the future. PHP 7.4 is end of life in November
    – raffe
    Aug 19, 2022 at 11:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.