I have a form with a WP Media to allow user uploads for a custom post type on the front end. Every time I try to upload as a user I can the message You don't have permission to attach files to this post.
Investigating it further, I get denied action in the file ajax-actions.php
where it checks current_user_can( 'edit_post', $post_id )
before uploading the file. I've bent myself backwards trying to user user_has_cap
filter to allow user to this action, but it looks awfully unsecured:
public function allow_user_to_upload( $allcaps, $caps, $args ) {
if ( $args[2] != 9 ) // 9 is the post_id where the form is
return $allcaps;
foreach ($caps as $cap ) {
$allcaps[$cap] = true;
}
return $allcaps;
} add_filter( 'user_has_cap', array( $this, 'allow_user_to_upload'), 100, 3 );
This works but it looks terribly unsecured. There must be a better way to do this, please help!!!
edit_post
since that's what it is called for in theajax-actions.php
file, but it denies onunfiltered_html
cap first. If I try to allowunfiltered_html
then it fails on a different capability and so on... I've never used this filter before, so I don't really know what i am doing. Fsenna, thanks for your link, I didn't see anything on user capabilities but I will study it further, I'll probably learn something new. – Caio Mar May 13 '16 at 5:21upload_files
capability? Are you mapping the capabilities of the CPT to deafult "post" or are you using customcapability_type
? – cybmeta May 13 '16 at 5:45customer
role, the CPT's capabilities are mapped topost
andcustomer
role hasupload_files
capability. Like I said, I don't know how to work around this while keeping everything in the safe side. A page ( of the post_type 'page') holds the form to submit the CPT, so whencurrent_user_can('edit_post', $post_id)
is called,$post_id
is a type ofpage
post_type, and customers aren't allowed to edit, which makes sense the denial... but yet there must be a way to safely accept such request. – Caio Mar May 13 '16 at 5:57