我试图安排有动态挂钩的wordpress cron工作。我正在开发一个插件,并对cron作业进行动态调度。我需要一种方法来命名与登录用户直接对应的钩子。因此,我将用户登录名作为钩子名追加。这是我正在使用的代码片段。
class Easy_Editor_Settings {
public $script="";
public $default_email="";
public $default_name="";
public $hook="";
public function __construct() {
global $current_user;
wp_get_current_user();
$current_username=$current_user->user_login;
$this->hook="easy_user".$current_username;
add_action ($this->hook, [$this,'run_service'], 1, 10 );
add_action( 'wp_ajax_easy_email_settings_call_back', [$this,'easy_email_settings_call_back'] );
add_filter('cron_schedules',[$this,'my_cron_schedules']);
}公共函数运行服务只打印错误日志,但接受我以后必须使用的10个参数。我可以看到工作计划使用WP-控制插件。问题是,当我使用钩子名称时,在末尾追加$current_username变量时,run_service函数不会运行。但是Wp-Control显示了对所需函数的操作以及参数和钩子的名称。但是,当我将get_current_user()作为
$this->hook="easy_user".get_current_user();但是,run_service函数工作正常,get_current_user返回我的OS用户名而不是wp用户名。我不知道是什么导致了这个问题,我需要一些方式来附加当前登录用户的钩子名称。
发布于 2018-08-24 20:35:11
我相信您需要使用wp_get_current_user() (参见https://codex.wordpress.org/Function_参考/wp_到达_当前_用户 ),它将返回当前的WP用户信息作为对象。
从那里,您可以获得用户名(或其他参数)。在该页面中,这个示例应该会让您开始:
$current_user = wp_get_current_user();
/**
* @example Safe usage: $current_user = wp_get_current_user();
* if ( !($current_user instanceof WP_User) )
* return;
*/
echo 'Username: ' . $current_user->user_login . '
';
echo 'User email: ' . $current_user->user_email . '
';
echo 'User first name: ' . $current_user->user_firstname . '
';
echo 'User last name: ' . $current_user->user_lastname . '
';
echo 'User display name: ' . $current_user->display_name . '
';
echo 'User ID: ' . $current_user->ID . '
';您使用的get_current_user()函数是一个PHP命令,而不是WP命令。它将返回运行脚本的用户。请参阅http://php.net/manual/en/function.get-current-user.php
https://wordpress.stackexchange.com/questions/312419
复制相似问题