Advertisement
wzislam

WordPress Edit Post

May 28th, 2013
364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 10.30 KB | None | 0 0
  1. <?php
  2. /*
  3. Template Name: Edit Product
  4. */
  5. get_header();
  6. if (is_user_logged_in() ) {
  7. ?>
  8.  
  9. <?php
  10. //error_reporting(0);
  11. if(isset ($_POST['submit'])=='UPDATE')
  12. {
  13.    
  14.     $postid=$_POST['prodId'];
  15.     $prodName=$_POST['prodName'];
  16.     $category=$_POST['category'];
  17.     $prodBrand=$_POST['product_brand'];
  18.     $prodModel=$_POST['product_model'];
  19.     $prodCondition=$_POST['product_condition']; // new/ used
  20.     $prodPrice=$_POST['product_price'];
  21.     $prodMktPrice=$_POST['market_price'];
  22.     $prodDescription=$_POST['prodDescription'];
  23.         // PRODUCT IMAGES
  24.         $prodImage1=$_FILES['product_image_1']['name'];
  25.         $prodImage2=$_FILES['product_image_2']['name'];
  26.         $prodImage3=$_FILES['product_image_3']['name'];
  27.         $prodImage4=$_FILES['product_image_4']['name'];
  28.    
  29.     if($prodName=="")
  30.     {
  31.         $setval=0;
  32.         $pnameerror="You forgot to enter a Product Title";
  33.     }
  34.     else
  35.     {
  36.         $setval=1;
  37.         $pnameerror="";
  38.     }
  39.     if($prodPrice=="")
  40.     {
  41.         $setval4=0;
  42.         $priceerror="Please enter a value for the Product Price field";
  43.     }
  44.     else
  45.     {
  46.         $setval4=1;
  47.         $priceerror="";
  48.     }
  49.    
  50.     if($setval==1 && $setval4==1)
  51.     {
  52.     $postdate = date('Y-m-d H:m:s');
  53.     $post_slug = sanitize_title( $prodName );
  54.     $post = array(
  55.                     'ID'      => $postid,
  56.                     'post_title'    => wp_strip_all_tags($prodName),
  57.                     'post_name'     => $post_slug,
  58.                     'post_content'     => $prodDescription,
  59.                     'post_category'    =>array( $category ),  // Usable for custom taxonomies too
  60.                     'post_date'     =>   $postdate,
  61.                     'post_status'    => 'publish'  // Choose whether 'draft' | 'publish' | 'pending'| 'future' | 'private' | or, custom registered status
  62.                 );
  63.     // Pass  the value of $post to WordPress the insert function
  64.     // http://codex.wordpress.org/Function_Reference/wp_insert_post
  65.     wp_update_post($post);
  66.    
  67.             require_once(ABSPATH . 'wp-admin/includes/admin.php');
  68.             require_once (ABSPATH . 'wp-admin/includes/media.php');
  69.             require_once (ABSPATH . 'wp-admin/includes/file.php');
  70.             require_once (ABSPATH . 'wp-admin/includes/image.php');
  71.    
  72.     update_post_meta($postid, 'product_brand', $prodBrand);
  73.     update_post_meta($postid, 'product_model', $prodModel);
  74.     update_post_meta($postid, 'product_price', $prodPrice);
  75.     update_post_meta($postid, 'market_price', $prodMktPrice);
  76.     update_post_meta($postid, 'product_condition', $prodCondition); // New or Old
  77.     // PRODUCT IMAGE
  78.     if($prodImage1!="")
  79.     {
  80.         $attachmentId1 = media_handle_upload($postid,'product_image_1');
  81.         update_post_meta($postid, 'product_image_1', $attachmentId1);
  82.         // TO MAKE THE IMAGE A POST THUMBNAIL
  83.         set_post_thumbnail($postid, $attachmentId1);   
  84.     }
  85.     if($prodImage2!="")
  86.     {
  87.         $attachmentId2 = media_handle_upload($postid, 'product_image_2');  
  88.         update_post_meta($postid, 'product_image_2', $attachmentId2);
  89.     }
  90.  
  91.     if($prodImage3!='')
  92.     {
  93.         $attachmentId3 = media_handle_upload($postid,'product_image_3');   
  94.         update_post_meta($postid, 'product_image_3', $attachmentId3);
  95.     }
  96.  
  97.     if($prodImage4!="")
  98.     {
  99.         $attachmentId4 = media_handle_upload($postid,'product_image_4');   
  100.         update_post_meta($postid, 'product_image_4', $attachmentId4);
  101.     }
  102.    
  103.     //$location=home_url('/my-products');
  104. ?>
  105.    <script type="text/javascript">
  106.    <!--
  107.     //  window.location= <?php echo "'" . $location . "'"; ?>;
  108.    //-->
  109.    </script>
  110. <?php
  111. }}
  112. ?>
  113.  
  114. <?php
  115. $post_id=$_GET['post'];
  116. $queried_post = get_post($post_id);
  117. $custom = get_post_custom($post_id);
  118.  ?>
  119.        
  120.         <?php get_sidebar(); ?>
  121.        
  122.         <section id="main">
  123.            
  124.             <h1 class="entry-title">Edit Product</h1>
  125.             <br/>
  126.             <div class="entry-content">
  127.             <form name="new_post" id="new_post" action="" method="post"  enctype="multipart/form-data">
  128.             <input type="hidden" name="prodId" value="<?php echo $post_id;?>"  />
  129.            
  130.                 <div id="edit-product-table">
  131.                     <div><label for="fname" class="left bold">Product Title <span class="required">*</span></label></div>
  132.                     <div><input type="text" class="right" name="prodName" value="<?php echo $queried_post->post_title;?>"/><br/>
  133.                     <div class="validationerror"><?php if( isset($pnameerror) ){ echo $pnameerror; }?></div></div>
  134.                     <br/>
  135.                     <div><label for="fname" class="left bold">Product Category</label></div>
  136.                     <div>
  137.                     <?php $get_category = get_the_category( $post_id ); ?>
  138.                     <?php $cat_id = $get_category[0]->cat_ID; ?>
  139.                     <?php $args = array(
  140.                         'show_option_all'    => '',
  141.                         'show_option_none'   => '',
  142.                         'orderby'            => 'ID',
  143.                         'order'              => 'ASC',
  144.                         'show_count'         => 0,
  145.                         'hide_empty'         => 0,
  146.                         'child_of'           => 0,
  147.                         'exclude'            => '1',
  148.                         'echo'               => 1,
  149.                         'selected'           => $cat_id,
  150.                         'hierarchical'       => 1,
  151.                         'name'               => 'category',
  152.                         'id'                 => 'thecategory',
  153.                         'class'              => 'postform edit-product-categories',
  154.                         'depth'              => 0,
  155.                         'tab_index'          => 0,
  156.                         'taxonomy'           => 'category',
  157.                         'title_li' => __( 'Categories' ),
  158.                         'hide_if_empty'      => false
  159.                         ); ?>
  160.                     <?php wp_dropdown_categories( $args ); ?>                    
  161.                     <script>
  162.                         //document.getElementById('thecategory').selected="selected";
  163.                     </script>
  164.                     </div>
  165.                     <br/>
  166.                     <div><label for="prodDescription" class="left bold">Product Details</label></div>
  167.                     <div><textarea cols="68" rows="10" name="prodDescription"><?php echo $queried_post->post_content; ?></textarea></div>
  168.                     <br/>
  169.                     <div  style="width: 44%; position: relative; float: left;">
  170.                         <div><label for="product_price" class="left bold">Product Price <span class="required">*</span></label></div>
  171.                         <div><input type="text" class="right" name="product_price" value="<?php echo $custom['product_price'][0];?>"/><br/>
  172.                         <div class="validationerror"><?php if( isset($priceerror) ){ echo $priceerror; }?></div></div>
  173.                     </div>
  174.                     <div  style="width: 44%; position: relative; float: left;">
  175.                         <div><label for="market_price" class="left bold">Market Price</label></div>
  176.                         <div><input type="text" class="right" name="market_price" value="<?php echo $custom['market_price'][0];?>"/></div>
  177.                     </div>
  178.                    
  179.                     <div style="clear:both;"></div><br/>
  180.                    
  181.                     <div  style="width: 44%; position: relative; float: left;">
  182.                         <div><label for="product_brand" class="left bold">Brand</label></div>
  183.                         <div><input type="text" class="right" name="product_brand" value="<?php echo $custom['product_brand'][0];?>"/></div>
  184.                     </div>
  185.                    
  186.                     <div  style="width: 44%; position: relative; float: left;">
  187.                         <div><label for="product_model" class="left bold">Model</label></div>
  188.                         <div><input type="text" class="right" name="product_model" value="<?php echo $custom['product_model'][0];?>"/></div>
  189.                     </div>
  190.                    
  191.                     <div style="clear:both;"></div><br/>
  192.                     <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 &nbsp; <input type="radio" value="Old" name="product_condition" <?php if ( $custom['product_condition'][0] == 'Old' ) { echo 'checked="checked"'; } ?>/> Old</div>
  193.                     <br/>
  194.                     <div><label for="product_images" class="left bold">Product Image[s]</label></div>
  195.                     <ul id="edit-product-img-field" style="margin-top: 2%;">
  196.                         <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>
  197.                         <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>
  198.                         <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>
  199.                         <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>
  200.                     </ul>
  201.                     <br/>
  202.                     <div>
  203.                         <?php //cryptographic 'nonce' field will load here, but will be hidden ?>
  204.                         <input type="hidden" name="action" value="new_post" />
  205.                         <?php wp_nonce_field( 'new-post' ); ?>
  206.                         <input type="submit" name="submit" value="UPDATE" style="padding: 0.5% 2%; font-weight: bold; cursor: pointer; border: 1px solid #333;"/>
  207.                     </div>
  208.                    
  209.                 </div> <!-- #edit-product-table -->
  210.                
  211.             </div><!-- .entry-content -->
  212.            
  213.         </section>
  214. <?php
  215. }
  216. else
  217. {
  218.     $location=home_url('/wp-login.php');
  219. ?>
  220.    <script type="text/javascript">
  221.    <!--
  222.       window.location= <?php echo "'" . $location . "'"; ?>;
  223.    //-->
  224.    </script>
  225. <?php
  226. }
  227.  
  228. get_footer(); ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement