• Resolved Jos Klever

    (@josklever)


    I’m debugging an issue while logging in on a site running on PHP 8.0 (no issues on 7.4) and I’ve managed to isolate it to a single function call I’m using to disable admin e-mail verification messages during login.

    Setup:

    • WordPress 5.9 (same with 5.8.x)
    • No plugins
    • Default theme Twenty Twenty (same with other themes)
    • mu-plugin with just 1 filter
    • PHP 8.0.15 (same with older 8.0.x versions)

    Single line of code in wp-content/mu-plugins/my-mu-plugin.php:
    add_filter( 'admin_email_check_interval', 0 );

    Error when logging in:

    Fatal error: Uncaught TypeError: call_user_func_array(): Argument #1 ($callback) must be a valid callback, no array or string given in
    /.../wp-includes/class-wp-hook.php:307 Stack trace: #0
    /.../wp-includes/plugin.php(189): WP_Hook->apply_filters() #1
    /.../wp-login.php(1282): apply_filters() #2
    {main} thrown in /.../wp-includes/class-wp-hook.php on line 307

    A few other filters in the same plugin (removed for testing) don’t give any issues, so it’s this specific filter.

    • This topic was modified 4 years, 2 months ago by Jos Klever.
Viewing 6 replies - 1 through 6 (of 6 total)
  • Thread Starter Jos Klever

    (@josklever)

    I’ve found another reference on a site for this filter using:
    add_filter( 'admin_email_check_interval', '__return_false' );
    and that seems to work, although that’s not what the documentation suggests on
    https://developer.wordpress.org/reference/hooks/admin_email_check_interval/

    Dion

    (@diondesigns)

    The answer to your question can be found in the documentation for add_filter():

    https://developer.wordpress.org/reference/functions/add_filter/

    The second argument must be a valid callback, and you’re passing an integer. Versions of PHP prior to 8.0 did not strictly enforce matching argument types, which is why your original code worked in PHP 7.4. The __return false() function works in PHP 8.0 because false is (usually) equivalent to zero. However, this type mismatch can potentially cause errors in PHP 8.1+, so it’s best to return an integer, as follows:

    add_filter('admin_email_check_interval', function($whocares){return 0;});
    
    Thread Starter Jos Klever

    (@josklever)

    I’m just using the filter as it’s described in the documentation, so that means it’s a bug (in code or documentation), right?

    Dion

    (@diondesigns)

    Actually…the documentation is correct, and the bug is in your use of the filter (or depending on your point of view, perhaps a bug in PHP 8+). The correct usage is to pass a valid callback, not an integer.

    The version I posted uses the correct syntax, though some “WP purists” balk at the use of closures because it doesn’t allow the hook to be removed. Closures are great for single-use functions, so I don’t care what the “purists” think. 🙂

    Please be aware that the majority of the PHP developers have been voting as a block to move PHP towards C++, which means a lot (and I mean A LOT) of current PHP code will be unusable in the next couple years.

    Thread Starter Jos Klever

    (@josklever)

    The documentation says that 0 disables it: https://developer.wordpress.org/reference/hooks/admin_email_check_interval/.

    If you don’t like that WordPress is using PHP, please stop replying here, because it doesn’t help anyone. It definitely doesn’t help to fix this bug.

    Thread Starter Jos Klever

    (@josklever)

    I’ve created a core trac ticket: https://core.trac.wordpress.org/ticket/54998

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘add_filter issue with PHP 8’ is closed to new replies.