0

I am trying to schedule wordpress cron job having dynamic hooks. I am developing a plugin and the cron jobs are to be scheduled dynamically. I need a way to name the hooks that directly corresponds to logged in user. So i am appending user login name as hook name. Here is the piece of code that i am using.

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']);



}

The public function run service only prints error log but takes in 10 arguments those i have to use later. I can see the job being scheduled using WP-Control plugin. The problem is when i use hook name by appending $current_username variable at the end, the run_service function does not run. But Wp-Control shows the action to required function along with arguments and hook's name. However, when i append get_current_user() as

$this->hook="easy_user".get_current_user();

The run_service function works, however, get_current_user returns my OS username instead of wp username. I am not sure what is causing the issue, i need some way to append current logged in user in hook name.

1 Answer 1

1

I believe you need to use wp_get_current_user() (see https://codex.wordpress.org/Function_Reference/wp_get_current_user ), which will return the current WP user information as an object.

From there, you can get the user name (or other parameters). From that page, this example should get you started:

$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 . '<br />';
    echo 'User email: ' . $current_user->user_email . '<br />';
    echo 'User first name: ' . $current_user->user_firstname . '<br />';
    echo 'User last name: ' . $current_user->user_lastname . '<br />';
    echo 'User display name: ' . $current_user->display_name . '<br />';
    echo 'User ID: ' . $current_user->ID . '<br />';

The get_current_user() function you used is a PHP command, not a WP command. It will return the user that is running the script. See http://php.net/manual/en/function.get-current-user.php

2
  • Actually, my real issue is that if i use $current_user as you stated above, the run_service function which is the main function that cron job has to run, does not run at all. Aug 24, 2018 at 20:37
  • I believe the CRON job needs a system user/pass as the credentials to run the CRON job. The WP user has no rights at the operating system level. The get_current_user will get you the current operating system user (the user that is 'running' the page). If you are trying to use the WP user as the credentialed user for the CRON command, then that won't work. It might help if you added to your question and showed the CRON command you are trying to build. Aug 25, 2018 at 2:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.