add_cap( 'administrator', 'add_to_menu' );
$wp_roles->add_cap( 'administrator', 'add_to_menu_settings' );
}
//
// Options page
//
//
/**
* Hook to register options admin page
*/
add_action( 'admin_menu', 'add_to_menu__menu' );
function add_to_menu__menu() {
add_options_page( __('Add to menu settings'), __('Add to menu'), 'add_to_menu_settings', 'add-to-menu', 'add_to_menu__options' );
}
/**
* Hook to admin_init to register settings
*/
function add_to_menu__register_settings(){
register_setting( 'add-to-menu', 'add_to_menu_post_types_to_handle' );
}
add_action( 'admin_init', 'add_to_menu__register_settings' );
/**
* Handle the options page
*/
function add_to_menu__options(){
?>
ancestors)) {
$active_post = get_post( $edit_post->ancestors[0] );
} else {
$active_post = $post;
}
$get_args = array('meta_key' => '_menu_item_object_id', 'meta_value' => $active_post->ID, 'post_status' => 'any' );
$menus = wp_get_nav_menus( array('orderby' => 'name') );
foreach( $menus as $id => $menu ){
$has_menu_item = wp_get_nav_menu_items($menu->term_id, $get_args);
$has_menu_item = count($has_menu_item) > 0;
echo '';
}
}
/**
* Hook save_post
*
* Processes the 'Add to menu' meta box
*/
add_action('save_post', 'add_to_menu__save_post', 20, 2);
function add_to_menu__save_post( $post_id, $edit_post ) {
// no need for this if it is a nav_menu_item
if($post->post_type == 'nav_menu_item' || !isset($_POST['add_to_menu'])) {
return;
}
// Get the actually post object instead of a revision post
if(isset($edit_post->ancestors)) {
$post_id = $edit_post->ancestors[0];
}
$post = get_post($post_id);
// get the menus to add the menu item to
$menus = $_POST['add_to_menu'];
unset($_POST['add_to_menu']); // to prevent it getting handled again
foreach($menus as $menu_id => $v ){
// Find if the post has a parent post, if so try to put it underneath the parent in the menu
$parent_id = 0;
$walk_post = clone $post;
// If this is the first time the post is being saved (hence being inserted into the DB),
// the post_parent is not set for some reason, then grab it from $_POST['parent_id']
if( empty($walk_post->post_parent) && !empty($_POST['parent_id']) ){
$walk_post->post_parent = $_POST['parent_id'];
}
while( $parent_id == 0 && $walk_post->post_parent != 0 ){
$get_args = array('meta_key' => '_menu_item_object_id', 'meta_value' => $walk_post->post_parent, 'post_status' => 'any' );
$parent_menu_items = wp_get_nav_menu_items($menu_id, $get_args);
if( count($parent_menu_items) > 0 ){
$parent_id = $parent_menu_items[0]->ID;
}
$walk_post = get_post( $walk_post->post_parent );
}
$menu_item_data = array(
'menu-item-object' => $post->post_type,
'menu-item-object-id' => $post_id,
'menu-item-parent-id' => $parent_id, // the parent menu item I think
// 'menu-item-position' => 0,
'menu-item-type' => 'post_type',
'menu-item-title' => $post->post_title,
'menu-item-status' => 'draft',
'menu-item-url' => get_permalink( $post_id ),
);
wp_update_nav_menu_item( $menu_id, 0, $menu_item_data);
}
}