[wp-hackers] Auto creating custom menu
Tom Barrett
tcbarrett at gmail.com
Wed Nov 16 12:14:01 UTC 2011
I am building a WordPress 'deployment system'. As part of this, I want: 1)
install and activate a theme 2) create some pages 3) Add a custom menu with
those pages. The first two steps are fine, and I can create the custom
menu. But I cannot get the pages into the menu.
How the automated part works, is that it creates wp-config.php file, and a
PHP script in wp-admin that installs WordPress (calling wp_install()
e.t.c). It then calls an action hook that is added in the theme's files.
Something a bit like this:
<?php
// Script in wp-admin
require_once( '../wp-load.php' );
require_once( './includes/upgrade.php' );
wp_install( ..... );
do_action( 'setup_wordpress' );
?>
<?php
// Theme functions.php file
add_action( 'setup_wordpress', 'do_setup_wordpress' );
function do_setup_wordpress(){
$mypages = make_three_pages_and_return_their_post_id(); // returns
array( [0]=>3, [1]=>4, [2]=>5 )
$mymenuname = 'My Menu';
wp_create_nav_menu( $mymenuname );
$menu = get_term_by( 'name', $mymenuname, 'nav_menu' );
$menu_id = $menu->term_id;
foreach( $mypages as $page_id ){
$args = array(
'menu-item-object-id' => $page_id,
'menu-item-object' => 'page',
'menu-item-type' => 'post_type',
'menu-item-status' => 'publish',
);
$menu_item_id = wp_update_nav_menu_item( $menu_id, 0, $args );
}
}
?>
It does everything except put the pages in the menu. The code works if I
trigger it through browsing the site. EG: add_action( 'admin_init',
'do_setup_wordpress' ); (This is how I developed it initially).
The $menu_item_id variable gets a value (usually 6,7,8).
Any ideas what I'm missing or not doing? Am I just being crazy?
--
http://www.tcbarrett.com | http://gplus.to/tcbarrett |
http://twitter.com/tcbarrett
More information about the wp-hackers
mailing list