0

I want to get a course rolling where I show a new link for a weekly lesson to a subscriber, based on how many weeks the subscriber has been registered.

Say, the subscriber goes to the course page and gets shown something like the schematic below.

http://imageshack.us/photo/my-images/845/coursedesignwordpress.png/

Visitor Signups To My Blog = Week 0

I was thinking of using slugs to do this. So I would create a page with 'week-3' slug and then I would use some custom PHP code checking for the slug and return the right ones, on the course page. Example:

  1. Check for how long the user has signed-up (in weeks)
  2. Show the title of page that has 'slug-'$weeks

But there is a problem with this. I wouldn't be able to easily change the order of the lesson. Say for example, change lesson from week-3 to week-2.

What is an easy way to do this and be able to easily change the lesson week?

1 Answer 1

0

Your task is fairly simple given the tools that wordpress provides.

Step 1: The date/time a user registers an account at your site at is not naturally recorded by wordpress. You'd have to manually add such a functionality. It would make sense to save that to the wp_usermeta table.

Have a look the php time() and the wordpress add_user_meta() functions. Since the metadata would have to be written when the new user registers, you should trigger the timestamp adding with user_register hook.

Step 2, Option 1: Compare current time to user registration time:

$reg_time = get_user_meta($user_id, 'reg_time', true); // dynamically insert user ID
$week = intval( ceil( ( time() - $reg_time ) / 604800 ) )

Step 2, Option 2: If you need more complex things to happen to the user on a weekly basis, you could schedule weekly events. That could be either done via cronjobs directly in the server's shell or via wordpress itsself.

For the latter option check out wp_schedule_event().

Since that natively supports only hourly, bi-daily and daily events, you'd have to add a custom recurrence:

function add_weekly_recurrence($schedules) {
    $schedules['weekly'] = array(
        'interval'=> 604800,
        'display'=>  __('Once Every Week')
    );
    return $schedules;
}
add_filter('cron_schedules', 'add_weekly_recurrence');

The scheduling of a new job should probably also be triggered by user_register.

Step 3. Now that you have the user registration time as well as the ability compare time or schedule weekly events, you'd have to lastly write a function that actually defines what content is shown (and possibly gets scheduled). It would have to have the user ID as (one of its) argument(s) and, if you chose option 2, it should also increment the week/lesson, hence then you should probably store the user's current week/lesson number in the wp_usermeta table as well and make that a function parameter also.

Done.

Further reading:

Asides: I doubt weekly lessons are a good idea for any subject. I personally would hate not being able to go at my own speed and be restricted in my process, regardless of what I am taught.

1
  • Thank you so much, your response is great and very complete. :)
    – peterbz
    Commented Dec 8, 2011 at 0:12

Your Answer

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

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