我安装了WooCommerce的wordpress,我正在尝试使用此代码登录管理员用户:
if ( !is_user_logged_in() ) { $user = get_userdatabylogin( $username ); $id = $user->ID; wp_set_current_user( $id,$user->user_login ); wp_set_auth_cookie( $id ); do_action( 'wp_login',$user->user_login ); }
但它返回此错误消息:
Warning: Missing argument 2 for wc_maybe_store_user_agent() in
/home/i/n/username/public_html/shop/wp-content/plugins/woocommerce/includes/wc-core-functions.PHP
on line 1516
我试过在网上四处寻找,但似乎没有人遇到过这个问题.
if (!is_user_logged_in()) { //determine wordpress user account to impersonate $user_login = 'guest'; //get user ID $user = get_userdatabylogin($user_login); $user_id = $user->ID; //login wp_set_current_user($user_id,$user_login); wp_set_auth_cookie($user_id); do_action('wp_login',$user_login); }
像WooCommerce这样有一个钩子.
add_action( 'wp_login','wc_maybe_store_user_agent',10,2 );
“wc_maybe_store_user_agent”需要2个参数.如果未设置用户ID,则会显示您提到的错误.
测试我上面的代码片段
函数位于plugins / woocommerce / includes / wc-core-functions.PHP的第1516行
function wc_maybe_store_user_agent( $user_login,$user ) { if ( 'yes' === get_option( 'woocommerce_allow_tracking','no' ) && user_can( $user,'manage_woocommerce' ) ) { $admin_user_agents = array_filter( (array) get_option( 'woocommerce_tracker_ua',array() ) ); $admin_user_agents[] = wc_get_user_agent(); update_option( 'woocommerce_tracker_ua',array_unique( $admin_user_agents ) ); } } add_action( 'wp_login',2 );