Error when upload image in author role

Support MB Frontend Submission Error when upload image in author roleResolved

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #16464
    proyectohappyweb@gmail.com[email protected]
    Participant

    Hello,

    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:

    https://d.pr/i/zxmW1x

    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,
    Sergio

    #16469
    proyectohappyweb@gmail.com[email protected]
    Participant

    Mention that with the editor role, you can upload or attach images from back office and from the front end without any problem.

    #16481
    Anh TranAnh Tran
    Keymaster

    That probably relates with the permission that WordPress gives to the author. Please try with the code that adjust capabilities in this docs.

    #16489
    proyectohappyweb@gmail.com[email protected]
    Participant

    Thanks. 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,
    Sergio

    #16499
    proyectohappyweb@gmail.com[email protected]
    Participant

    Hi, I could solve the problem, but I don't know why this error ...

    https://stackoverflow.com/questions/31885086/error-you-dont-have-permission-to-attach-files-to-this-post-on-wordpress

    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,
    Sergio

    #16527
    Anh TranAnh Tran
    Keymaster

    Hi 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' );
    } );
    #16559
    proyectohappyweb@gmail.com[email protected]
    Participant

    Hello,

    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

Viewing 7 posts - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.