0

I added TinyMCE editor to fronend page via wp_editor() function. I am trying to give user ability to upload images via "Add Media" button in that editor.

Which permissions should I give to user for he have ability to upload images?

This user should have ability to edit/delete his uploaded images, and should not be able to edit/delete other users images.

  • What is the actual problem? Whether the 'Add Media' button is not showing OR you want the user to only be able to edit/delete his uploaded images? – Prasad Nevase Oct 8 '15 at 18:09
  • The "Add Media" button is shown on frontend. But with different permissions creates different prolems. Or I got error that user are not able to upload images to post, or user can edit/delete other users images. – webvitaly Oct 8 '15 at 18:54
2

Only logged in user can upload media from front end. Below code will allow users to see only their media files and not others. Put this in your theme's functions.php file. I hope this helps.


add_action( 'pre_get_posts', 'users_own_attachments');
function users_own_attachments( $wp_query_obj )
    {
        global $current_user, $pagenow;

        if ( $pagenow == 'upload.php' || ( $pagenow == 'admin-ajax.php' && !empty( $_POST[ 'action' ] ) && $_POST[ 'action' ] == 'query-attachments' ) ) {
            $wp_query_obj->set( 'author', $current_user->ID );
        }
    }
  • Thank you very much for your code. It works and it shows only user's images. Now if user is trying to upload image from frontend WP generates error "You don't have permission to attach files to this post.". Now user only can upload images via admin section and then choose them in frontend. – webvitaly Oct 8 '15 at 18:56
  • But anyway this is the correct answer already. Thanks. – webvitaly Oct 8 '15 at 19:05

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.