答案 0 :(得分:2)
您应该对插件使用激活挂钩,以便在激活插件时执行任何操作。
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 );
}
这应该有效。
答案 1 :(得分:0)
我解决了。 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')
要在激活我的插件时添加帖子:
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' );