The script below displays the named file, which is located in the "myplugin" folder (the folder where the script itself is located), and runs the file_get_contents () file to load the contents into memory, and then does some preprocessing on the contents before finally paste it as an entry into the WordPress database using the wp_insert_post method.
$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);
This method works very well. However, this method forces me to write a different procedure for each file that I want to use as the basis for a new page.
"" .txt, for , , ( .txt) .
, :
.txt
.txt
WordPress, " ", , . , , " " .
, .txt , , .
for .
"posts", , .
.
@clientbucket:
DEFINE ('PAGES', './pages/');
$directory_pages = new DirectoryIterator(PAGES);
foreach ($directory_pages as $files) {
if ($files_pages->isFile()) {
$file_name_page = $files_pages->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->isFile()) {
$file_name_post = $files_posts->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);
}
}
: "UnexpectedValueException" "DirectoryIterator:: __ construct (./pages/) [directoryiterator.-- construct]: : ' C:\xampplite\htdocs\mytestsite\-\Plugins\myplugindirectory\myplugin.php: 339
339 > $directory_pages = new DirectoryIterator ();