Looking for ideas to solve auto-posting to WP site

I'm trying to figure out which way to go with this problem and thought I might get some suggestions from you folks.
This is what I've got to start from:
I've got a text file and an image file and want to auto-post to a WP site
These files are being created from a javascript triggered in Acrobat (converting PDFs into text&image)
These files are named identically (besides the extension) so there is example.txt and example.jpg
These files will always be saved in the same location.

What I've played with so far:
I've tried Postie, and successfully emailed txt & jpg into the WP site. But I would next have to automate the creation of that email, and don't really want to introduce another independent working script into the mix. Too many pieces tends to make for trouble :slight_smile:

So I'm wondering now, and looking for ideas on how to pursue making WordPress do the work - is there a way I could script WordPress to create post, import text file, attach jpg?

I started now looking at this: http://codex.wordpress.org/Function_Reference/wp_insert_post

Anyone else have any suggestions - maybe a smarter place to start?


  • jageo
    • Flash Drive

    Doesn't look like it auto-posts though.
    This is however, something interesting to look at for a different project. thanks :slight_smile:

    This is what I'm imagining: (but am totally open to completely different directions)
    'something' looks for new files in the folder
    when there is a file, read the text into a variable
    use wp_insert_post to create a new post with that text
    use media_sideload_image to attach the jpg
    delete the files to clean up

  • jageo
    • Flash Drive

    I think I'm getting there. I have some partial code that works in creating a post.

    $h = opendir('./text/'); //Open the temporary directory - need to fix this to Dropbox directory
    while (false !== ($entry = readdir($h))) {
        if($entry != '.' && $entry != '..') { //Skips over . and ..
            $filename =  pathinfo($entry, PATHINFO_FILENAME); //Strips whichever file it sees to just filename without extension, so this variable can be used for text file and jpg file
            break; //Exit the loop so no more files are read
    $textfile = "./text/".$filename.".txt";  //sets up to read the text file
    $file = fopen("$textfile", 'r'); //opens the text file with read access
    $Data = fread($file, filesize($textfile)); //puts the text in variable data
    fclose($file);  //closes the text file
    unlink($textfile); //deletes the text file
    echo $Data . "<br/>";  //temporary, echoes the text onto the wordpress page
    <?php // Create post object
    $my_post = array(
      'post_title'    => $filename,
      'post_content'  => $Data,
      'post_status'   => 'publish',
      'post_author'   => 1,
    $post_id = wp_insert_post( $my_post );  // Inserts the post into the database, and grabs the ID

    still need to work on
    a) the path (should be reading from a dropbox folder)
    b) automating this PHP (I have never played with cron yet)
    c) the image (I got it to work half way, but the spaces in the file names are messing me up. I'll play with it a bit more before I share. Until then I left that part off)
    d) error handling

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.