Skip to content

@roine /functions.php
Last active

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
  1. roine revised this gist . 1 changed file with 2 additions and 1 deletion.
    View
    3  functions.php
    @@ -9,7 +9,8 @@ function add_custom_box_wpse_87594()
    __( 'Available Nav-Menus' ),
    'inner_custom_box_wpse_87594',
    'post',
    - 'side'
    + 'side',
    + 'high'
    );
    }
  2. roine revised this gist . 1 changed file with 1 addition and 0 deletions.
    View
    1  functions.php
    @@ -41,6 +41,7 @@ function inner_custom_box_wpse_87594()
    }
    echo '</select>';
    + }
    }
    function save_postdata_wpse_87594( $post_id, $post_object )
  3. roine revised this gist . 1 changed file with 24 additions and 13 deletions.
    View
    37 functions.php
    @@ -27,12 +27,18 @@ function inner_custom_box_wpse_87594()
    foreach( $navmenus as $m )
    {
    - printf(
    - '<option value="%s">%s</option>',
    - $m->term_id,
    - $m->name
    - );
    - }
    + echo "<optgroup label='$m->name'>";
    + echo "<option value={$m->term_id}>&#8593; Add on top</option>";
    + $items = wp_get_nav_menu_items( $m->term_id);
    + foreach ($items as $key => $menu_item ) {
    + if(!$menu_item->menu_item_parent){
    + printf('<option value="%s-%s">%s</option>',
    + $m->term_id,
    + $menu_item->object_id,
    + $menu_item->title);
    + }
    + echo "</optgroup>";
    + }
    echo '</select>';
    }
    @@ -56,16 +62,21 @@ function save_postdata_wpse_87594( $post_id, $post_object )
    if( !empty( $_POST['nav_menus_dropdown'] ) )
    {
    + $parents = explode('-', $_POST['nav_menus_dropdown']);
    + $item = array(
    + 'menu-item-title' => $_POST['post_title'],
    + 'menu-item-object' => 'page',
    + 'menu-item-object-id' => $_POST['ID'],
    + 'menu-item-type' => 'post_type',
    + 'menu-item-status' => 'publish'
    + );
    + if(count($parents) > 1){
    + $item['menu-item-parent-id'] = $parents[1];
    + }
    wp_update_nav_menu_item(
    $_POST['nav_menus_dropdown'],
    0,
    - array(
    - 'menu-item-title' => $_POST['post_title'],
    - 'menu-item-object' => 'page',
    - 'menu-item-object-id' => $_POST['ID'],
    - 'menu-item-type' => 'post_type',
    - 'menu-item-status' => 'publish'
    - )
    + $item
    );
    }
    }
  4. roine revised this gist . 1 changed file with 1 addition and 0 deletions.
    View
    1  functions.php
    @@ -1,3 +1,4 @@
    +<?php
    add_action( 'add_meta_boxes', 'add_custom_box_wpse_87594' );
    add_action( 'save_post', 'save_postdata_wpse_87594', 10, 2 );
  5. roine renamed this gist . 1 changed file with 0 additions and 0 deletions.
    View
    0  gistfile1.txt → functions.php
    File renamed without changes
  6. roine created this gist .
    View
    70 gistfile1.txt
    @@ -0,0 +1,70 @@
    +add_action( 'add_meta_boxes', 'add_custom_box_wpse_87594' );
    +add_action( 'save_post', 'save_postdata_wpse_87594', 10, 2 );
    +
    +function add_custom_box_wpse_87594()
    +{
    + add_meta_box(
    + 'section_id_wpse_87594',
    + __( 'Available Nav-Menus' ),
    + 'inner_custom_box_wpse_87594',
    + 'post',
    + 'side'
    + );
    +}
    +
    +function inner_custom_box_wpse_87594()
    +{
    + $navmenus = wp_get_nav_menus( array( 'hide_empty' => false, 'orderby' => 'none' ) );
    +
    + // DEBUG
    + // echo '<pre>' . print_r( $navmenus, true ) . '</pre>';
    +
    + wp_nonce_field( plugin_basename( __FILE__ ), 'noncename_wpse_87594' );
    +
    + echo '<select name="nav_menus_dropdown" id="nav_menus_dropdown">
    + <option value="">- Select -</option>';
    +
    + foreach( $navmenus as $m )
    + {
    + printf(
    + '<option value="%s">%s</option>',
    + $m->term_id,
    + $m->name
    + );
    + }
    +
    + echo '</select>';
    +}
    +
    +function save_postdata_wpse_87594( $post_id, $post_object )
    +{
    + // Auto save?
    + if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
    + return;
    +
    + // Security
    + if (
    + !isset( $_POST['noncename_wpse_87594'] )
    + || !wp_verify_nonce( $_POST['noncename_wpse_87594'], plugin_basename( __FILE__ ) )
    + )
    + return;
    +
    + // Correct post_type
    + if ( 'post' != $post_object->post_type )
    + return;
    +
    + if( !empty( $_POST['nav_menus_dropdown'] ) )
    + {
    + wp_update_nav_menu_item(
    + $_POST['nav_menus_dropdown'],
    + 0,
    + array(
    + 'menu-item-title' => $_POST['post_title'],
    + 'menu-item-object' => 'page',
    + 'menu-item-object-id' => $_POST['ID'],
    + 'menu-item-type' => 'post_type',
    + 'menu-item-status' => 'publish'
    + )
    + );
    + }
    +}
Something went wrong with that request. Please try again.