Allow "customer" user role to upload files in front-end with acf_form

5k views Asked by At

I've created a front-end form with acf_form() ( advanced custom field front-end form ) in woocommerce "view order" page which allows customers to upload some files for us , the form works fine for administrators but when you log-in with a customer account and you choose your file it says "you don't have permission to attach files" , after some researches i edited "customer" by adding below code to functions.php :

/**
 * Allow customers to upload files
 *
 * @package Wordpress
 * @subpackage Rightec Theme
 * @author Dornaweb.com
 */
if ( current_user_can('customer') ) {
    add_action('init', 'allow_customer_uploads', 20);
    add_action('admin_init', 'allow_customer_uploads', 20);
}
function allow_customer_uploads() {
    $customer = get_role('customer');
    $customer->add_cap('upload_files');
    $customer->add_cap('unfiltered_upload');
}

i as well tried "user role editor" plugin , but it doesn't work too

help me please!

5

There are 5 answers

1
andycrone On BEST ANSWER

I also had this problem trying to allow a new WooCommerce customer to upload files against an order. In addition to the permissions above, you need to:

  1. Make sure you are using ACF Pro (have to purchase it).
  2. Define 'uploader' => 'basic' in your acf_form() array.
  3. Add the capabilities to 'edit_pages' and 'edit_posts' to the customer role.

You need ACF Pro to support the basic uploader, which you need because the WP Media uploader will never allow uploads from the frontend unless you're an administrator.

Hope this helps someone!

0
Sergey Nepomnyaschiy On

Solution was easy. Give the user those capabilities:

$author->add_cap('manage_options');
0
Joe Mester On

upload_files was the only capability I needed to add in order to achieve this functionality. See @toni_lehtimaki comment in the first answer.

0
Tim On

Recently came across the same issue, found this post and now the solution. The required capabilities are upload_files in order to support the WP media uploader instead of the basic upload field and the capability edit_published_pages is required in order to fix the permission restriction 'You don't have permission to attach files to this post.'

function customer_file_upload_permissions() {

    $user = wp_get_current_user();

    // If user is customer
    if (in_array('customer', (array) $user->roles)) {
        
        // If user can't upload files
        if (!current_user_can('upload_files') || !current_user_can('edit_published_pages')) {

            // Grant permission for file uploads for customer user role
            $customer = get_role('customer');

            // required in order to upload images via wp media --> acf_form 'uploader' => 'wp' support, instead of 'basic'
            $customer->add_cap('upload_files'); 
            
            // Fix error: You don't have permission to attach files to this post.
            // https://support.advancedcustomfields.com/forums/topic/permission-issue-on-frontend-image-upload/
            $customer->add_cap('edit_published_pages');
        }
    }

}
add_action('init', 'customer_file_upload_permissions');
0
Duc Manh Nguyen On

I'm using ACF Pro 5.8.0 and i have add cap to author role with this function below and define 'uploader' => 'basic' or 'uploader' => 'wp' but author still can't upload in frontend.

function wp_add_upload_files_cap() {
    $role = get_role( 'author' ); //The role you want to grant the capability
    $role->add_cap( 'upload_files' );
    $role->add_cap( 'edit_pages' );
    $role->add_cap( 'edit_posts' );
}
register_activation_hook( __FILE__, 'wp_add_upload_files_cap' );