Advertisement
Guest User

Untitled

a guest
Jul 17th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 21.04 KB | None | 0 0
  1. <?php
  2.    /*
  3.    Plugin Name: Import Properties by XML
  4.    Plugin URI:
  5.    description: A Custom plugin for Import Properties by XML
  6.    Version: 1
  7.    Author: Mrs. R.Kalra
  8.    Author URI:
  9.    License: GPL2
  10.    */
  11.    
  12.    
  13.  
  14. function wpb_image_editor_default_to_gd( $editors ) {
  15.     $gd_editor = 'WP_Image_Editor_GD';
  16.     $editors = array_diff( $editors, array( $gd_editor ) );
  17.     array_unshift( $editors, $gd_editor );
  18.     return $editors;
  19. }
  20. add_filter( 'wp_image_editors', 'wpb_image_editor_default_to_gd' );
  21.  
  22.  
  23. // Register Custom Taxonomy
  24. function custom_taxonomy() {
  25.  
  26.     $labels = array(
  27.         'name'                       => _x( 'Taxonomiesxx', 'Taxonomy General Name', 'text_domain' ),
  28.         'singular_name'              => _x( 'Taxonomyxx', 'Taxonomy Singular Name', 'text_domain' ),
  29.         'menu_name'                  => __( 'Taxonomy', 'text_domain' ),
  30.         'all_items'                  => __( 'All Items', 'text_domain' ),
  31.         'parent_item'                => __( 'Parent Item', 'text_domain' ),
  32.         'parent_item_colon'          => __( 'Parent Item:', 'text_domain' ),
  33.         'new_item_name'              => __( 'New Item Name', 'text_domain' ),
  34.         'add_new_item'               => __( 'Add New Item', 'text_domain' ),
  35.         'edit_item'                  => __( 'Edit Item', 'text_domain' ),
  36.         'update_item'                => __( 'Update Item', 'text_domain' ),
  37.         'view_item'                  => __( 'View Item', 'text_domain' ),
  38.         'separate_items_with_commas' => __( 'Separate items with commas', 'text_domain' ),
  39.         'add_or_remove_items'        => __( 'Add or remove items', 'text_domain' ),
  40.         'choose_from_most_used'      => __( 'Choose from the most used', 'text_domain' ),
  41.         'popular_items'              => __( 'Popular Items', 'text_domain' ),
  42.         'search_items'               => __( 'Search Items', 'text_domain' ),
  43.         'not_found'                  => __( 'Not Found', 'text_domain' ),
  44.         'no_terms'                   => __( 'No items', 'text_domain' ),
  45.         'items_list'                 => __( 'Items list', 'text_domain' ),
  46.         'items_list_navigation'      => __( 'Items list navigation', 'text_domain' ),
  47.     );
  48.     $args = array(
  49.         'labels'                     => $labels,
  50.         'hierarchical'               => false,
  51.         'public'                     => true,
  52.         'show_ui'                    => true,
  53.         'show_admin_column'          => true,
  54.         'show_in_nav_menus'          => true,
  55.         'show_tagcloud'              => true,
  56.     );
  57.     register_taxonomy( 'taxonomyxxx', array( 'post' ), $args );
  58.  
  59. }
  60. add_action( 'init', 'custom_taxonomy', 0 );
  61.  
  62.  
  63. add_action( 'admin_menu', 'prop_admin_menu' );
  64.  
  65. function prop_admin_menu() {
  66.   add_menu_page( 'Import Properties by XML', 'Import Properties by XML', 'manage_options', 'imp-prop-byxml', 'impor_prop_byxml_callback');
  67. }
  68.  
  69.  
  70. function impor_prop_byxml_callback()
  71. {
  72. ?>
  73.  
  74.     <div class="wrap"><h2>Import Properties by XML URL:</h2><br/>
  75.     <form action="" method="post">
  76.         <input type="hidden" name="action" value="xmlimpo" />
  77.         <input type="Submit" value="Click to Import New Properties!" />
  78.     </form>
  79.     <p>Please dont close the browser untill you get message - "properties are successfully imported", please refresh the url if you get any internal server error!</p>
  80.     <?php
  81.  
  82. if(isset($_GET['loop']) && $_GET['loop']!='')
  83. {
  84.    
  85.     $crloop = $_GET['loop'];
  86.     $nextloop = $_GET['loop'] + 1;
  87.         $url = 'https://mmzr.mycrm.com/feed/privatesite/71a35b4940147de1dda45876020b103b';
  88.         $geturl = file_get_contents($url);
  89.         $xmlcont = simplexml_load_string($geturl, 'SimpleXMLElement', LIBXML_NOCDATA);
  90.         $tcount = 1;
  91.         $totproperty = count($xmlcont->property);
  92.        
  93.         if($nextloop > $totproperty)
  94.         {
  95.             echo '<br/>Properties are successfull imported!';          
  96.         }
  97.         else
  98.         {
  99.             foreach($xmlcont->property as $prop)
  100.             {
  101.                if($tcount == $nextloop)
  102.                {
  103.                    
  104.                                 $p_title = $prop->title_en;
  105.                                 $p_desc = $prop->description_en;               
  106.                                 global $wpdb;
  107.                                 $newpstable = $wpdb->prefix.'posts';
  108.                                 $newpropresult = $wpdb->get_results( "SELECT ID FROM $newpstable WHERE post_title = '".$p_title."' and post_type like 'property' and post_status like 'publish' ", OBJECT );
  109.                                 if(empty($newpropresult))
  110.                                 {                      
  111.                                     $post_id = wp_insert_post(array (
  112.                                         'post_type' => 'property',
  113.                                         'post_title' => $p_title,
  114.                                         'post_content' => $p_desc,
  115.                                         'post_status' => 'publish',
  116.                                         'comment_status' => 'closed',   // if you prefer
  117.                                         'ping_status' => 'closed',      // if you prefer
  118.                                     ));
  119.                          
  120.                                 $photoarr = (array) $prop->photo->url;
  121.                                 unset($photoarr['@attributes']);
  122.                                 }
  123.                             if ($post_id) {
  124.                                
  125.                                
  126.                                
  127.                                 $parent_term = term_exists($prop->city, 'property_city' ); // array is returned if taxonomy is given
  128.                                                                 if(!empty($parent_term))
  129.                                 {
  130.                                 $set_city_termid = $parent_term['term_id'];
  131.                                 }
  132.                                 else
  133.                                 {
  134.                                 $city_termid = wp_insert_term(
  135.                                 $prop->city, // the term
  136.                                 'property_city', // the taxonomy
  137.                                  array(
  138.                                   'description'=> ''
  139.                                   )
  140.                                  );
  141.                                  $set_city_termid = $city_termid->term_id;
  142.                                  }
  143.                                 $posted = wp_set_post_terms( $post_id, $set_city_termid, 'property_city',true );
  144.                                
  145.                                
  146.                                 update_post_meta($post_id, 'fave_featured', '1');
  147.                                 update_post_meta($post_id, 'fave_property_id', ''.$prop->reference_number.'');
  148.                                 update_post_meta($post_id, 'fave_property_price', ''.$prop->price.'');
  149.                                 update_post_meta($post_id, 'fave_property_address', ''.$prop->sub_community.', '.$prop->community.','.$prop->city.'');
  150.                                 update_post_meta($post_id, 'fave_property_size', ''.$prop->size.'');
  151.                                 update_post_meta($post_id, 'fave_property_size_prefix', 'Sq Ft');
  152.                                
  153.                                 update_post_meta($post_id, 'fave_property_bedrooms', ''.$prop->bedroom.'');
  154.                                 update_post_meta($post_id, 'fave_property_bathrooms', ''.$prop->bathroom.'');
  155.                                 update_post_meta($post_id, 'fave_single_content_area', 'v4');
  156.                                 update_post_meta($post_id, 'fave_single_top_area', 'v4');
  157.                                 if($prop->amenities != '')
  158.                                 {
  159.                                    
  160.                                     $amennewarr = array();
  161.                                     $amen = $prop->amenities;
  162.                                     $amenexp = explode(',',$amen);
  163.                                     foreach($amenexp as $amenval)
  164.                                     {
  165.                                         $amenval = strtolower($amenval);
  166.                                         $term = get_term_by('slug', $amenval, 'propamenities');
  167.                                         $amennewarr[] = $term->name;
  168.                                     }
  169.                                     $impamen = implode(',',$amennewarr);
  170.                                     update_post_meta($post_id, 'fave_additional_features_enable', 'enable');
  171.                                     $returnValue = array(array('fave_additional_feature_title' => 'Amenities','fave_additional_feature_value' => ''.$impamen.'' ));
  172.                                    
  173.                                      update_post_meta($post_id, 'additional_features',$returnValue);
  174.                                 }
  175.                                 $loc = $prop->geopoints;
  176.                                 $locexp = explode(',',$loc);
  177.                                 update_post_meta($post_id, 'fave_property_location', ''.$locexp[1].','.$locexp[0].'');
  178.                                 if($prop->geopoints!='')
  179.                                 {
  180.                                 update_post_meta($post_id, 'fave_property_map', '1');
  181.                                 update_post_meta($post_id, 'fave_property_map_address',''.$prop->sub_community.', '.$prop->community.','.$prop->city.'');
  182.                                 $geocodeFromLatLong = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?api=AIzaSyBjJwEX78xOROeRLbwpF24fqWQtUM8M3mY&latlng='.trim($locexp[1]).','.trim($locexp[0]).'&sensor=false');
  183.                                         $output = json_decode($geocodeFromLatLong);
  184.                                         $status = $output->status;
  185.                                         if($status=="OK")
  186.                                         {
  187.                                         $locountry = $output->results[1]->address_components[4]->short_name;
  188.                                         if($locountry == 'AE')
  189.                                         {
  190.                                             $locountry = 'UAE';
  191.                                         }
  192.                                         update_post_meta($post_id, 'fave_property_country', $locountry);
  193.                                         }
  194.  
  195.            
  196.                                 }
  197.                                
  198.                                 global $wpdb;
  199.                                 $pstable = $wpdb->prefix.'posts';
  200.                                 $agentresult = $wpdb->get_results( "SELECT * FROM $pstable WHERE post_title like '".$prop->agent->name."'", OBJECT );
  201.                                
  202.                                 if(empty($agentresult))
  203.                                 {
  204.  
  205.                                         $ag_post_id = wp_insert_post(array (
  206.                                             'post_type' => 'houzez_agent',
  207.                                             'post_title' => $prop->agent->name,
  208.                                             'post_status' => 'publish',
  209.                                             'comment_status' => 'closed',   // if you prefer
  210.                                             'ping_status' => 'closed',      // if you prefer
  211.                                         ));                    
  212.                                         if ($ag_post_id) {
  213.                                             update_post_meta($ag_post_id, 'fave_agent_email', ''.$prop->agent->email.'');
  214.                                             update_post_meta($ag_post_id, 'fave_agent_office_num', ''.$prop->agent->phone.''); 
  215.                                             update_post_meta($ag_post_id, 'fave_agent_office_num', ''.$prop->agent->phone.'');
  216.                                             update_post_meta($post_id, 'fave_agents', ''.$ag_post_id.'');  
  217.                                             update_post_meta($post_id, 'fave_agent_display_option', 'agent_info'); 
  218.                                                          }
  219.                                 }
  220.                                 else
  221.                                 {
  222.                                     update_post_meta($agentresult[0]->ID, 'fave_agent_email', ''.$prop->agent->email.'');
  223.                                     update_post_meta($agentresult[0]->ID, 'fave_agent_office_num', ''.$prop->agent->phone.'');
  224.                                     update_post_meta($post_id, 'fave_agents', ''.$agentresult[0]->ID.'');  
  225.                                     update_post_meta($post_id, 'fave_agent_display_option', 'agent_info');                             
  226.                                 }
  227.  
  228.  
  229.                                
  230.                                 $rentcond = ''.$prop->rental_period.'';
  231.                                 if($rentcond == 'Y')
  232.                                 {
  233.                                     $rtid = 33;
  234.                                 }
  235.                                 else
  236.                                 {
  237.                                     $rtid = 34;                            
  238.                                 }
  239.                                
  240.                                 wp_set_post_terms( $post_id, $rtid, 'property_status');
  241.                                
  242.                                 $proptype = ''.$prop->property_type.'';
  243.                                 if($proptype != '')
  244.                                 {
  245.                                 $proptype = strtolower($proptype);
  246.                                 if($proptype == 'ap')
  247.                                 {
  248.                                     $proptype = 'apartment';
  249.                                 }
  250.                                  $setermid = get_term_by( 'slug', $proptype, 'property_type');
  251.                                  wp_set_post_terms( $post_id, $setermid->term_id, 'property_type');
  252.                                 }
  253.                                
  254.                                 /*******************images******************************/
  255.                                  //$featrdpath = 'http://shivayeweb.com/wp-content/uploads/2018/05/zpo97vYOzupYJ2JM_.jpg';
  256.                                  $featrdpath = $photoarr[0];
  257.                                  Generate_Featured_Image($featrdpath, $post_id);
  258.                                
  259.                                 foreach($photoarr as $gall)
  260.                                 {
  261.                                     Generate_Img_propgllery($gall, $post_id);
  262.                                 }
  263.                                 /*******************images******************************/                          
  264.                             }
  265.                     echo '<script>window.location.href="?page=imp-prop-byxml&loop='.$nextloop.'"</script>';
  266.                  exit;
  267.                }
  268.                $tcount++;
  269.             }
  270.         }
  271. }
  272. if(isset($_POST['action']) && $_POST['action']=='xmlimpo')
  273.     {
  274.         $url = 'https://mmzr.mycrm.com/feed/privatesite/71a35b4940147de1dda45876020b103b';
  275.         $geturl = file_get_contents($url);
  276.         $xmlcont = simplexml_load_string($geturl, 'SimpleXMLElement', LIBXML_NOCDATA);
  277.         $tcount = 1;
  278.         foreach($xmlcont->property as $prop)
  279.         {
  280.            if($tcount > 1)
  281.            {
  282.                     echo '<script>window.location.href="?page=imp-prop-byxml&loop=1"</script>';
  283.                 exit;  
  284.            }
  285.  
  286.                             $p_title = $prop->title_en;
  287.                             $p_desc = $prop->description_en;               
  288.                             global $wpdb;
  289.                             $newpstable = $wpdb->prefix.'posts';
  290.                             $newpropresult = $wpdb->get_results( "SELECT ID FROM $newpstable WHERE post_title = '".$p_title."' and post_type like 'property' and post_status like 'publish' ", OBJECT );
  291.                             if(empty($newpropresult))
  292.                             {
  293.                                 $post_id = wp_insert_post(array (
  294.                                     'post_type' => 'property',
  295.                                     'post_title' => $p_title,
  296.                                     'post_content' => $p_desc,
  297.                                     'post_status' => 'publish',
  298.                                     'comment_status' => 'closed',   // if you prefer
  299.                                     'ping_status' => 'closed',      // if you prefer
  300.                                 ));
  301.                      
  302.                             $photoarr = (array) $prop->photo->url;
  303.                             unset($photoarr['@attributes']);
  304.                             }
  305.  
  306.                                
  307.                         if ($post_id) {
  308.  
  309.                                 $parent_term = term_exists($prop->city, 'property_city' );
  310.                                 if(!empty($parent_term))
  311.                                 {
  312.                                 $set_city_termid = $parent_term['term_id'];
  313.                                 }
  314.                                 else
  315.                                 {
  316.                                 $city_termid = wp_insert_term(
  317.                                 $prop->city, // the term
  318.                                 'property_city', // the taxonomy
  319.                                  array(
  320.                                   'description'=> ''
  321.                                   )
  322.                                  );
  323.                                  $set_city_termid = $city_termid->term_id;
  324.                                  }
  325.                                 $posted = wp_set_post_terms( $post_id, $set_city_termid, 'property_city',true );
  326.                            
  327.                                
  328.                                
  329.                             update_post_meta($post_id, 'fave_featured', '1');
  330.                             update_post_meta($post_id, 'fave_property_id', ''.$prop->reference_number.'');
  331.                             update_post_meta($post_id, 'fave_property_price', ''.$prop->price.'');
  332.                             update_post_meta($post_id, 'fave_property_address', ''.$prop->sub_community.', '.$prop->community .','.$prop->city.'');
  333.                             update_post_meta($post_id, 'fave_property_size', ''.$prop->size.'');
  334.                             update_post_meta($post_id, 'fave_property_size_prefix', 'Sq Ft');
  335.                             update_post_meta($post_id, 'fave_property_bedrooms', ''.$prop->bedroom.'');
  336.                             update_post_meta($post_id, 'fave_property_bathrooms', ''.$prop->bathroom.'');
  337.                             update_post_meta($post_id, 'fave_single_content_area', 'v2');
  338.                             update_post_meta($post_id, 'fave_single_top_area', 'v2');
  339.                             if($prop->amenities != '')
  340.                             {
  341.                                
  342.                                 $amennewarr = array();
  343.                                 $amen = $prop->amenities;
  344.                                 $amenexp = explode(',',$amen);
  345.                                 foreach($amenexp as $amenval)
  346.                                 {
  347.                                     $amenval = strtolower($amenval);
  348.                                     $term = get_term_by('slug', $amenval, 'propamenities');
  349.                                     $amennewarr[] = $term->name;
  350.                                 }
  351.                                 $impamen = implode(',',$amennewarr);
  352.                                 update_post_meta($post_id, 'fave_additional_features_enable', 'enable');
  353.                                 $returnValue = array(array('fave_additional_feature_title' => 'Amenities','fave_additional_feature_value' => ''.$impamen.'' ));
  354.                                
  355.                                  update_post_meta($post_id, 'additional_features',$returnValue);
  356.                             }
  357.                             $loc = $prop->geopoints;
  358.                             $locexp = explode(',',$loc);
  359.                             update_post_meta($post_id, 'fave_property_location', ''.$locexp[1].','.$locexp[0].'');
  360.                             if($prop->geopoints!='')
  361.                             {
  362.                             update_post_meta($post_id, 'fave_property_map', '1');
  363.                             update_post_meta($post_id, 'fave_property_map_address',''.$prop->sub_community.', '.$prop->community .','.$prop->city.'');
  364.                             $geocodeFromLatLong = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?api=AIzaSyBjJwEX78xOROeRLbwpF24fqWQtUM8M3mY&latlng='.trim($locexp[1]).','.trim($locexp[0]).'&sensor=false');
  365.                                     $output = json_decode($geocodeFromLatLong);
  366.                                     $status = $output->status;
  367.                                     if($status=="OK")
  368.                                     {
  369.                                     $locountry = $output->results[1]->address_components[4]->short_name;
  370.                                     if($locountry == 'AE')
  371.                                     {
  372.                                         $locountry = 'UAE';
  373.                                     }
  374.                                     update_post_meta($post_id, 'fave_property_country', $locountry);
  375.                                     }
  376.  
  377.        
  378.                             }
  379.                            
  380.                             global $wpdb;
  381.                             $pstable = $wpdb->prefix.'posts';
  382.                             $agentresult = $wpdb->get_results( "SELECT * FROM $pstable WHERE post_title like '".$prop->agent->name."'", OBJECT );
  383.                            
  384.                             if(empty($agentresult))
  385.                             {
  386.  
  387.                                     $ag_post_id = wp_insert_post(array (
  388.                                         'post_type' => 'houzez_agent',
  389.                                         'post_title' => $prop->agent->name,
  390.                                         'post_status' => 'publish',
  391.                                         'comment_status' => 'closed',   // if you prefer
  392.                                         'ping_status' => 'closed',      // if you prefer
  393.                                     ));                    
  394.                                     if ($ag_post_id) {
  395.                                         update_post_meta($ag_post_id, 'fave_agent_email', ''.$prop->agent->email.'');
  396.                                         update_post_meta($ag_post_id, 'fave_agent_office_num', ''.$prop->agent->phone.''); 
  397.                                         update_post_meta($ag_post_id, 'fave_agent_office_num', ''.$prop->agent->phone.'');
  398.                                         update_post_meta($post_id, 'fave_agents', ''.$ag_post_id.'');  
  399.                                         update_post_meta($post_id, 'fave_agent_display_option', 'agent_info'); 
  400.                                                      }
  401.                             }
  402.                             else
  403.                             {
  404.                                 update_post_meta($agentresult[0]->ID, 'fave_agent_email', ''.$prop->agent->email.'');
  405.                                 update_post_meta($agentresult[0]->ID, 'fave_agent_office_num', ''.$prop->agent->phone.'');
  406.                                 update_post_meta($post_id, 'fave_agents', ''.$agentresult[0]->ID.'');  
  407.                                 update_post_meta($post_id, 'fave_agent_display_option', 'agent_info');                             
  408.                             }
  409.  
  410.  
  411.                            
  412.                             $rentcond = ''.$prop->rental_period.'';
  413.                             if($rentcond == 'Y')
  414.                             {
  415.                                 $rtid = 33;
  416.                             }
  417.                             else
  418.                             {
  419.                                 $rtid = 34;                            
  420.                             }
  421.                            
  422.                             wp_set_post_terms( $post_id, $rtid, 'property_status');
  423.                            
  424.                             $proptype = ''.$prop->property_type.'';
  425.                             if($proptype != '')
  426.                             {
  427.                             $proptype = strtolower($proptype);
  428.                             if($proptype == 'ap')
  429.                             {
  430.                                 $proptype = 'apartment';
  431.                             }
  432.                              $setermid = get_term_by( 'slug', $proptype, 'property_type');
  433.                              wp_set_post_terms( $post_id, $setermid->term_id, 'property_type');
  434.                             }
  435.                            
  436.                             /*******************images******************************/
  437.                              //$featrdpath = 'http://shivayeweb.com/wp-content/uploads/2018/05/zpo97vYOzupYJ2JM_.jpg';
  438.                              $featrdpath = $photoarr[0];
  439.                              Generate_Featured_Image($featrdpath, $post_id);
  440.                            
  441.                             foreach($photoarr as $gall)
  442.                             {
  443.                                 Generate_Img_propgllery($gall, $post_id);
  444.                             }
  445.                             /*******************images******************************/                          
  446.                         }
  447.             $tcount++;
  448.         }
  449.    
  450.     echo '<br/>Properties are successfull imported!';
  451.    
  452.     }
  453.     ?>
  454.     </div>
  455.    
  456.    
  457. <?php
  458. }
  459.  
  460.  
  461. function Generate_Featured_Image( $image_url, $post_id  ){
  462.  // Add Featured Image to Post
  463.     $image_url        = $image_url; // Define the image URL here
  464.     $image_name       = basename($image_url);
  465.     $upload_dir       = wp_upload_dir(); // Set upload folder
  466.     $image_data       = file_get_contents($image_url); // Get image data
  467.     $unique_file_name = wp_unique_filename( $upload_dir['path'], $image_name ); // Generate unique name
  468.     $filename         = basename( $unique_file_name ); // Create image file name
  469.  
  470.     // Check folder permission and define file location
  471.     if( wp_mkdir_p( $upload_dir['path'] ) ) {
  472.         $file = $upload_dir['path'] . '/' . $filename;
  473.     } else {
  474.         $file = $upload_dir['basedir'] . '/' . $filename;
  475.     }
  476.  
  477.     // Create the image  file on the server
  478.     file_put_contents( $file, $image_data );
  479.  
  480.     // Check image file type
  481.     $wp_filetype = wp_check_filetype( $filename, null );
  482.  
  483.     // Set attachment data
  484.     $attachment = array(
  485.         'post_mime_type' => $wp_filetype['type'],
  486.         'post_title'     => sanitize_file_name( $filename ),
  487.         'post_content'   => '',
  488.         'post_status'    => 'inherit'
  489.     );
  490.    
  491.  
  492.     // Create the attachment
  493.     $attach_id = wp_insert_attachment( $attachment, $file, $post_id );
  494.  
  495.  
  496.     // Include image.php
  497.     require_once(ABSPATH . 'wp-admin/includes/image.php');
  498.  
  499.     // Define attachment metadata
  500.     $attach_data = wp_generate_attachment_metadata( $attach_id, $file );
  501.  
  502.     // Assign metadata to attachment
  503.     wp_update_attachment_metadata( $attach_id, $attach_data );
  504.  
  505.     // And finally assign featured image to post
  506.     set_post_thumbnail( $post_id, $attach_id );
  507.  
  508. }
  509.  
  510. function Generate_Img_propgllery( $image_url, $post_id  ){
  511.  // Add Featured Image to Post
  512.     $image_url        = $image_url; // Define the image URL here
  513.     $image_name       = basename($image_url);
  514.     $upload_dir       = wp_upload_dir(); // Set upload folder
  515.     $image_data       = file_get_contents($image_url); // Get image data
  516.     $unique_file_name = wp_unique_filename( $upload_dir['path'], $image_name ); // Generate unique name
  517.     $filename         = basename( $unique_file_name ); // Create image file name
  518.  
  519.     // Check folder permission and define file location
  520.     if( wp_mkdir_p( $upload_dir['path'] ) ) {
  521.         $file = $upload_dir['path'] . '/' . $filename;
  522.     } else {
  523.         $file = $upload_dir['basedir'] . '/' . $filename;
  524.     }
  525.  
  526.     // Create the image  file on the server
  527.     file_put_contents( $file, $image_data );
  528.  
  529.     // Check image file type
  530.     $wp_filetype = wp_check_filetype( $filename, null );
  531.  
  532.     // Set attachment data
  533.     $attachment = array(
  534.         'post_mime_type' => $wp_filetype['type'],
  535.         'post_title'     => sanitize_file_name( $filename ),
  536.         'post_content'   => '',
  537.         'post_status'    => 'inherit'
  538.     );
  539.  
  540.     // Create the attachment
  541.     $attach_id = wp_insert_attachment( $attachment, $file, $post_id );
  542.  
  543.     // Include image.php
  544.     require_once(ABSPATH . 'wp-admin/includes/image.php');
  545.  
  546.     // Define attachment metadata
  547.     $attach_data = wp_generate_attachment_metadata( $attach_id, $file );
  548.  
  549.     // Assign metadata to attachment
  550.     wp_update_attachment_metadata( $attach_id, $attach_data );
  551.    
  552.  
  553.     // And finally assign featured image to post
  554.     add_post_meta($post_id, 'fave_property_images', $attach_id);
  555.  
  556. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement