admin管理员组文章数量:1327750
I’d like to have a page added by default when a new site is created with WP Multisite.
So I have my function that creates two pages:
function my_default_pages() {
$default_pages = array('Impress', 'Contact');
$existing_pages = get_pages();
foreach($existing_pages as $page) {
$temp[] = $page->post_title;
}
$pages_to_create = array_diff($default_pages,$temp);
foreach($pages_to_create as $new_page_title) {
// Create post object
$my_post = array();
$my_post['post_title'] = $new_page_title;
$my_post['post_content'] = 'This is my '.$new_page_title.' page.';
$my_post['post_status'] = 'publish';
$my_post['post_type'] = 'page';
// Insert the post into the database
$result = wp_insert_post( $my_post );
}
}
And I found out that I have to hook into the wpmu_new_blog
[1] action that fires when a new site is created.
add_action('wpmu_new_blog', 'my_default_pages');
But I don’t know how make both working together …
I’d like to have a page added by default when a new site is created with WP Multisite.
So I have my function that creates two pages:
function my_default_pages() {
$default_pages = array('Impress', 'Contact');
$existing_pages = get_pages();
foreach($existing_pages as $page) {
$temp[] = $page->post_title;
}
$pages_to_create = array_diff($default_pages,$temp);
foreach($pages_to_create as $new_page_title) {
// Create post object
$my_post = array();
$my_post['post_title'] = $new_page_title;
$my_post['post_content'] = 'This is my '.$new_page_title.' page.';
$my_post['post_status'] = 'publish';
$my_post['post_type'] = 'page';
// Insert the post into the database
$result = wp_insert_post( $my_post );
}
}
And I found out that I have to hook into the wpmu_new_blog
[1] action that fires when a new site is created.
add_action('wpmu_new_blog', 'my_default_pages');
But I don’t know how make both working together …
Share Improve this question edited Nov 7, 2012 at 19:15 user1706680 asked Nov 7, 2012 at 17:57 user1706680user1706680 6943 gold badges13 silver badges29 bronze badges 3 |1 Answer
Reset to default 11The hook is not the problem - your code runs in the context of the current site, not the one just created! The following code isn't tested, but it should at least highlight the problem:
function wpse_71863_default_pages( $new_site ) {
$default_pages = array(
'Impress',
'Contact',
);
switch_to_blog( $new_site->id );
if ( $current_pages = get_pages() ) {
$default_pages = array_diff( $default_pages, wp_list_pluck( $current_pages, 'post_title' ) );
}
foreach ( $default_pages as $page_title ) {
$data = array(
'post_title' => $page_title,
'post_content' => "This is my $page_title page.",
'post_status' => 'publish',
'post_type' => 'page',
);
wp_insert_post( add_magic_quotes( $data ) );
}
restore_current_blog();
}
add_action( 'wp_insert_site', 'wpse_71863_default_pages' );
本文标签: WP Multisite Adding pages on blog creation by default
版权声明:本文标题:WP Multisite: Adding pages on blog creation by default 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742226574a2436428.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
add_action
? – TheDeadMedic Commented Nov 7, 2012 at 18:54add_action
right after thefunction
in myfunctions.php
. But it doesn’t work. – user1706680 Commented Nov 7, 2012 at 18:59