The question:
When a new blog is created in a WP Multisite instance I want to be able to set the default theme and configuration options
-
create 2 menus (main and secondary) and associate them with the 2 slots provided by the theme
-
set various theme options as defined on the theme option page
What’s the best way to go about achieving this?
-
which hook should i use – I’m going to use this: (‘WP_DEFAULT_THEME’, ‘theme-folder-name’ in wp-config.php to set the default theme – unless this prevents a needed hook from firing.
-
easiest way to programatically create menus and associate them with existing theme menu ‘slots’
The Solutions:
Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.
Method 1
The best hook I can find is wpmu_new_blog
(line 1086, wp-includes/ms-functions.php
, wpmu_create_blog()
) – it passes 6 arguments like so;
do_action( 'wpmu_new_blog', $blog_id, $user_id, $domain, $path, $site_id, $meta );
$meta
is an array of initial site options, not to be confused with the options generated by populate_options()
.
Programatically creating nav menus may prove to be a little tricky, as there is no ‘API’ as such – they’re a mix of posts, post meta and nav_menu
terms, coupled with a few available functions such as is_nav_menu_item()
and wp_setup_nav_menu_item()
.
My best advice would be to check out wp-admin/nav-menus.php
, as this is where all the core code lies for creating menus.
Using WP_DEFAULT_THEME
should be fine, and is probably the best approach too.
Method 2
This is a little late, but I had to figure out the other part of this question on my own and thought I would share.
To create a default menu and place it into a theme location, you will need a pre-existing location in the theme, and you will need to ensure any pages you link in your menu are also created already.
In your theme’s function.php, register any menu locations. I registered two:
function my_register_navs() {
register_nav_menus(
array( 'header-menu' => __( 'Header Menu' )
, 'footer-menu' => __( 'Footer Menu' ) )
);
}
add_action( 'init', 'my_register_navs' );
Next, you will need to create your menus on site creation. I didn’t use a hook for this, but instead called wpmu_create_blog
manually, but you could hook into wpmu_new_blog
if you preferred.
// Create the menus
$hdr_menu = array(
'menu-name' => 'Header Menu'
, 'description' => 'The primary navigation menu for this website'
);
$header_menu = wp_update_nav_menu_object( 0, $hdr_menu );
$ftr_menu = array(
'menu-name' => 'Footer Menu'
, 'description' => 'The menu that appears at the bottom of most pages in this website'
);
$footer_menu = wp_update_nav_menu_object( 0, $ftr_menu );
// Set the menus to appear in the proper theme locations
$locations = get_theme_mod('nav_menu_locations');
$locations['header-menu'] = $header_menu;
$locations['footer-menu'] = $footer_menu;
set_theme_mod('nav_menu_locations', $locations);
Lastly, you’ll need to add items to the menus. Repeat this code for each item in each menu.
// Build menu item
$menu_item = array(
'menu-item-object-id' => $page_id
, 'menu-item-parent-id' => 0
, 'menu-item-position' => $menu_order
, 'menu-item-object' => 'page'
, 'menu-item-type' => 'post_type'
, 'menu-item-status' => 'publish'
, 'menu-item-title' => $label
);
// Add to nav menu
wp_update_nav_menu_item( $header_menu, 0, $menu_item );
Hope this helps! Here are some external references for you:
- get_theme_mod
- wp_update_nav_menu_item This blog has a good example of creating menu items
Method 3
function mysite_wpmu_new_blog($blog_id)
{
switch_to_blog($blog_id);
switch_theme('my-theme', 'my-theme');
restore_current_blog();
}
add_action('wpmu_new_blog', 'mysite_wpmu_new_blog');
This works for me!
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0