I have a button on the front end which will trigger a function via AJAX to follow another user. When the process is complete/successful, it will run one last function which is a do_action.

Despite even manually passing static values, I am getting back the following error:

Too few arguments to function sa_post_follow_user_callback(), 1 passed

I am clearly passing two:

do_action( 'sa_post_follow_user', 1, 16 );

Here is part of the code which is run by ajax:

if ( $followed ) {

  do_action( 'sa_post_follow_user', 1, 16 );

  return true;
}

The add_action:

function sa_post_follow_user_callback( $user_id, $user_to_follow ) {

    // Get user data from follower and followee.
    $followee_user_data = get_userdata( $user_to_follow );
    $follower_user_data = get_userdata( $user_id );

    // Compile user data into array to pass to function.
    $data = [
        'followee_username' => $followee_user_data->user_login,
        'followee_email'    => $followee_user_data->user_email,
        'follower_username' => $follower_user_data->user_login,
        'follower_profile_link' => get_home_url() . '/user/' . $follower_user_data->user_login,
    ];

    // Send email
    send_follow_email( $data );

}
add_action( 'sa_post_follow_user', 'sa_post_follow_user_callback' );

error:

PHP Fatal error: Uncaught ArgumentCountError: Too few arguments to
function sa_post_follow_user_callback(), 1 passed in
/srv/www/$

Stack trace:
#0 sa_post_follow_user_callback(1)
#1 WP_Hook->apply_filters('', Array)
#2 WP_Hook->do_action(Array)
#3 do_action('sa_post_follow_...', 1, 16)
#4 sa_follow_user(1, 17)

1 Answer
1

Your function needs 2 arguments, but you didn’t mention this to add_action

add_action( string $tag, callable $function_to_add, int $priority = 10, int $accepted_args = 1 )

add_action()

By only passing the tag and function to add, PHP used the defaults for priority and accepted arguments, which were a priority of 10, and 1 accepted argument. But 1 != 2.

So, to fix this, you need to specify the accepted arguments parameter

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *