Error when upload image in author role
Support › MB Frontend Submission › Error when upload image in author roleResolved
- This topic has 6 replies, 2 voices, and was last updated 5 years, 7 months ago by
[email protected].
-
AuthorPosts
-
October 10, 2019 at 7:04 PM #16464
[email protected]
ParticipantHello,
I have the user role author. In the back office, when I create a post or cpt, this role can add / upload an image with the native upload wordpress.
But in the front end, this role can select the image, but there is an error when upload the image:
The image is not upload and it appear a message:
"Sorry, you do not have permissions to attach files to this entry."Do you know how to solve it?
Thanks,
SergioOctober 11, 2019 at 1:45 AM #16469[email protected]
ParticipantMention that with the editor role, you can upload or attach images from back office and from the front end without any problem.
October 11, 2019 at 2:51 PM #16481Anh Tran
KeymasterThat probably relates with the permission that WordPress gives to the author. Please try with the code that adjust capabilities in this docs.
October 11, 2019 at 11:26 PM #16489[email protected]
ParticipantThanks. It has not worked for me ... I do not know if it will be a problem of permissions, because in the back office cpt if I upload images without problems, it is only in the front office that it does not leave me. (with permission of administrator and editor if you let me upload images in the frontend).
I have read in a post this possible solution, but with this function my web breaks.
https://es.wordpress.org/support/topic/edit_post-habilitado-pero-error-al-subir-media-en-frontend/
function add_media_upload_scripts() {
if ( is_admin() ) {
return;
}
wp_enqueue_media();
}
add_action('wp_enqueue_scripts', 'add_media_upload_scripts');from the original post:
https://derekspringer.wordpress.com/2015/05/07/using-the-wordpress-media-loader-in-the-front-end/I don't know if this information can help or not ...
Thanks,
SergioOctober 13, 2019 at 12:24 AM #16499[email protected]
ParticipantHi, I could solve the problem, but I don't know why this error ...
Just Update the wp-admin/includes/ajax-action.php file, instead of 'edit_post' it should be 'edit_posts'
if ( isset( $_REQUEST['post_id'] ) ) { $post_id = $_REQUEST['post_id']; if ( ! current_user_can( 'edit_posts', $post_id ) ) { echo wp_json_encode( array( 'success' => false, 'data' => array( 'message' => __( "You don't have permission to attach files to this post." ), 'filename' => $_FILES['async-upload']['name'], ) ) ); wp_die(); } }
I update the file and write edit_post and now author can upload images in the front end...
I don't know if this modification will affect me to other things, and I don't see it normal to have to modify wordpress files ... but I see that I am not the first person to have this happened ...
Greetings,
SergioOctober 15, 2019 at 4:32 PM #16527Anh Tran
KeymasterHi Sergio,
Please don't edit WordPress core files. When WordPress updates, it removes your changes.
I guess the problem here is author doesn't have permission to edit others' posts. In case on the front end, WordPress might think it's not the author's posts, since you're actually on the Edit page (the page that you paste the shortcode).
So, maybe modifying the user permissions might help. Here is the modified code from the code that allows uploads:
add_action( 'init', function () { if ( is_admin() ) { return; } $author = get_role( 'author' ); $author->add_cap( 'edit_posts' ); } );
October 16, 2019 at 11:14 PM #16559[email protected]
ParticipantHello,
Thanks for your reply, finally I can solve this problem with this capabilities:
$author = get_role( 'author' ); $author->add_cap( 'upload_files' ); $author->add_cap( 'edit_others_pages' ); $author->add_cap( 'edit_published_pages' );
Greetings,
Sergio -
AuthorPosts
- You must be logged in to reply to this topic.