Note that there are some explanatory texts on larger screens.

plurals
  1. POFor loop over named folder to read each .txt filename and contents
    text
    copied!<p>The script below takes a named file that resides in the "myplugin" folder (the folder that the script itself resides in) and runs file_get_contents() on it to load the contents into memory, then does some preprocessing on the contents before finally inserting it as a post into the WordPress database via the wp_insert_post method.</p> <pre><code>$my_post3 = array(); $my_post3['post_title'] = 'Privacy Policy'; if(file_exists(ABSPATH.'/wp-content/plugins/myplugin/pages/privacy_policy.txt')) { $my_privacy_policy = file_get_contents(ABSPATH.'/wp-content/plugins/myplugin/pages/privacy_policy.txt'); } else { $my_privacy_policy = ""; } $my_post3['post_content'] = addslashes($my_post3_replace); $my_post3['post_type'] = 'page'; $my_post3['post_status'] = 'publish'; wp_insert_post($my_post3); </code></pre> <p>This method works pretty good. However, this method forces me to write a different routine for every file I want to use as the basis of a new page.</p> <p>What I would like to do instead, is create a folder called "pages" and place my .txt files in that, then run a for loop on the contents of the folder, creating a new page for each file in the folder. I'd like to use the file name (minus the .txt extension) as the name of the page.</p> <p>For example, the pages folder may have these files:</p> <p>About Us.txt Contact Us.txt</p> <p>And the routine would result in the creation of two new pages in WordPress site, one called "About Us" containing the content found in that file. The other page would of course be "Contact Us" with the contents of that file.</p> <p>In this way, I can just drop an unlimited number of named and prepopulated .txt files into that folder and when I activate my plugin, it creates those pages.</p> <p>I just need some help with the for loop and how to reference the folder and files.</p> <p>I will also have a folder called "posts", which will do the same for posts that this routine does for pages.</p> <p>Thanks in advance for your help and suggestions.</p> <p>Update based on @clientbucket answer:</p> <pre><code>DEFINE ('PAGES', './pages/'); $directory_pages = new DirectoryIterator(PAGES); foreach ($directory_pages as $files) { if ($files_pages-&gt;isFile()) { $file_name_page = $files_pages-&gt;getFilename(); $my_page_content = file_get_contents(PAGES. $file_name_page); $my_page['post_content'] = addslashes($my_page_content); $my_page['post_title'] = $file_name_page; $my_page['post_type'] = 'page'; $my_page['post_status'] = 'publish'; wp_insert_post($my_page); } } DEFINE ('POSTS', './posts/'); $directory_posts = new DirectoryIterator(POSTS); foreach ($directory_posts as $files_posts) { if ($files_posts-&gt;isFile()) { $file_name_post = $files_posts-&gt;getFilename(); $my_post_content = file_get_contents(POSTS. $file_name_post); $my_post['post_content'] = addslashes($my_post_content); $my_post['post_title'] = $file_name_post; $my_post['post_type'] = 'post'; $my_post['post_status'] = 'publish'; $post_id = wp_insert_post($my_post); stick_post($post_id); } } </code></pre> <p>Fatal error: Uncaught exception 'UnexpectedValueException' with message 'DirectoryIterator::__construct(./pages/) [directoryiterator.--construct]: failed to open dir: No such file or directory' in C:\xampplite\htdocs\mytestsite\wp-content\plugins\myplugindirectory\myplugin.php:339</p> <p>Line 339 is here > $directory_pages = new DirectoryIterator(PAGES); </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload