[wp-hackers] Auto creating custom menu
Tom Barrett
tcbarrett at gmail.com
Thu Nov 17 14:33:16 UTC 2011
Thanks Doug.
The code I am using does work. The problem is that it won't add the pages
to the menu, unless I am logged in. Most likely this is me falling foul of
the 'edit_posts' capability again.
Tom
On 16 November 2011 16:42, Doug Stewart <zamoose at gmail.com> wrote:
> Here's what I do to set up a custom BuddyPress menu when I detect BP
> as active. It seems pretty close to what you're doing. Maybe it'll
> help.
>
> $menuname = $lblg_themename . ' BuddyPress Menu';
> $bpmenulocation = 'lblgbpmenu';
> // Does the menu exist already?
> $menu_exists = wp_get_nav_menu_object( $menuname );
>
> // If it doesn't exist, let's create it.
> if( !$menu_exists){
> $menu_id = wp_create_nav_menu($menuname);
>
> // Set up default BuddyPress links and add them to the menu.
> wp_update_nav_menu_item($menu_id, 0, array(
> 'menu-item-title' => __('Home'),
> 'menu-item-classes' => 'home',
> 'menu-item-url' => home_url( '/' ),
> 'menu-item-status' => 'publish'));
>
> wp_update_nav_menu_item($menu_id, 0, array(
> 'menu-item-title' => __('Activity'),
> 'menu-item-classes' => 'activity',
> 'menu-item-url' => home_url( '/activity/' ),
> 'menu-item-status' => 'publish'));
>
> wp_update_nav_menu_item($menu_id, 0, array(
> 'menu-item-title' => __('Members'),
> 'menu-item-classes' => 'members',
> 'menu-item-url' => home_url( '/members/' ),
> 'menu-item-status' => 'publish'));
>
> wp_update_nav_menu_item($menu_id, 0, array(
> 'menu-item-title' => __('Groups'),
> 'menu-item-classes' => 'groups',
> 'menu-item-url' => home_url( '/groups/' ),
> 'menu-item-status' => 'publish'));
>
> wp_update_nav_menu_item($menu_id, 0, array(
> 'menu-item-title' => __('Forums'),
> 'menu-item-classes' => 'forums',
> 'menu-item-url' => home_url( '/forums/' ),
> 'menu-item-status' => 'publish'));
>
> // Grab the theme locations and assign our newly-created
> menu
> // to the BuddyPress menu location.
> if( !has_nav_menu( $bpmenulocation ) ){
> $locations = get_theme_mod('nav_menu_locations');
> $locations[$bpmenulocation] = $menu_id;
> set_theme_mod( 'nav_menu_locations', $locations );
> }
>
> }
>
> On Wed, Nov 16, 2011 at 7:14 AM, Tom Barrett <tcbarrett at gmail.com> wrote:
> > 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
> > _______________________________________________
> > wp-hackers mailing list
> > wp-hackers at lists.automattic.com
> > http://lists.automattic.com/mailman/listinfo/wp-hackers
> >
>
>
>
> --
> -Doug
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>
--
http://www.tcbarrett.com | http://gplus.to/tcbarrett |
http://twitter.com/tcbarrett
More information about the wp-hackers
mailing list