Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Is it possible to have new pages automatically added to the primary menu via functions.php?

This particular theme will be deployed on a special version of multisite, so the checkbox which is available via admin isn't an option.

I can't seem to find anything in codex or on here.

share|improve this question
1  
Did you see the checkbox that makes exactly that possibly at the bottom at Design>Menus? –  kraftner Mar 24 at 17:42
    
I did @kraftner. This particular theme will be deployed on a special version of multisite where the editor will have little input via admin. –  Squideyes Mar 24 at 17:50
    
Please show some effort. There are numerous questions here that should get you started. –  s_ha_dum Mar 24 at 18:02
    
I would make some effort if I could find some examples via codex. –  Squideyes Mar 24 at 18:10
    
I believe it was suggested that there are many related questions available here on this site. Just off the top of my head, the wp_nav_menu_items filter might be pertinent. –  helgatheviking Mar 24 at 18:17

1 Answer 1

up vote 1 down vote accepted

Not a specific answer, but will be too long for a comment. You could also try looking at the code that WordPress core uses itself. I know you said you couldn't use the check box that auto-adds pages, but you could look over how it is being used. A little digging around (with a decent text editor you should be able to search for a phrase throughout the entire WordPress code base) in the WordPress code reveals:

/**
 * Automatically add newly published page objects to menus with that as an option.
 *
 * @since 3.0.0
 * @access private
 *
 * @param string $new_status The new status of the post object.
 * @param string $old_status The old status of the post object.
 * @param object $post The post object being transitioned from one status to another.
 * @return void
 */
function _wp_auto_add_pages_to_menu( $new_status, $old_status, $post ) {
    if ( 'publish' != $new_status || 'publish' == $old_status || 'page' != $post->post_type )
        return;
    if ( ! empty( $post->post_parent ) )
        return;
    $auto_add = get_option( 'nav_menu_options' );
    if ( empty( $auto_add ) || ! is_array( $auto_add ) || ! isset( $auto_add['auto_add'] ) )
        return;
    $auto_add = $auto_add['auto_add'];
    if ( empty( $auto_add ) || ! is_array( $auto_add ) )
        return;

    $args = array(
        'menu-item-object-id' => $post->ID,
        'menu-item-object' => $post->post_type,
        'menu-item-type' => 'post_type',
        'menu-item-status' => 'publish',
    );

    foreach ( $auto_add as $menu_id ) {
        $items = wp_get_nav_menu_items( $menu_id, array( 'post_status' => 'publish,draft' ) );
        if ( ! is_array( $items ) )
            continue;
        foreach ( $items as $item ) {
            if ( $post->ID == $item->object_id )
                continue 2;
        }
        wp_update_nav_menu_item( $menu_id, 0, $args );
    }
}

This code is run on:

add_action( 'transition_post_status',     '_wp_auto_add_pages_to_menu', 10, 3 );

Maybe this will point you in the right direction and perhaps you can copy what WP is doing to do something similar.

share|improve this answer
    
Thank you @helgatheviking. I'm fairly new to WP development, so your input has been very useful. I always try and find an answer myself, but on this occasion I couldn't seem to find too much on the subject. #RockOn –  Squideyes Mar 24 at 18:44
    
Very glad to hear it. Unfortunately there are plenty of people who treat this site as a "do my work for me" site, hence the request to show some research effort in your question. –  helgatheviking Mar 24 at 18:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.