Skip to content

Instantly share code, notes, and snippets.

@benhuson
Created September 2, 2015 13:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save benhuson/1778327edb428169733b to your computer and use it in GitHub Desktop.
Save benhuson/1778327edb428169733b to your computer and use it in GitHub Desktop.
Manage WordPress Orphan Menu Items (drop-in)
<?php
class Manage_Orphan_Menu_Items {
function Manage_Orphan_Menu_Items() {
add_action( 'admin_init', array( $this, 'process_menu_item_delete' ) );
add_action( 'admin_menu', array( $this, 'add_admin_page' ) );
}
function add_admin_page() {
add_submenu_page( 'tools.php', 'Orphan Menu Items', 'Orphan Menu Items', 'edit_theme_options', 'manage_orphan_menu_items', array( $this, 'options_page' ) );
}
function process_menu_item_delete() {
if ( isset( $_GET['delete_nav_item_nonce'] ) && wp_verify_nonce( $_GET['delete_nav_item_nonce'], 'delete_nav_item' ) && isset( $_GET[ 'delete_nav_item' ] ) ) {
wp_delete_post( absint( $_GET[ 'delete_nav_item' ] ), true );
}
}
function options_page() {
?>
<div class="wrap">
<?php
screen_icon();
echo '<h1>' . __( 'Manage Orphan Menu Items' ) . '</h1>';
$q = new WP_Query( array(
'post_type' => 'nav_menu_item',
'nopaging' => true,
'order' => 'ASC',
'orderby' => 'menu_order',
'posts_per_page' => -1
) );
if ( $q->have_posts() ) {
echo '<table>';
echo '<tr><th align="left">Menu Item</th><th align="left">Object Type</th><th align="left">Menu</th><th></th></tr>';
while ( $q->have_posts() ) {
$q->the_post();
echo '<tr><td style="border-top: 1px solid #ccc;">';
the_title();
$o_id = get_post_meta( get_the_ID(), '_menu_item_object_id', true );
$o_type = get_post_meta( get_the_ID(), '_menu_item_type', true );
$o_object = get_post_meta( get_the_ID(), '_menu_item_object', true );
if ( 'post_type' == $o_type ) {
echo ' <strong>' . get_the_title( $o_id ) . '</strong>';
} elseif ( 'taxonomy' == $o_type ) {
$trm = get_term( $o_id, $o_object );
echo ' <strong>' . $trm->name . '</strong>';
}
echo '</td><td style="border-top: 1px solid #ccc;">';
printf( ' (%s, %s, %s)', $o_id, $o_type, $o_object );
echo '</td><td style="border-top: 1px solid #ccc;">';
$t = wp_get_object_terms( get_the_ID(), 'nav_menu', array( 'fields' => 'names' ) );
if ( $t && ! is_wp_error( $t ) ) {
echo implode( ', ', $t );
}
echo '</td><td style="border-top: 1px solid #ccc;">';
if ( $t && ! is_wp_error( $t ) ) {
} else {
$del_url = wp_nonce_url( add_query_arg( array( 'delete_nav_item' => get_the_ID() ) ), 'delete_nav_item', 'delete_nav_item_nonce' );
printf( '<a href="%s">Delete</a>', $del_url );
}
echo '</td></tr>';
}
echo '</table>';
wp_reset_postdata();
}
?>
</div>
<?php
}
}
new Manage_Orphan_Menu_Items();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment