WordPress提供了一个名为wpmu_new_blog
并传递参数$blog_id
(新博客的ID)和$user_id
(用户,刚刚创建了一个新博客)(还有更多)到它。您可以挂接到此操作以创建新页面并编辑选项:
<?php
/**
* Plugin Name: Default Site Structure
* Description: Crates default pages and set them as front/blog index pages.
* Network: true
* Plugin URL: http://wordpress.stackexchange.com/a/219504/31323
* License: MIT
* Version: 1.0.0-alpha
*/
namespace WPSE177819;
add_action( \'wp_loaded\', __NAMESPACE__ . \'\\init\' );
/**
* @wp-hook wp_loaded
*/
function init() {
add_action(
\'wpmu_new_blog\',
function( $blog_id, $user_id ) {
switch_to_blog( $blog_id );
$front_page_id = wp_insert_post( front_page_data( $user_id ) );
$index_page_id = wp_insert_post( index_page_data( $user_id ) );
if ( ! is_wp_error( $front_page_id ) && 0 !== $front_page_id ) {
update_option( \'show_on_front\', \'page\' );
update_option( \'page_on_front\', $front_page_id );
}
if ( ! is_wp_error( $index_page_id ) && 0 !== $index_page_id ) {
update_option( \'page_for_posts\', $index_page_id );
}
update_option( \'date_format\', date_format() );
update_option( \'time_format\', time_format() );
restore_current_blog();
},
10,
2
);
}
/**
* Returns the data of the blog index page
*
* @param int $post_author
*
* @return array
*/
function index_page_data( $post_author ) {
return [
\'post_title\' => \'My blog index\',
\'post_content\' => \'\',
\'post_type\' => \'page\',
\'post_author\' => $post_author,
\'post_status\' => \'publish\'
];
}
/**
* Returns the data of the front page
*
* @param int $post_author
*
* @return array
*/
function front_page_data( $post_author ) {
return [
\'post_title\' => \'Hello World\',
\'post_content\' => \'Welcome to my new site!\',
\'post_type\' => \'page\',
\'post_author\' => $post_author,
\'post_status\' => \'publish\'
];
}
/**
* Returns the custom date format
*
* @return string
*/
function date_format() {
return \'d,m,Y\';
}
/**
* Returns the custom time format
*
* @return string
*/
function time_format() {
return \'H/i/s\';
}
将此插件用作
mu-plugin
这将影响每一个新博客。此示例直接编辑所有选项键。您应该花一些时间来弄清楚WordPress是否提供API函数来设置这些选项。(类似于
wp_set_front_page()
…)