0

There are multiple users who have to post their own post on respective post types (like news, reports etc) with feature image upload, extra file (pdf, txt, excel) upload button from the front end which is approve by the admin from backend.

I have solve for feature image but hanging my mind for file upload from front end for the respective post.

I have upload the file also from backend using metabox but no idea how to make it working from frontend to upload.Any suggestion?

  • Do you want to use the Media Library upload? – Mayeenul Islam Sep 8 '15 at 13:20
0

Would this plugin possibly provide what you need? Frontend Uploader.

This plugin is a simple way for users to submit content to your site. The plugin uses a set of shortcodes to let you create highly customizable submission forms to your posts and pages. Once the content is submitted, it is held for moderation until you approve it.

  • no use of plugin please – clap Sep 8 '15 at 16:43
0

Use wp_handle_upload in a custom function

if ( $_FILES ) {
upload_user_file($_FILES['test_upload']);
}


if ( ! function_exists( 'upload_user_file' ) ) :
    function upload_user_file( $file = array(), $title = false ) {

        require_once ABSPATH.'wp-admin/includes/admin.php';

        $file_return = wp_handle_upload($file, array('test_form' => false));

        if(isset($file_return['error']) || isset($file_return['upload_error_handler'])){

            return false;

        }else{

            $filename = $file_return['file'];

            $attachment = array(
                'post_mime_type' => $file_return['type'],
                'post_content' => '',
                'post_type' => 'attachment',
                'post_status' => 'inherit',
                'guid' => $file_return['url']
            );

            if($title){
                $attachment['post_title'] = $title;
            }

            $attachment_id = wp_insert_attachment( $attachment, $filename );

            require_once(ABSPATH . 'wp-admin/includes/image.php');

            $attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename );

            wp_update_attachment_metadata( $attachment_id, $attachment_data );

            if( 0 < intval( $attachment_id ) ) {
                return $attachment_id;
            }
        }

        return false;
    }
endif;

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.