Advertisement
daymobrew

Programatically create Event Manager Locations

Feb 9th, 2015
466
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.06 KB | None | 0 0
  1. <?php
  2. define('WP_USE_THEMES', false);
  3.  
  4. /** Loads the WordPress Environment and Template */
  5. require( dirname( __FILE__ ) . '/wp-blog-header.php' );
  6.  
  7. # Create an associative array of locations to be used later.
  8. $fp = fopen('ent_regions.csv', 'r');
  9. $regions = array();
  10. if ($fp) {
  11.     while (($line = fgets($fp, 4096)) !== false) {
  12.         $items = explode('|', $line);
  13.         $regions[$items[0]] = $items[1];
  14.     }
  15.     fclose($fp);
  16. }
  17. else {
  18.     print "ERROR: Could not open regions file.";
  19.     exit;
  20. }
  21.  
  22.  
  23. # CSV file is export of other database.
  24. $fp = fopen('outlets.csv', 'r');
  25. if ($fp) {
  26.     $author_id = 1;
  27.     $location_id = 5;  # INCREMENT before wp_insert_post.
  28.  
  29.     while (($line = fgets($fp, 4096)) !== false) {
  30.         $venue = explode('|', $line);
  31. #var_dump($venue);
  32.        $location_id++;
  33.         $location_name = $venue[1];
  34.         $slug = strtr(strtolower($location_name), ' ', '-');
  35.         $slug = str_replace('-&-', '-and-', $slug);
  36.         $slug = str_replace('---', '-', $slug);
  37.         $slug = str_replace('--', '-', $slug);
  38.        
  39.         $description = sprintf('%s%sWebsite: <a href="%s">%s</a>%sEmail: %s%sPhone: +353 0 %s', $venue[13], "\n\n", $venue[6], $venue[6], "\n", $venue[4], "\n", $venue[5]);
  40.         $excerpt = $venue[14];
  41.         $longitude = $venue[18];
  42.         $latitude = $venue[19];
  43.         $address = $venue[2];
  44.         $town = $regions[$venue[3]];
  45.  
  46. #print "$location_name - $slug - $excerpt\n$longitude - $latitude\n$address - $town\n\n";
  47. $post_id = wp_insert_post(
  48.         array(
  49.             'post_name'     =>  $slug,
  50.             'post_title'        =>  $location_name,
  51.             'post_content'          =>  $description,
  52.             'post_excerpt'          =>  $excerpt,
  53.             'post_author'       =>  $author_id,
  54.             'comment_status'    =>  'closed',
  55.             'ping_status'       =>  'closed',
  56.             'post_status'       =>  'publish',
  57.             'post_type'     =>  'location'
  58.         ));
  59. #echo "Post id: ".$post_id;
  60.  
  61. # Delete post meta to be thorough.
  62. delete_post_meta($post_id, '_location_status');
  63. update_post_meta($post_id, '_location_status', '1');
  64.  
  65. delete_post_meta($post_id, '_location_longitude');
  66. update_post_meta($post_id, '_location_longitude', $longitude);
  67.  
  68. delete_post_meta($post_id, '_location_latitude');
  69. update_post_meta($post_id, '_location_latitude', $latitude);
  70.  
  71. delete_post_meta($post_id, '_location_country');
  72. update_post_meta($post_id, '_location_country', 'IE');
  73.  
  74. delete_post_meta($post_id, '_location_region');
  75. update_post_meta($post_id, '_location_region', '');
  76.  
  77. delete_post_meta($post_id, '_location_id');
  78. update_post_meta($post_id, '_location_id', $location_id);
  79.  
  80. delete_post_meta($post_id, '_location_address');
  81. update_post_meta($post_id, '_location_address', $address);
  82.  
  83. delete_post_meta($post_id, '_location_town');
  84. update_post_meta($post_id, '_location_town', $town);
  85.  
  86. delete_post_meta($post_id, '_location_state');
  87. update_post_meta($post_id, '_location_state', 'Co. Cork');
  88.  
  89. delete_post_meta($post_id, '_location_postcode');
  90. update_post_meta($post_id, '_location_postcode', '');
  91.  
  92.     }
  93.     fclose($fp);
  94. }
  95. else {
  96.     print "ERROR: Could not open outlets file.";
  97.     exit;
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement