0

Editors note: Twitter ID and Google Analytics only serve as examples and are exchangeable.

TL;DR

I want to save external data in a user profile once or twice a day. I added two custom fields in my user profiles:

  1. Twitter_ID (I manually put it from the dashboard)
  2. test1 (external data got with php function)

I would like to be able to do something like : Save once a day in test1 for each user profile the external data I get from Twitter_ID.

The main goal : I am working on a panel which will display statistics of my user websites (external website). For that, I use the Google Analytics API. I save the Google Analytics profile number in a custom profile field. For the moment, I run the script (statistics) each time my users want to see them (in front end). And it takes few seconds to generate them. My statistics change approx once a day (It's not hourly stats, but daily stats). So, the idea was to save once a day the statistics to the user profiles. So, like that, it won't take time to generate them each time the user will load a stats page.

The solutions : So, I started to think about different solutions. But the best would be to save the data without any action from the users. Or maybe when someone visits the website, it would save the data for everyone once the day with wp_cron. For the moment, the only thing I succeed was to save the data when a user update his profile. But it will save only his data, and not for everyone, so it's not what I want.

EDIT

I tried this :

add_filter('cron_schedules', 'new_interval');

function new_interval($interval) {

    $interval['minutes_1'] = array('interval' => 10*60, 'display' => 'Once 1 minute');

    return $interval;
}


function InitiateMyCron() {
    if (!wp_next_scheduled('MyCronAction')) {
        wp_schedule_event(time(), 'minutes_1', 'MyCronAction');
    }
}

function MyCronAction( $user_id ) {
    $twitter_id = $_POST['twitter_id'];
    $result = $twitter_id;
    if ( !current_user_can( 'edit_user', $user_id ) )
    return false;
    update_usermeta( $user_id, 'test1', $result );
}

add_action( 'personal_options_update', 'MyCronAction' );
add_action( 'edit_user_profile_update', 'MyCronAction' );

But, the field "test1" is updated when I save the "Twitter_id". I don't know if it is updated every minute

EDIT 2

if ( ! wp_next_scheduled( 'my_save_statistiques' ) ) {
  wp_schedule_event( strtotime('tomorrow'), 'daily', 'my_save_statistiques' );
} 

function my_save_statistiques( $user_id ) {


"blabla php"  =        
$ga->requestReportData($num_analytics,array('pagePath'),array('visits'), null, null, $fromDate, $toDate); 
$visites_totales = $ga->getVisits();



if ( !current_user_can( 'edit_user', $user_id ) )
return false;
update_usermeta( $user_id, 'visites_totales', $visites_totales );
add_action( 'personal_options_update', 'my_save_statistiques' );
add_action( 'edit_user_profile_update', 'my_save_statistiques' );
}

EDIT 3

I finally ended up with this but it's not working : (I changed 1 week to 30 seconds but the name are still the same)

add_filter( 'cron_schedules', 'myprefix_add_weekly_cron_schedule' );
function myprefix_add_weekly_cron_schedule( $schedules ) {
    $schedules['weekly'] = array(
        'interval' => 30, // 30 seconds
        'display'  => __( 'Once Weekly' ),
    );

    return $schedules;
}

// Schedule an action if it's not already scheduled
if ( ! wp_next_scheduled( 'myprefix_my_cron_action' ) ) {
    wp_schedule_event( time(), 'weekly', 'myprefix_my_cron_action' );
}

// Hook into that action that'll fire weekly
add_action( 'myprefix_my_cron_action', 'myprefix_function_to_run' );
function myprefix_function_to_run() {
    function my_save_statistiques( $user_id ) { 
            [...]
        $visites_totales = $ga->getVisits();

        if ( !current_user_can( 'edit_user', $user_id ) )
        return false;
        update_usermeta( $user_id, 'visites_totales', $visites_totales );
    }
    add_action( 'personal_options_update', 'my_save_statistiques' );
    add_action( 'edit_user_profile_update', 'my_save_statistiques' );
}

Edit 4:

I discovered that add_action( 'personal_options_update', 'my_save_statistiques' ); is the action when a user updates his own profile but it's not what I want... Because I want to save the data for all users once a day. So that's why (I guess), I thought that my previous code wasn't working.

Edit 5:

FIRST STEP : 1/2 SUCCESS

The user will save his data each time he visits the website. The main goal was to update the data for everyone but it can work like that. Next step : Cron

function my_save_statistiques( $user_id ) { 
    $visites_totales = [myscript];
    if ( !current_user_can( 'edit_user', $user_id ) )
    return false;
    update_usermeta( $user_id, 'visites_totales', $visites_totales );
}
add_action( 'wp_head', 'my_save_statistiques' );

EDIT 6:

Is it correct ?

// Schedule an action if it's not already scheduled
if ( ! wp_next_scheduled( 'wp_head' ) ) {
wp_schedule_event( time(), 'hourly', 'wp_head' );
}
// Hook into that action that'll fire weekly
function my_save_statistiques( $user_id ) { 
$visites_totales = [myscript];
update_usermeta( $user_id, 'visites_totales', $visites_totales );       
}
add_action( 'wp_head', 'my_save_statistiques' );

I found a different solution

The solution :

Save the time when the action has been ran and check the difference with actual time :

function my_save_statistiques( $user_id ) { 
    $user_id = get_current_user_id(); 
    $current_time = time(); 

    if (empty($current_time)) {
    update_usermeta( $user_id, 'last_analytics',  $current_time );
    }

    $last_analytics = get_user_meta( $user_id, 'last_analytics', true );
    $diff = $current_time - $last_analytics;
    $one_day = 86400;

    if ($diff > $one_day) {

    $visites_totales = [myscript]

    update_usermeta( $user_id, 'last_analytics',  $current_time );
    update_usermeta( $user_id, 'visites_totales',  $visites_totales );
    }
}

add_action( 'wp_head', 'my_save_statistiques' );
  • 1
    Hmm, your code is a little bit messed up... But first of all... Do you call 'InitiateMyCron' function anywhere in your code? – Krzysiek Dróżdż Feb 28 '14 at 18:47
  • no... I don't know how to do that – Noob Feb 28 '14 at 18:48
  • I'm lost... First of all, I'm not sure to understand what the code I put should do... Is it going to be loaded every nminute even if nobody goes in website ? Or it will be loaded by all users each they go to the website ? – Noob Feb 28 '14 at 18:54
  • Ok, I checked in the internet, and the action will be ran by the first user/visitor (after the scheduled time). – Noob Feb 28 '14 at 18:59
  • 2
    Reduce the time to 5 minutes. Than it's easier to monitor. ;) – kaiser Feb 28 '14 at 20:02
0

An alternative way to do what I wanted :

Save the time when the action has been ran and check the difference with actual time :

function my_save_statistiques( $user_id ) { 
    $user_id = get_current_user_id(); 
    $current_time = time(); 

    if (empty($current_time)) {
    update_usermeta( $user_id, 'last_analytics',  $current_time );
    }

    $last_analytics = get_user_meta( $user_id, 'last_analytics', true );
    $diff = $current_time - $last_analytics;
    $one_day = 86400;

    if ($diff > $one_day) {

    $visites_totales = [myscript]

    update_usermeta( $user_id, 'last_analytics',  $current_time );
    update_usermeta( $user_id, 'visites_totales',  $visites_totales );
    }
}

add_action( 'wp_head', 'my_save_statistiques' );

So, the script will run for a logged user if the time between the last "run" and now is more than 24h, knowing that my website is only accessible to a logged user

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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