How to set a Static page (example Sample Page with every WordPress install) as default fron page for every newly created site on multisite network. I mean when a new user creates a site on my multisite network a Page with custom name sets as front page automatically. Please tell me how to accomplish that.
4 Answers
Try the following code. Based on these two Q&A's (not tested):
add_action( 'wpmu_new_blog', 'process_extra_field_on_blog_signup', 10, 6 );
function process_extra_field_on_blog_signup( $blog_id, $user_id, $domain, $path, $site_id, $meta ) {
switch_to_blog($blog_id);
$homepage = get_page_by_title( 'Sample Page' );
if ( $homepage )
{
update_blog_option( $blog_id, 'page_on_front', $homepage->ID );
update_blog_option( $blog_id, 'show_on_front', 'page' );
}
restore_current_blog();
}
Use it as a Must Use plugin.
Be carefull, wpmu_new_blog is deprecated. Use wp_insert_site instead. To give a proper overview, the question can now be answered as follow:
function process_extra_field_on_blog_signup( $new_site ) {
$blog_id = $new_site->blog_id;
switch_to_blog($blog_id);
$homepage = get_page_by_title( 'Sample Page' );
if ( $homepage )
{
update_blog_option( $blog_id, 'page_on_front', $homepage->ID );
update_blog_option( $blog_id, 'show_on_front', 'page' );
}
restore_current_blog();
}
add_action( 'wp_insert_site', 'process_extra_field_on_blog_signup', 10, 6 );
Wordpress provides a function update_option to set static page programmatically by default when a new blog is created in network multisite.
function my_new_blog_settings($blog_id, $user_id) {
// set static front page in general settings
update_option( 'show_on_front', 'page', true);
// set the static page based on page ID
update_option( 'page_on_front', $page_id, true);
}
Above given code can be invoked using action 'wpmu_new_blog'
add_action('wpmu_new_blog', 'my_new_blog_settings', 10, 2);
1) To set Your first page you can add new page as home.php
in your directory. Wordpress by default take home.php
as its first page if it is declared.
2) Go to settings->Reading->Front page display. You can set here your front page which will display when your site open.