Please or Register to create posts and topics.

Hide uploads from logged in users with conditions


I'm trying to complete the behaviour of  the functionality "hide_uploads_from_guests" by adding another case :
If a user is logged in but does not have a special permission (thanks to a membership), so he can't see the uploaded files.

That is working in 'forum-uploads.php', after this IF

if (!is_user_logged_in() && $this->asgarosforum->options['hide_uploads_from_guests'])

by adding the following ELSEIF (including the permission check) :

elseif (is_user_logged_in() && $this->asgarosforum->options['hide_uploads_from_guests'] && [[SPECIAL-USER-PERMISSION]]) {

$uploadedFiles .= '<li>'.__('Please <a href="/link-to-upgrade">UPGRADE</a>.', 'asgaros-forum').'</li>';

Instead of the uploaded files, it displays "Please UPGRADE" with a link to the upgrade page.


In this case, the problem is that every uploaded files are hidden, even those uploaded by the current user. The current user should be able to see his own uploaded files.

I tried this, by adding in the ELSEIF another condition : When the current user browses another user's posts, he can't see the uploaded files.

elseif (is_user_logged_in() && $this->asgarosforum->options['hide_uploads_from_guests'] && [[SPECIAL-USER-PERMISSION]] && (get_current_user_id() != $author_id=$post->post_author)) { 

$uploadedFiles .= '<li>'.__('Please <a href="/link-to-upgrade">UPGRADE</a>.', 'asgaros-forum').'</li>';

But it does not work. I guess the code is incorrect :

&& (get_current_user_id() != $author_id=$post->post_author)

Could you help me please ?

How to show to the current user only his own uploaded files when he browses the forum ? Ideally just by editing the ELSEIF I've described.

Thanks a lot in advance,

The full function for the context :

public function show_uploaded_files($post_id, $post_uploads) {
  $path = $this->upload_path.$post_id.'/';
      $url = $this->upload_url.$post_id.'/';
      $uploads = maybe_unserialize($post_uploads);
      $uploadedFiles = '';
  $output = '';

      if (!empty($uploads) && is_dir($path)) {
    // Generate special message instead of file-list when hiding uploads for guests.
    if (!is_user_logged_in() && $this->asgarosforum->options['hide_uploads_from_guests']) {
      $uploadedFiles .= '<li>'.__('You need to login to have access to uploads.', 'asgaros-forum').'</li>';

                     elseif... (see code above)

                     else {
      foreach ($uploads as $upload) {
                if (is_file($path.wp_basename($upload))) {
          $file_extension = strtolower(pathinfo($path.wp_basename($upload), PATHINFO_EXTENSION));
          $imageThumbnail = ($this->asgarosforum->options['uploads_show_thumbnails'] && $file_extension !== 'pdf') ? wp_get_image_editor($path.wp_basename($upload)) : false;

          $uploadedFiles .= '<li class="uploaded-file">';

          if ($imageThumbnail && !is_wp_error($imageThumbnail)) {
            $uploadedFiles .= '<a href="'.$url.utf8_uri_encode($upload).'" target="_blank"><img class="resize" src="'.$url.utf8_uri_encode($upload).'" alt="'.$upload.'"></a>';
          } else {
            $uploadedFiles .= '<a href="'.$url.utf8_uri_encode($upload).'" target="_blank">'.$upload.'</a>';

          $uploadedFiles .= '</li>';

    if (!empty($uploadedFiles)) {
              $output .= '<strong class="uploaded-files-title">'.__('Uploaded files:', 'asgaros-forum').'</strong>';
              $output .= '<ul>'.$uploadedFiles.'</ul>';

  return $output;

I hope there's nothing to change into the 'ELSE'...

Thank you for your help.


Hi @asgaros (and @qualmy91 as you're a strong Asgaros Forum partner / contributor), if you have any concrete idea how to do, I'm available to discuss it. Tschüss !

It's a wonderful piece. In my opinion, this is one of the best blog posts ever. I admire and am inspired by your work. That's really kind of you.  hurdle game