wp_insert_postを使えばページ追加できます。
http://wpdocs.osdn.jp/%E9%96%A2%E6%95%B0%E3%83%AA%E3%83%95%E3%82%A1%E3%83%AC%E3%83%B3%E3%82%B9/wp_insert_post
一回だけ投稿させようとすると、
「すでにhogeというページがあるかチェックする => なければ投稿処理する」
といったチェックが必要ですね。
固定ページ数が多ければ、自動化する価値はあるかもしれません。
MIZUNOさん
返信ありがとうございます。
実際の固定ページとして登録してしまう、という方法ですね。
やはりその方法しかないですかね〜。
できればDB側はスッキリさせたい気も…してたりします…!
feed の出力は、実際の固定ページが無くても表示されているので、
feed 周りを参考にすれば、rewrite で作れるかもしれません。
この場合は、hogetanさんの懸念されている、スラッグ重複が怖いですが。
昔テーマのディレクトリのHTMLをチェックして、
- ファイル名と同名の固定ページが無ければ登録
- 存在し、タイムスタンプが新しければ更新
みたいなのはやってみたことはあります。サイト内検索などもあったので。
Class PageContents {
private $dir;
private $separator = "_";
private $content_dir = "page-contents";
private $ext = "html";
public function __construct() {
$this->dir = get_stylesheet_directory()."/".$this->content_dir;
}
private function getContentFiles() {
return array_map(function($fileName){
return $fileName;
}, glob($this->dir.'/{*.'.$this->ext.'}',GLOB_BRACE));
}
public function sync() {
foreach ($this->getContentFiles() as $key => $fileName) {
$pageName = $this->getPageNameFromFile($fileName);
$post = get_page_by_path($pageName);
if($post) {
if( (strtotime($post->post_modified_gmt) < intval(filemtime($fileName))) ) {
$newpost = $this->getPostFromFile($fileName);
if($newpost->post_title) {
$post->post_title = $newpost->post_title;
}
$post->post_content = $newpost->post_content;
wp_update_post($post);
}
}
else {
$newpost = $this->getPostFromFile($fileName);
$postData = [
'post_status' => 'publish',
'post_type' => 'page',
'post_parent' => $newpost->post_parent,
'post_title' => $newpost->post_title,
'post_content' => $newpost->post_content,
'post_name' => $newpost->post_name
];
wp_insert_post($postData);
}
}
}
private function getPostFromFile($fileName) {
$content = file_get_contents($fileName);
$post = new \stdClass();
$post->post_title = "";
$post->post_parent = 0;
if( preg_match( '|Content Name:(.*)$|mi', $content, $header ) ){
$post->post_title = $header[1];
}
$pageSlug = $this->getPageNameFromFile($fileName);
$slugs = explode("/", $pageSlug);
$postName = array_pop($slugs);
$post->post_name = $postName;
if(count($slugs) > 0) {
$page = get_page_by_path(implode("/", $slugs));
if($page) {
$post->post_parent = $page->ID;
}
}
// remove Comment.
$post->post_content = preg_replace('/\<!--(.|\n)*?--\>(\s)*?/i', "", $content);
return $post;
}
private function getPageNameFromFile($fileName) {
$pageName = str_replace(['.'.$this->ext, $this->dir."/"], "", $fileName);
$pageName = str_replace([$this->separator], "/", $pageName);
return $pageName;
}
}
あとは、宮内さんのブログの以下の記事が参考になるかもです。
* WordPressプラグインで好き勝手な内容を好き勝手なURLで表示する。
* WordPressに独自のURLを追加する。2014年版
MIZUNO さん
feed の出力は、実際の固定ページが無くても表示されているので、
feed 周りを参考にすれば、rewrite で作れるかもしれません。
なるほど〜、feedの出力周りでそういうことやってたのですね。
情報ありがとうございます〜。ちょっと見てみたいと思います!
Toro_Unit さん
お返事おそくなってすみません (>_<
コードありがとうございます。これは特定のディレクトリにあるhtmlをページとして登録、ですね。参考になります!
また宮内さんの記事面白そうですね。この記事周辺やお二方お情報を組み合わせると表題の件をうまくできそうな気がしますね!
ありがとうございます〜。
Toro_Unitさん MIZUNOさん
その後おかげさまでプラグインとして作成することができました。
認識してすぐに固定ページとして登録してしまうと後に響きそうなので、rewriteを利用してendpointを設定するようにしました。
また今後のアップデートで固定ページとして登録する事も考えています。
https://github.com/yousan/add-page-from-template
教えて頂いてありがとうございました。