Want to insert attachments from front end form in WordPress? This handles the uploaded files by attaching them to the post and display them in the loop. Still no featured image is set.
Use following code in your theme’s functions.php file OR in site specific plugin.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
if ( $_FILES ) { foreach ( $_FILES as $file => $array ) { $newupload = insert_attachment( $file, $pid_buy ); } } function insert_attachment( $file_handler, $post_id, $setthumb='false' ) { 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 ); return $attach_id; } function show_all_thumbs() { // displaying the attached thumbs global $post; $post = get_post( $post ); /* image code */ $images =& get_children( 'post_type=attachment&post_mime_type=image&output=ARRAY_N&orderby=menu_order&order=ASC&post_parent=' . $post->ID ); if ( $images ){ foreach ( $images as $imageID => $imagePost ){ unset( $the_b_img ); $the_b_img = wp_get_attachment_image( $imageID, 'thumbnail', false ); $thumblist .= '<a href="'.get_attachment_link($imageID).'">'.$the_b_img.'</a>'; } } return $thumblist; } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.