From wordpress 3.0 version wordpress introduced the custom_post_type function. Many people want to attach the file field with add_meta_box function. In this tutorial I will tell you how to upload file with custom meta box and post type.
In this tutorial I will show you how to create the custom post type and add custom meta boxes to that post type and upload file with custom meta box.
After digging into wordpress files and functions I created following code. Just open your functions.php file and put following code in that file for creating custom post type.
__('Product'), 'singular_label' => __('Product'), 'public' => true, 'show_ui' => true, 'capability_type' => 'post', 'hierarchical' => false, 'rewrite' => true, 'supports' => array('title', 'editor', 'thumbnail') ); register_post_type('product',$product_args); } ?>
For uploading the file through custom meta box use the following code. Following code will add the file field to custom meta box and you are able to upload file or image to wordpress and upload file attachment to you custom post. Following code is very helpful to many wordpress theme and plugin developer. If you are having any issues or trouble using code then get back to me.
Image may be NSFW.
Clik here to view.
ID); $purchase_url = $custom["purchase_url"][0]; $product_price = $custom["product_price"][0]; $product_image = $custom["product_image"][0]; $video_code = $custom["video_code"][0]; ?> <div id="product-options"> <label>Purchase URL:</label><input name="purchase_url" size="100" value="<?php echo $purchase_url; ?>" /> <label>Product Price:</label><input name="product_price" size="100" value="<?php echo $product_price; ?>" /> <label>Product Image:</label><input name="product_image" size="100" type="file" value="<?php echo $product_image; ?>" /> <img src="<?php echo $product_image; ?>" alt="" /> </div> <!--end product-options--> ID, "purchase_url", $_POST["purchase_url"]); update_post_meta($post->ID, "product_price", $_POST["product_price"]); update_post_meta($post->ID, "product_image", $_POST["product_image"]); if(!empty($_FILES['product_image']['name'])){ //New upload require_once( ABSPATH . 'wp-admin/includes/file.php' ); $override['action'] = 'editpost'; $uploaded_file = wp_handle_upload($_FILES['product_image'], $override); $post_id = $post->ID; $attachment = array( 'post_title' => $_FILES['product_image']['name'], 'post_content' => '', 'post_type' => 'attachment', 'post_parent' => $post_id, 'post_mime_type' => $_FILES['product_image']['type'], 'guid' => $uploaded_file['url'] ); // Save the data $id = wp_insert_attachment( $attachment,$_FILES['product_image'][ 'file' ], $post_id ); wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $_FILES['product_image']['file'] ) ); update_post_meta($post->ID, "product_image", $uploaded_file['url']); } } ?> For Uploading the file your post form need to add the enctype=”multipart/form-data” type to your form. Just use the following code in your functions.php file. 1 <script type="text/javascript"><!--mce:0--></script>
For checking all information in edit or preview section use following code.
"<input type="\"checkbox\"" />", "title" => "Product Title", "purchase_url" => "Purchase URL", "product_price" => "Product Price", "product_image" => "Product Image", ); return $columns; } function product_custom_columns($column){ global $post; switch ($column) { case "purchase_url": $custom = get_post_custom(); echo $custom["purchase_url"][0]; break; case "product_price": $custom = get_post_custom(); echo $custom["product_price"][0]; break; case "product_image": $custom = get_post_custom(); $img_url =$custom["product_image"][0]; echo "<img src=".$img_url." alt="" width="100" height="100" />"; break; } } ?>
Image may be NSFW.
Clik here to view.
Above code is useful for any type of customization. If you are having any issue with using this code then please get back to me.
Here is list of articles which is most useful for adding functionality in wordpress without adding any wordpress plugins.
Best wordpress hacks by WordPressAPI.com
how to create contact us page without wordpress plugin
Add the extra new users details or fields to wordpress without plugin
Display wordpress Tags In A Dropdown Menu without plugin
How to change the Visual Editor Font Size wordpress without plugin
Display wordpress Tags In A Dropdown Menu without plugin
wordpress pagination style without wordpress plugin
show popular posts without wordpress plugin in theme
How to send smtp email through wordpress without plugin
How to put digg button in wordpress without plugin
Get the recent comments without using wordpress plugin or widget
How to Add the social Bookmar Icons in WordPress theme without wordpress plugins
How to exclude pages from wordpress menu without plugin
Show related posts with wordpress post without using any plugin
Image may be NSFW.
Clik here to view.
Incoming search terms:
- add_meta_box
- wordpress add_meta_box
- wp insert_attachment
- wordpress add_meta_box image
- facebook twitter icon png
- add_meta_box wordpress
- add_meta_box add css
- create a fileupload in wordpress
- facebook icons
- facebook icon png
- wordpress image upload
- mp3 attachments from post wordpress
- add_meta_box image upload
- metabox upload pdf wordpress
- add_meta_box image input
- wordpress form update after update_post_meta
- WordPress Custom Post Type with Image Upload
- wordpress custom meta box upload mp3
- wordpress custom meta box file upload
- wordpress attach custom files to post
Follow us on Twitter WordPress API