2

We have a front-end form with wp-editor on front-end template page. Everything is working fine. but the only problem is "we can't add new images on front end form". there is an error "An error occurred in the upload. Please try again later"

This front end shows only the author own files.

// Show only posts and media related to logged in author
add_action('pre_get_posts', 'query_set_only_author' );
function query_set_only_author( $wp_query ) {
    global $current_user;
    if( is_admin() && !current_user_can('edit_others_posts') ) {
        $wp_query->set( 'author', $current_user->ID );
        add_filter('views_edit-post', 'fix_post_counts');
        add_filter('views_upload', 'fix_media_counts');
    }
}

If the user is "Editor" or "Administrator" it works ok. then I think role problem with upload capibility.

but I setup capability correctly.

 function tour_all_user_attachment_callback(){
    wp_get_current_user()->add_cap('upload_files');
 }
 add_action('init', 'tour_all_user_attachment_callback');

And I added this from here ( exposing attachment uploading to the front end -- delicate )

 function insert_attachment($file_handler,$post_id,$setthumb='false') {
  // check to make sure its a successful upload
    if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK) __return_false();
    require_once(ABSPATH . "wp-admin" . '/includes/image.php');
    require_once(ABSPATH . "wp-admin" . '/includes/file.php');
    require_once(ABSPATH . "wp-admin" . '/includes/media.php');
    $attach_id = media_handle_upload( $file_handler, $post_id );

    if ($setthumb) update_post_meta($post_id,'_thumbnail_id',$attach_id);
    return $attach_id;
  }


  // set $post_id to the id of the post you want to attach
  // these uploads to (or 'null' to just handle the uploads
  // without attaching to a post)

  if ($_FILES) {
    foreach ($_FILES as $file => $array) {
      $newupload = insert_attachment($file,$post_id);
      // $newupload returns the attachment id of the file that
      // was just uploaded. Do whatever you want with that now.
    }
  }

it makes better. after I upload files it shows the error but it uploaded. I have to refresh and I can see the files uploaded. but still it gets the error and I had to refresh.

any tips?

2
  • same here , anyone know how to fix ?
    – Ayd In
    Commented Aug 17, 2016 at 18:44
  • Are they large files? You might want to check upload_max_filesize in the php.ini configuration.
    – brandozz
    Commented Aug 17, 2016 at 18:58

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.