1

I have a multisite. In Site A, a user is logged in as author.

In Site B, user is still logged without role, but he can't upload file in a front end form (made in ACF).

I need that user only logged in Site A, can upload file in this front end form in Site B. I have tried with two solutions:

if ( is_user_logged_in() )
 add_action('admin_init', 'allow_contributor_uploads');

function allow_contributor_uploads() {
 $user_id = get_current_user_id();
 $user_id->add_cap( 'upload_files');

}

and

if ( is_user_logged_in() )
   add_action('admin_init', 'allow_uploads');
function allow_uploads() {
   global $current_user;
   $current_user = get_current_user_id();
   $current_user->add_cap( 'upload_files');
}
1
  • You're adding the capability for the current_user upload files, that means that all users logged in, will upload files. What you have to do is: create a capability can_upload_files (or something like that), and when a user is registered, if you want add it to them. then when someone try to upload a file, check if the user has this capability, if he has you let him do it, if not throw a exception. – Cesar Henrique Damascena Jul 12 '17 at 12:54
1

Look into switch_to_blog() and its required follow up, restore_current_blog()

I left comments in code for basic idea to adapt into your function.

It may need tweaking, and can probably be improved.

//Site B
add_action( 'init', 'site_a_author_upload_site_b' );

function site_a_author_upload_site_b() {
    if ( is_user_logged_in() ) {
       //get a WP_User object of current user
       $current_user = wp_get_current_user();

       //ID of Site A
       $site_a = 1;

       //Switch to Site A
       switch_to_blog( $site_a );

       //get current user's roles on Site A
       $get_role = $current_user->roles;

       //Check if logged in user is an author on Site A
       if ( in_array( 'author', $get_role ) ) {

           //if yes, let's 1) restore current blog (Site B)
           restore_current_blog();

           //and 2) Add the cap we want to add to Wp_User object
           $current_user->add_cap( 'upload_files' );
       }

       else {

        //be sure we have restored current blog
        restore_current_blog();
      }
   }//end if logged in


}//end site_a_author_upload_site_b()
2
  • Perfect, thank! it's work. But there is a syntax error in your code. Anyway, it's work very well! – Andrea A Jul 12 '17 at 22:27
  • awesome! fixed the missing ` ) ` in if ( in_array( 'author', $get_role ) ), too. – hwl Jul 12 '17 at 23:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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