我想动态创建一个新的php页面。但是,我不想通过访问wordpress仪表板来创建页面。
我的目标是使用正确的模板名称和URL注册新创建的页面。
例如:
1.playground.php创建了一个名为Playground_x.php的新文件
2.playground_x.php具有模板名称(Playground X)和网址(/ playground_x)
3。最终在不访问wordpress仪表板的情况下注册了park_x.php页面
这是我到目前为止的内容。请告知如何执行步骤3。
playground.php
<?php
// Template Name: Playground
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$filename = 'playground_x.php';
$templatename = 'Playground X';
$path = './wp-content/themes/production_theme/';
$content = <<<EOT
<?php
//Template Name: {$templatename}
?>
EOT;
$success = file_put_contents($path .$filename, $content);
if($success):
echo "success";
else:
echo "fail";
endif;
?>
我能够如下解决这个问题。
解决方案
if($success):
$my_post = array(
'post_content' => $rawname,
'post_title' => $cleanname,
'post_name' => $cleanname,
'post_type' => 'page', // must be 'page' to accept the 'page_template' below
'page_template' => $filename,
'post_status' => "publish"
);
$ID = wp_insert_post( $my_post );
$permalink = get_permalink($ID);
if($ID && $permalink):
echo "<br />ID for new page is $ID, Permalink for new page is $permalink";
endif;
else:
echo "fail";
endif;
参考