Make WordPress Core

Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#49445 closed defect (bug) (invalid)

Only one argument passed to wp_login callback.

Reported by: tmfhokies's profile tmfhokies Owned by:
Milestone: Priority: normal
Severity: normal Version: 5.3.2
Component: Login and Registration Keywords:
Focuses: Cc:

Description

Hook into the wp_login action from a barebones plugin.

<?php
/**
 * Plugin Name: wp_login hook issue
 */
add_action('wp_login', function ($user_login, $user) {
});

Activate the "wp_login hook issue" plugin, log out, try to log in. That results in an "PHP Fatal error: Uncaught ArgumentCountError: Too few arguments to function".

Even though the do_action call passes both (https://core.trac.wordpress.org/browser/tags/5.3/src/wp-includes/user.php#L110), the closure only receives $user_login.

Tried with PHP 7.2.22.

Change History (4)

#1 @pbiron
5 years ago

Hi @tmfhokies, welcome to Trac!

The problem you're experiencing is because of the way you're calling add_action(). add_action() has an optional 2nd & 3rd parameter: the 3rd is "The number of arguments the function accepts". For more details, see add_action().

If you change the call to:

<?php
add_action('wp_login', function ($user_login, $user) {
}, 10, 2 );

you will find the error no longer happens.

Last edited 5 years ago by pbiron (previous) (diff)

#2 @tmfhokies
5 years ago

  • Resolution set to invalid
  • Status changed from new to closed

@pbiron Thanks, I definitely overlooked the default value for $accepted_args.

#3 @pbiron
5 years ago

glad I could help.

#4 @SergeyBiryukov
5 years ago

  • Milestone Awaiting Review deleted
Note: See TracTickets for help on using tickets.