fldtrace / user-private-files-plugin

User Private File Plugin for WordPress
6 stars 4 forks source link

Select multiple users? #3

Open trhooper123 opened 8 years ago

trhooper123 commented 8 years ago

Hello, Awesome plugin! I am using it for a school project where we have to assign pdf docs to 3 specific users. I found the lines in the php code and edited them to show three drop down boxes but it makes all three boxes the same whenever I save. Any tips or ideas??

<p><input type="file" name="upf_file" id="upf_file" /></p>
<p class="label"><label for="upf_user1"><?php _e('Select a user', 'user-private-files');?></label></p>  
<select name="upf_user1" id="upf_user1">
    <?php
    $users = get_users();
    $upf_user1 = get_post_meta($post->ID, 'upf_user1', true);
    foreach ($users as $user) { ?>
        <option value="<?php echo $user->ID;?>" <?php if ($upf_user1 == $user->user_login) echo 'selected="selected"';?>><?php echo $user->user_login;?></option>
        <?php
    }
    ?>
</select>
<p class="label"><input type="checkbox" name="upf_notify" value="1"> <label for="upf_notify"><?php _e('Notify User', 'user-private-files');?></label></p>   
<p class="label"><label for="upf_user2"><?php _e('Select a user', 'user-private-files');?></label></p>  
<select name="upf_user2" id="upf_user2">
    <?php
    $users = get_users();
    $upf_user2 = get_post_meta($post->ID, 'upf_user2', true);
    foreach ($users as $user) { ?>
        <option value="<?php echo $user->ID;?>" <?php if ($upf_user2 == $user->user_login) echo 'selected="selected"';?>><?php echo $user->user_login;?></option>
        <?php
    }
    ?>
</select>
<p class="label"><input type="checkbox" name="upf_notify" value="1"> <label for="upf_notify"><?php _e('Notify User', 'user-private-files');?></label></p>   
<p class="label"><label for="upf_user3"><?php _e('Select a user', 'user-private-files');?></label></p>  
<select name="upf_user3" id="upf_user3">
    <?php
    $users = get_users();
    $upf_user3 = get_post_meta($post->ID, 'upf_user3', true);
    foreach ($users as $user) { ?>
        <option value="<?php echo $user->ID;?>" <?php if ($upf_user3 == $user->user_login) echo 'selected="selected"';?>><?php echo $user->user_login;?></option>
        <?php
    }
    ?>
</select>
<p class="label"><input type="checkbox" name="upf_notify" value="1"> <label for="upf_notify"><?php _e('Notify User', 'user-private-files');?></label></p>   

<?php 
fldtrace commented 8 years ago

it's not clear if the functionality is working when you save but in case it does and you want to save the selection you can look into something like this solution: http://stackoverflow.com/questions/10957926/saving-dropdown-menu-selection-in-a-cookie

trhooper123 commented 8 years ago

It will let you select three things, then when you click update on the page it changes all three to the same user.

trhooper123 commented 8 years ago

I was able to get it working with choosing 3 users and having it save them successful. The only thing left is to have it display it for the users. I am only able to get it to show for the first user.

Here is the code for creating the three separate users

<p><input type="file" name="upf_file" id="upf_file" /></p>
<p class="label"><label for="upf_user1"><?php _e('Select a user', 'user-private-files');?></label></p>  
<select name="upf_user1" id="upf_user1">
    <?php
    $users = get_users();
    $upf_user1 = get_post_meta($post->ID, 'upf_user1', true);
    foreach ($users as $user) { ?>
        <option value="<?php echo $user->ID;?>" <?php if ($upf_user1 == $user->user_login) echo 'selected="selected"';?>><?php echo $user->user_login;?></option>
        <?php
    }
    ?>
</select>
<p class="label"><input type="checkbox" name="upf_notify" value="1"> <label for="upf_notify"><?php _e('Notify User', 'user-private-files');?></label></p>   
<select name="upf_user2" id="upf_user2">
    <?php
    $users = get_users();
    $upf_user2 = get_post_meta($post->ID, 'upf_user2', true);
    foreach ($users as $user) { ?>
        <option value="<?php echo $user->ID;?>" <?php if ($upf_user2 == $user->user_login) echo 'selected="selected"';?>><?php echo $user->user_login;?></option>
        <?php
    }
    ?>
</select>
<p class="label"><input type="checkbox" name="upf_notify" value="1"> <label for="upf_notify"><?php _e('Notify User', 'user-private-files');?></label></p>
<select name="upf_user3" id="upf_user3">
    <?php
    $users = get_users();
    $upf_user3 = get_post_meta($post->ID, 'upf_user3', true);
    foreach ($users as $user) { ?>
        <option value="<?php echo $user->ID;?>" <?php if ($upf_user3 == $user->user_login) echo 'selected="selected"';?>><?php echo $user->user_login;?></option>
        <?php
    }
    ?>

And for saving the three users

// if invalid $post object or post type is not 'userfile', return if(!$post || get_post_type($post->ID) != 'userfile') return;

$user_info = get_userdata($_POST['upf_user1']);
add_post_meta($post_id, 'upf_user1', $user_info->user_login);
update_post_meta($post_id, 'upf_user1', $user_info->user_login);

$user_info = get_userdata($_POST['upf_user2']);
add_post_meta($post_id, 'upf_user2', $user_info->user_login);
update_post_meta($post_id, 'upf_user2', $user_info->user_login);

$user_info = get_userdata($_POST['upf_user3']);
add_post_meta($post_id, 'upf_user3', $user_info->user_login);
update_post_meta($post_id, 'upf_user3', $user_info->user_login);

And here is where I am stuck where it creates the file list

'userfile', 'meta_key' => 'upf_user1', 'meta_value' => $current_user->user_login, 'orderby' => 'date', 'order' => DESC );