Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Template Name: Edit Product
- */
- get_header();
- if (is_user_logged_in() ) {
- ?>
- <?php
- //error_reporting(0);
- if(isset ($_POST['submit'])=='UPDATE')
- {
- $postid=$_POST['prodId'];
- $prodName=$_POST['prodName'];
- $category=$_POST['category'];
- $prodBrand=$_POST['product_brand'];
- $prodModel=$_POST['product_model'];
- $prodCondition=$_POST['product_condition']; // new/ used
- $prodPrice=$_POST['product_price'];
- $prodMktPrice=$_POST['market_price'];
- $prodDescription=$_POST['prodDescription'];
- // PRODUCT IMAGES
- $prodImage1=$_FILES['product_image_1']['name'];
- $prodImage2=$_FILES['product_image_2']['name'];
- $prodImage3=$_FILES['product_image_3']['name'];
- $prodImage4=$_FILES['product_image_4']['name'];
- if($prodName=="")
- {
- $setval=0;
- $pnameerror="You forgot to enter a Product Title";
- }
- else
- {
- $setval=1;
- $pnameerror="";
- }
- if($prodPrice=="")
- {
- $setval4=0;
- $priceerror="Please enter a value for the Product Price field";
- }
- else
- {
- $setval4=1;
- $priceerror="";
- }
- if($setval==1 && $setval4==1)
- {
- $postdate = date('Y-m-d H:m:s');
- $post_slug = sanitize_title( $prodName );
- $post = array(
- 'ID' => $postid,
- 'post_title' => wp_strip_all_tags($prodName),
- 'post_name' => $post_slug,
- 'post_content' => $prodDescription,
- 'post_category' =>array( $category ), // Usable for custom taxonomies too
- 'post_date' => $postdate,
- 'post_status' => 'publish' // Choose whether 'draft' | 'publish' | 'pending'| 'future' | 'private' | or, custom registered status
- );
- // Pass the value of $post to WordPress the insert function
- // http://codex.wordpress.org/Function_Reference/wp_insert_post
- wp_update_post($post);
- require_once(ABSPATH . 'wp-admin/includes/admin.php');
- require_once (ABSPATH . 'wp-admin/includes/media.php');
- require_once (ABSPATH . 'wp-admin/includes/file.php');
- require_once (ABSPATH . 'wp-admin/includes/image.php');
- update_post_meta($postid, 'product_brand', $prodBrand);
- update_post_meta($postid, 'product_model', $prodModel);
- update_post_meta($postid, 'product_price', $prodPrice);
- update_post_meta($postid, 'market_price', $prodMktPrice);
- update_post_meta($postid, 'product_condition', $prodCondition); // New or Old
- // PRODUCT IMAGE
- if($prodImage1!="")
- {
- $attachmentId1 = media_handle_upload($postid,'product_image_1');
- update_post_meta($postid, 'product_image_1', $attachmentId1);
- // TO MAKE THE IMAGE A POST THUMBNAIL
- set_post_thumbnail($postid, $attachmentId1);
- }
- if($prodImage2!="")
- {
- $attachmentId2 = media_handle_upload($postid, 'product_image_2');
- update_post_meta($postid, 'product_image_2', $attachmentId2);
- }
- if($prodImage3!='')
- {
- $attachmentId3 = media_handle_upload($postid,'product_image_3');
- update_post_meta($postid, 'product_image_3', $attachmentId3);
- }
- if($prodImage4!="")
- {
- $attachmentId4 = media_handle_upload($postid,'product_image_4');
- update_post_meta($postid, 'product_image_4', $attachmentId4);
- }
- //$location=home_url('/my-products');
- ?>
- <script type="text/javascript">
- <!--
- // window.location= <?php echo "'" . $location . "'"; ?>;
- //-->
- </script>
- <?php
- }}
- ?>
- <?php
- $post_id=$_GET['post'];
- $queried_post = get_post($post_id);
- $custom = get_post_custom($post_id);
- ?>
- <?php get_sidebar(); ?>
- <section id="main">
- <h1 class="entry-title">Edit Product</h1>
- <br/>
- <div class="entry-content">
- <form name="new_post" id="new_post" action="" method="post" enctype="multipart/form-data">
- <input type="hidden" name="prodId" value="<?php echo $post_id;?>" />
- <div id="edit-product-table">
- <div><label for="fname" class="left bold">Product Title <span class="required">*</span></label></div>
- <div><input type="text" class="right" name="prodName" value="<?php echo $queried_post->post_title;?>"/><br/>
- <div class="validationerror"><?php if( isset($pnameerror) ){ echo $pnameerror; }?></div></div>
- <br/>
- <div><label for="fname" class="left bold">Product Category</label></div>
- <div>
- <?php $get_category = get_the_category( $post_id ); ?>
- <?php $cat_id = $get_category[0]->cat_ID; ?>
- <?php $args = array(
- 'show_option_all' => '',
- 'show_option_none' => '',
- 'orderby' => 'ID',
- 'order' => 'ASC',
- 'show_count' => 0,
- 'hide_empty' => 0,
- 'child_of' => 0,
- 'exclude' => '1',
- 'echo' => 1,
- 'selected' => $cat_id,
- 'hierarchical' => 1,
- 'name' => 'category',
- 'id' => 'thecategory',
- 'class' => 'postform edit-product-categories',
- 'depth' => 0,
- 'tab_index' => 0,
- 'taxonomy' => 'category',
- 'title_li' => __( 'Categories' ),
- 'hide_if_empty' => false
- ); ?>
- <?php wp_dropdown_categories( $args ); ?>
- <script>
- //document.getElementById('thecategory').selected="selected";
- </script>
- </div>
- <br/>
- <div><label for="prodDescription" class="left bold">Product Details</label></div>
- <div><textarea cols="68" rows="10" name="prodDescription"><?php echo $queried_post->post_content; ?></textarea></div>
- <br/>
- <div style="width: 44%; position: relative; float: left;">
- <div><label for="product_price" class="left bold">Product Price <span class="required">*</span></label></div>
- <div><input type="text" class="right" name="product_price" value="<?php echo $custom['product_price'][0];?>"/><br/>
- <div class="validationerror"><?php if( isset($priceerror) ){ echo $priceerror; }?></div></div>
- </div>
- <div style="width: 44%; position: relative; float: left;">
- <div><label for="market_price" class="left bold">Market Price</label></div>
- <div><input type="text" class="right" name="market_price" value="<?php echo $custom['market_price'][0];?>"/></div>
- </div>
- <div style="clear:both;"></div><br/>
- <div style="width: 44%; position: relative; float: left;">
- <div><label for="product_brand" class="left bold">Brand</label></div>
- <div><input type="text" class="right" name="product_brand" value="<?php echo $custom['product_brand'][0];?>"/></div>
- </div>
- <div style="width: 44%; position: relative; float: left;">
- <div><label for="product_model" class="left bold">Model</label></div>
- <div><input type="text" class="right" name="product_model" value="<?php echo $custom['product_model'][0];?>"/></div>
- </div>
- <div style="clear:both;"></div><br/>
- <div><label for="product_condition" class="left bold">New/Old</label><input type="radio" value="New" <?php if ( $custom['product_condition'][0] == 'New' ) { echo 'checked="checked"'; } ?> name="product_condition" style="margin-left: 10%;"/> New <input type="radio" value="Old" name="product_condition" <?php if ( $custom['product_condition'][0] == 'Old' ) { echo 'checked="checked"'; } ?>/> Old</div>
- <br/>
- <div><label for="product_images" class="left bold">Product Image[s]</label></div>
- <ul id="edit-product-img-field" style="margin-top: 2%;">
- <li><?php if ( $custom['_thumbnail_id'][0] ) { ?><img src="<?php echo wp_get_attachment_url($custom['_thumbnail_id'][0]); ?>" width="50" height="50" alt="Product Image 1"/><?php } ?> <input type="file" class="right" name="product_image_1"/></li>
- <li><?php if ( $custom['product_image_2'][0] ) { ?><img src="<?php echo wp_get_attachment_url($custom['product_image_2'][0]); ?>" width="50" height="50" alt="Product Image 2"/><?php } ?> <input type="file" class="right" name="product_image_2"/> </li>
- <li><?php if ( $custom['product_image_3'][0] ) { ?><img src="<?php echo wp_get_attachment_url($custom['product_image_3'][0]); ?>" width="50" height="50" alt="Product Image 3"/><?php } ?> <input type="file" class="right" name="product_image_3" id="product_image_3"/></li>
- <li><?php if ( $custom['product_image_4'][0] ) { ?><img src="<?php echo wp_get_attachment_url($custom['product_image_4'][0]); ?>" width="50" height="50" alt="Product Image 4"/><?php } ?> <input type="file" class="right" name="product_image_4"/></li>
- </ul>
- <br/>
- <div>
- <?php //cryptographic 'nonce' field will load here, but will be hidden ?>
- <input type="hidden" name="action" value="new_post" />
- <?php wp_nonce_field( 'new-post' ); ?>
- <input type="submit" name="submit" value="UPDATE" style="padding: 0.5% 2%; font-weight: bold; cursor: pointer; border: 1px solid #333;"/>
- </div>
- </div> <!-- #edit-product-table -->
- </div><!-- .entry-content -->
- </section>
- <?php
- }
- else
- {
- $location=home_url('/wp-login.php');
- ?>
- <script type="text/javascript">
- <!--
- window.location= <?php echo "'" . $location . "'"; ?>;
- //-->
- </script>
- <?php
- }
- get_footer(); ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement