最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Too few arguments sent to custom function

programmeradmin0浏览0评论

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)

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)
Share Improve this question asked Feb 22, 2020 at 14:20 wharfdalewharfdale 3484 silver badges17 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 6

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 )

https://developer.wordpress/reference/functions/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

发布评论

评论列表(0)

  1. 暂无评论