[英]Wordpress - Add a page upon activation of plugin
I am currently making a Wordpress plugin, but I couldn't find an answer to this. 我目前正在制作Wordpress插件,但是找不到答案。
How do you add a page upon activation of the plugin? 激活插件后如何添加页面?
I have added posts upon activation earlier with the wp_insert_post function, but I can't find a way to insert a page. 我已经在使用wp_insert_post函数激活时添加了帖子,但是我找不到插入页面的方法。
You should use the activation hook for plugins in order to make any actions upon the plugin activation. 您应该对插件使用激活挂钩,以便在激活插件时执行任何操作。
register_activation_hook( __FILE__, 'activation_hook_callback');
function activation_hook_callback()
{
//add the post type and other options in the array for the query
$page = array(
'post_status' => 'publish' ,
'post_title' => 'Page name',
'post_type' => 'page',
);
//add the page and ID will be saved.
$the_page_itself = wp_insert_post( $page );
}
This should work. 这应该工作。
I solved it. 我解决了 Wordpress has several
post-types
: WordPress有几种
post-types
:
Post (Post Type: 'post')
Page (Post Type: 'page')
Attachment (Post Type: 'attachment')
Revision (Post Type: 'revision')
Navigation menu (Post Type: 'nav_menu_item')
To add a post upon activation of my plugin: 要在激活我的插件时添加帖子:
function add_page_upon_activation() {
$arr = array(
'post_title' => 'title',
'post_name' => 'slug',
'post_status' => 'publish',
'post_type' => 'page',
'post_content' => 'yes, a nice page',
);
wp_insert_post($arr);
}
add_action( 'activated_plugin', 'add_page_upon_activation' );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.