WordPress.org

Ready to get started?Download WordPress

Forums

Auto create blogposts using filename or directory-structure (3 posts)

  1. Joel de Bruijn
    Member
    Posted 4 years ago #

    I use WordPress a lot for it's CMS functionality. In these setups often a file itself is the center of attention, not the text in a blogpost. A file could be a photo, video, gps-track or other arbitrary file.

    I'm looking for a plugin which automagically creates post from the filename or directory-structure. E.g.:

    - When a WordPress setup is for photo's the plugin scans the upload directory for *.jpg's. When it find a directory like 2009/12/01 with a photo named "Weekend Paris.jpg" it creates a post with date 01-12-2009 and title "Weekend Paris". With the image attached.

    - Or the plugin scans the filenames, like "20091201 Weekend Paris.jpg" and creates the same post as above. Using a filename convention to populate the necessary fields in the WP tables.

    This plugin could rescan the upload directory by cronjobs. Another idea for photos:

    Take the directory structure for 1 blogpost, and attach al the files within a directory to one blogpost. This would turn WordPress into an automatic photoalbum gallery.

    With my gpx files, using the google-map plugin, it would turn WordPress into an automatic tracks sharing site.

    I scanned a lot of plugins to look for this functionality, but never found it. Any ideas?

  2. alchymyth
    The Sweeper & Moderator
    Posted 4 years ago #

    no plugin - but i am playing with the same idea - this is how far i got (for images only):

    function GetImagesFromFolderAndSaveAsNewPosts($folder) {
         // for instance: 'gallery';  - the images are in: http://mywebsite.com/wordpress/gallery
    
    	 $base = get_bloginfo('url');
    	 $path = '/'.$folder;
    	 $track = '.'.$path; 
    
    	 echo $base.$path.'<br />';
    //thanks to: http://www.xfunda.com/index.php?option=com_content&view=article&id=68:directory-listings-list-files-and-subfolder-using-php&catid=41:php&Itemid=44
    	 $filelist = array();
         $TrackDir=opendir($track);  
    
    	 while ($file = readdir($TrackDir)) {
          if ($file == "." || $file == "..") { }
         	else {
    			$ext = explode('.',$file); $extension=$ext[1]; //check for image files
    			if( $extension=='jpg' || $extension=='gif' || $extension=='png') {
         		$filelist[] = $file;	}
    			echo $file.'<br />';
    			 }
         }
         closedir($TrackDir);  
    
    foreach ($filelist as $link) : 
    
    //do something with the files;
    //i.e. make post names readable - no img extension
    
    $my_post_name = explode('.',$link); $my_post_name = $my_post_name[0]; //remove file extension
    $my_post_name = str_replace('_',' ',$my_post_name); //replace underlines with space
    $my_post_name = str_replace('-',' ',$my_post_name); //replace hyphen with space
    
    // check if title already exists in wpdb
    global $wpdb;
    if($wpdb->get_row("SELECT post_title FROM wp_posts WHERE post_title = '" . $my_post_name . "'", 'ARRAY_A')) {
    echo $my_post_name.' | already exists<br />';}
    else
    {
    echo 'generating new post: '. $my_post_name.' ;<br />';
    // Create post object - in this case just containing one image
      $my_post = array();
      $my_post['post_title'] = $my_post_name;
      $my_post['post_content'] = '<img src="'.get_bloginfo('url').'/gallery/'.$link.'"/>';
      $my_post['post_status'] = 'draft'; // or 'publish'
      $my_post['post_author'] = 1;
      $my_post['post_category'] = array(228);
    
    // Insert the post into the database
      wp_insert_post( $my_post );
    }
    endforeach;
    }
    ?>
    
    <?php GetImagesFromFolderAndSaveAsNewPosts('gallery'); ?>
  3. Joel de Bruijn
    Member
    Posted 4 years ago #

    @alchymyth Thanks for your reaction!

    I have little coding experience, but it's readable to me. Do I understand correct:
    - that I have to tell this function the foldername, so it can construct a directory path to scan?
    - it strips the extension from every filename?
    - filling the field 'post_content' takes care of the embedding of the image?

    To make this more generic:
    - Is it possible to have the 'foldername' be taken from the settings of a plugin?
    - Is it possible to have different embedding loops for different extensions? So the field 'post_content' could be filled different for different filetypes? For some filetypes a thumbnail with a link to the file would be enough.

    Anyway, I realize I have more questions than answers ;)

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags