Creating code to auto generate posts from pdfs

Hey all, I have several hundred pdf’s that I need to put up as seperate posts on my clients website, and I was wondering if anyone could point me in the direction of the code I would need (or need to write) in order to do this automatically. Preferably, the code would be able to take a folder or zip file, and generate posts from the pdf’s located in the folder and subfolders. It would create a category hierarchy based on the the names of the folders, and post titles based on the titles of the pdf’s. Each post would contain a link to the pdf and an embedded version of the pdf using the google docs viewer code. An exmple post can be seen here. If it could also auto generate a menu that would be great, but it isn’t as vital. So, any thoughts?

  • Sam
    • Site Builder, Child of Zeus

    and here is some code that my friend helped get me started with:

    <?php

    $directory = '/path/to/files/';
    $filter = '/*.pdf$/i'; // Filename ends in .pdf or .PDF
    processDirectory($directory, $filter);

    function processDirectory($directory, $filter) {
    if (substr($directory,strlen($directory)-1,1) != '/') { // Add trailing /
    $directory .= '/';
    }
    if ($handle = opendir($directory)) {
    /* This is the correct way to loop over the directory. */
    while (false !== ($entry = readdir($handle))) {
    if (is_dir($directory.$entry) {
    // Recurse into nested directory
    processDirectory($directory.$entry, $filter);
    } else if (preg_match($filter, $entry)) {
    echo "Processing file $directory/$entry<br />n";
    // Valid filename: Build post
    // Contents of $post array described in http://codex.wordpress.org/Function_Reference/wp_insert_post
    $post = Array(
    'post_content' => ' <a href="http://somesite.com/$directory/"' . $entry . '">' . $entry . '</a>.<br />'.
    '<iframe src=http://docs.google.com/viewer?embedded=true&url=' .
    urlencode($directory.$entry) . '></iframe>',
    // This won't be right! You'll need to convert $directory and add your website domain
    'post_title' => $entry
    );
    // Post post
    wp_insert_post($post);
    }
    }
    } else {
    echo "Failed to open $directory";
    }
    }

    ?>

  • PC
    • WPMU DEV Initiate

    Hey there Sam,

    Thanks for posting on the forums.

    I did some investigation on this but could not find something which can automate this but I reckon there should be a work around.

    A good place to start is : http://www.wpbeginner.com/plugins/how-to-embed-pdf-spreadsheet-and-others-in-wordpress-blog-posts/

    But that doesn’t automate post creation out of the box.

    Let me call my colleague @patrick Cohen for his views and see if he has got any quick tips on how this can be achieved.

    He should be here soon.

    Cheers, PC

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.