I have an online education website using WordPress. I have a custom post type called "course" which is created automatically through the e-learning plugin I am using "WooThemes Sensei".
I am still in an early stage but one important aspect I want to track is users' participation in courses. For this purpose, I want to automatically be able to create meta-fields for each individual course. Within each field, I want to have 3 radio choices: Did not start, started, and finished.
I will then hook the function to another function which is related to the user completing a course to have this meta-field updated.
I am not a programmer and certainly don't know if this is something possible. I've searched for weeks and read many online articles on user meta fields but did not find something that looked into how to do this.
So to sum up: - Custom post type = course. - When I published a course, I want to use course slug to create a new user meta field. But how do I do it once only? I will perhaps try something "if not exist" here? - For each auto-generated meta-field, there are 3 choices: Did not Start, Started, Finished.
I tried to be helpful and show that I've done some research on this before posting but I apologize if it's a complete mess!
I tried the following code, it outputs the form fine but it doesn't show the correct saved value, I suspect it has to do with $course_meta_name not working outside the loop at the top but I am not that good with coding and I haven't found a way to define it properly for $courses pulled. Any help would be appreciated!
Any comments are much appreciated with this and hopefully this will be of use to many others.
function educadme_courses_for_user( $user ) {
$courses_args = array(
'status' => 'approve',
'post_type' => 'course',
);
$courses = get_posts($courses_args);
$slug = $post->post_name;
$course_meta_name = $slug;
$course_for_user = get_the_author_meta( $course_meta_name, $user->ID);
?>
<h3><?php _e('الكورسات في الأكاديمية'); ?></h3>
<span class="description">سجل بالكورسات التي شارك فيها أو أنجزها المشارك.</span><br>
<table class="form-table">
<tr>
<td>
<table>
<?php
foreach($courses as $post) {
$slug = $post->post_name;
$course_title = $post->post_title;
?>
<tr>
<th style="border-bottom: 1px solid #000;">
<label for="<?php echo $slug; ?>">
<?php echo $course_title; ?>
</label>
<br>
<span class="description" style="font-weight:bold;"><?php echo $slug; ?></span>
</th>
<td style="border-bottom: 1px solid #000;">
<label><input type="radio" name="<?php echo $slug; ?>" <?php if ($course_for_user == 'Did not start' OR $course_for_user == '' ) { ?>checked="checked"<?php }?> value="Did not start" />لم يبدأ</label><br />
<label><input type="radio" name="<?php echo $slug; ?>" <?php if ($course_for_user == 'Started' ) { ?>checked="checked"<?php }?> value="Started" />بدأ في الكورس</label><br />
<label><input type="radio" name="<?php echo $slug; ?>" <?php if ($course_for_user == 'Finished' ) { ?>checked="checked"<?php }?> value="Finished" />إنتهى من الكورس</label><br />
</td>
</tr>
<?php } ?>
</table>
</td>
</tr>
</table>
<?php } ?>
<?php
function save_educadme_courses_for_user( $user_id ) {
if ( !is_super_admin() AND !current_user_can( 'edit_user', $user_id ) )
return FALSE;
update_usermeta( $user_id, $course_meta_name, $_POST[$course_meta_name] );
}
add_action( 'show_user_profile', 'educadme_courses_for_user' );
add_action( 'edit_user_profile', 'educadme_courses_for_user' );
add_action( 'personal_options_update', 'save_educadme_courses_for_user' );
add_action( 'edit_user_profile_update', 'save_educadme_courses_for_user' );