0

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' );
6
  • I don't think there's any reason to have the "did not start" option, you only need to add data for users who have started or finished. the fact that the data doesn't exist for a particular user/course is the indicator that they didn't start or finish it, then you'd only have to add the meta field on user action, not course publish.
    – Milo
    Commented Dec 25, 2013 at 21:52
  • @Milo Good point. To be honest, the main reason why I though of adding Did not start is in case I couldn't use an empty value in my e-marketing system. Aside from that, Isn't it more appropriate to run the action on course publish since it's only needed once really and the number of users' actions would be much more? Also, for user action it would make sense to update the meta already created? Thanks!
    – Dr.Hariri
    Commented Dec 25, 2013 at 22:32
  • you have to update it on user action regardless, when they start or complete a course, no? then you're only storing data for users who have started or completed a particular course, which is likely a much smaller dataset than storing data for every user/course - if all of your users average 50% course participation, you'll have a table that's half the size it would be if you stored empty rows for all users.
    – Milo
    Commented Dec 25, 2013 at 23:14
  • @Milo Oh I get what you mean now! With regard to the code, is it good or did you spot any issues? Thanks!
    – Dr.Hariri
    Commented Dec 25, 2013 at 23:18
  • @Milo Forgot to ask, how do you propose having the meta field code run for individual users, would this be running the whole new fields codes? Isn't it unnecessary to run the whole code if it has been run before or would running it by user bases restrict the meta field row to be created for that user only. Therefore, not having empty records? Thanks!
    – Dr.Hariri
    Commented Dec 26, 2013 at 1:03

1 Answer 1

0

After some help with a PHP developer, it seems the main code above was close but certain changes were made. Here is the code for those interested. In this code, the custom post type is: Course. Change accordingly if needed for your own use.

function save_educadme_courses_for_user( $user_id ) {

if ( !current_user_can( 'edit_user', $user_id ) ) { return false; }

$args=array('post_type' => 'Course');
$allposts = get_posts( $args );

foreach ( $allposts as $post ) {
update_user_meta( $user_id, $post->post_name, $_POST[$post->post_name] );
}//end of master loop

}           


function educadme_courses_for_user( $user ) {   ?>



<h3> <?php echo $post->post_name; ?> </h3>
<span class="description">   description (what courses have you taken) </span><br>
<table class="form-table">
    <tr>
                <td>
                    <table>                         
        <?php $args=array('post_type' => 'Course');
                    $allposts = get_posts( $args );
                    foreach ( $allposts as $post ) { ?>
        <tr>
        <th style="border-bottom: 1px solid #000;">
        <label for="<?php echo $post->post_name; ?>"><?php echo $post->post_name; ?> (currently: <?php echo esc_attr( get_the_author_meta($post->post_name , $user->ID )); ?>)</label>
        <br>
        <span class="description" style="font-weight:bold;"></span>
        </th>
        <td style="border-bottom: 1px solid #000;">                                      
<input type="radio" name="<?php echo $post->post_name; ?>" value="Did not start" <?php if(esc_attr( get_the_author_meta( $post->post_name , $user->ID ))=='Did not start' || esc_attr( get_the_author_meta( $post->post_name , $user->ID ))==''){ echo 'checked'; } ?> /><label>Did not start</label>
<input type="radio" name="<?php echo $post->post_name; ?>" value="Started" <?php if(esc_attr( get_the_author_meta( $post->post_name , $user->ID ))=='Started'){ echo 'checked'; } ?> /><label>Started</label>
<input type="radio" name="<?php echo $post->post_name; ?>" value="Finished" <?php if(esc_attr( get_the_author_meta( $post->post_name , $user->ID ))=='Finished'){ echo 'checked'; } ?> /><label>Finished</label>
        </td>
        </tr>
                    <?php } ?>
        </table>
            </td>
    </tr>
</table>

<?php }//end of function 


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

I have done some initial testing with the above code and it seems to work. I hope this helps!

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.