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.


Solution:

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.

function my_save_statistiques( $user_id ) { 
    $visites_totales = 'My stats script....';
    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' );

Read more here: Save automatically data to user profile once or twice a day


Related Wordpress search:

, , , , ,

Wordpress related questions and answers: