Instantly share code, notes, and snippets.
Last active
April 25, 2016 21:58
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save smhmic/fb6432a9326fcb72a0b8ecfc596c5761 to your computer and use it in GitHub Desktop.
Add an option for nav menus for new child pages of contained parent pages to be automatically added as sub items under that page in the nav.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// | |
// ADD OPTION IN wp-admin/nav-menus.php | |
// | |
#\pfxwp\hook::add_filter_after( 'pre_option_nav_menu_options', 'gettext', 'pfxwp_nav_option_auto_add_subpages' ); | |
add_filter( 'pre_option_nav_menu_options', 'pfxwp_nav_option_hook' ); | |
function pfxwp_nav_option_hook( $v ) { | |
remove_filter( 'pre_option_nav_menu_options', 'pfxwp_nav_option_hook' ); | |
add_filter( 'gettext', 'pfxwp_nav_option_auto_add_subpages' ); | |
return $v; | |
} | |
function pfxwp_nav_option_auto_add_subpages( $translations ) { | |
if ( 'Automatically add new top-level pages to this menu' === $translations ) { | |
remove_filter( 'gettext', 'pfxwp_nav_option_auto_add_subpages' ); | |
global $nav_menu_selected_id; | |
//$nav_menu_selected_id = isset( $_REQUEST['menu'] ) ? (int) $_REQUEST['menu'] : 0; | |
$auto_add = get_option( 'nav_menu_options' ); | |
if ( ! isset( $auto_add['auto_add_subpages'] ) ) | |
$auto_add = false; | |
elseif ( false !== array_search( $nav_menu_selected_id, $auto_add['auto_add_subpages'] ) ) | |
$auto_add = true; | |
else | |
$auto_add = false; | |
$r = ''; | |
$r .= $translations; | |
$r .= '</label></dd>'; | |
$r .= '<dd class="checkbox-input"> | |
<input type="checkbox"' . checked( $auto_add, true, false ) . ' name="auto-add-subpages" id="auto-add-subpages" value="1"> | |
<label for="auto-add-subpages">Automatically add new subpages to this menu</label> | |
</dd>'; | |
return $r; | |
} | |
return $translations; | |
} | |
\pfxwp\hook::add_filter( 'wp_update_nav_menu', function ( $nav_menu_selected_id ) { | |
if ( ! $nav_menu_selected_id ) | |
return; | |
$auto_add = ! empty( $_POST['auto-add-subpages'] ); | |
$nav_menu_option = (array) get_option( 'nav_menu_options' ); | |
if ( ! isset( $nav_menu_option['auto_add_subpages'] ) ) | |
$nav_menu_option['auto_add_subpages'] = array(); | |
if ( $auto_add ) { | |
if ( ! in_array( $nav_menu_selected_id, $nav_menu_option['auto_add_subpages'] ) ) | |
$nav_menu_option['auto_add_subpages'][] = $nav_menu_selected_id; | |
} else { | |
if ( false !== ( $key = array_search( $nav_menu_selected_id, | |
$nav_menu_option['auto_add_subpages'] ) ) | |
) | |
unset( $nav_menu_option['auto_add_subpages'][ $key ] ); | |
} | |
// Remove nonexistent/deleted menus | |
$nav_menu_option['auto_add_subpages'] = array_intersect( $nav_menu_option['auto_add_subpages'], | |
wp_get_nav_menus( array( 'fields' => 'ids' ) ) ); | |
update_option( 'nav_menu_options', $nav_menu_option ); | |
} ); | |
/////////////////////////////////////////////////////////////////////////////// | |
/////////////////////////////////////////////////////////////////////////////// | |
/////////////////////////////////////////////////////////////////////////////// | |
/* | |
Based on Auto Submenu Plugin by Jamo Web Creations | |
http://jamocreations.com/en/artikelen/auto-submenu | |
*/ | |
class AutoSubmenu { | |
/** | |
* Constructor | |
*/ | |
function __construct() { | |
add_action( 'publish_page', array( &$this, 'on_publish_page' ) ); | |
} | |
/** | |
* When publishing a new child page, add it to the appropriate custom menu. | |
*/ | |
function on_publish_page( $post_id ) { | |
// Theme supports custom menus? | |
if ( ! current_theme_supports( 'menus' ) ) { | |
return; | |
} | |
// Published page has parent? | |
$post = get_post( $post_id ); | |
if ( ! $post->post_parent ) { | |
return; | |
} | |
// Get menus with auto_add enabled | |
$auto_add = get_option( 'nav_menu_options' ); | |
if ( empty( $auto_add ) || ! is_array( $auto_add ) || ! isset( $auto_add['auto_add_subpages'] ) ) { | |
return; | |
} | |
$auto_add = $auto_add['auto_add_subpages']; | |
if ( empty( $auto_add ) || ! is_array( $auto_add ) ) { | |
return; | |
} | |
// Loop through the menus to find page parent | |
foreach ( $auto_add as $menu_id ) { | |
$menu_parent = null; | |
$menu_items = wp_get_nav_menu_items( $menu_id, | |
array( 'post_status' => 'publish,draft' ) ); | |
if ( ! is_array( $menu_items ) ) { | |
continue; | |
} | |
foreach ( $menu_items as $menu_item ) { | |
// Item already in menu? | |
if ( $menu_item->object_id == $post->ID ) { | |
continue 2; | |
} | |
if ( $menu_item->object_id == $post->post_parent ) { | |
$menu_parent = $menu_item; | |
} | |
} | |
// Add new item | |
if ( $menu_parent ) { | |
wp_update_nav_menu_item( $menu_id, 0, array( | |
'menu-item-object-id' => $post->ID, | |
'menu-item-object' => $post->post_type, | |
'menu-item-parent-id' => $menu_parent->ID, | |
'menu-item-type' => 'post_type', | |
'menu-item-status' => 'publish' | |
) ); | |
} | |
} | |
} | |
} | |
$auto_submenu = new AutoSubmenu(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment