Need help debugging two lines of code.

I am debugging two lines of code and need help getting it done.

printf( '<li><label><input type="checkbox" name="menu_manager[%1$s][0]" id="menu_manager-%1$s" value="1" %2$s/> %3$s</label>', $m[2], checked( $hidden[$m[2]][0], 1, false ), $m[0] );

printf( '
<li><label><input type="checkbox" name="menu_manager[%1$s][%2$s]" id="menu_manager-%1$s-%2$s" value="1" %3$s/> %4$s</label></li>
', $m[2], $s[2], checked( $hidden[$m[2]][$s[2]], 1, false ), $s[0] )

The error message for both lines is Notice: Undefined index: index.php, Vinod Dalvi helped me out some time ago with a similar problem and it was greatly appreciated.

Any help would be great.
Thanks, Vic

  • Site Division

    @Ashok The full message just shows the path. The last time I had this problem I used if(isset but these two lines are a bit more complicated.

    Notice: Undefined index: edit.php in /usr/share/nginx/sitedivision/wp-content/plugins/super-admin-menu-manager/sa-menu-manager.php on line 67

    Notice: Undefined index: edit.php in /usr/share/nginx/sitedivision/wp-content/plugins/super-admin-menu-manager/sa-menu-manager.php on line 71

  • Ash

    Hi there

    Would you please use the following codes before those two lines?

    $m 				= ! isset( $m ) || ! is_array( $m ) ? array() : $m;
    $m[2] 				= ! isset( $m[2] ) ? '' : $m[2];
    $m[0] 				= ! isset( $m[0] ) ? '' : $m[0];
    $hidden[$m[2]] 			= ! isset( $hidden[$m[2]] ) || ! is_array( $hidden[$m[2]] ) ? array() : $m;
    $hidden[$m[2]][0] 		= ! isset( $hidden[$m[2]][0] ) ? '' : $hidden[$m[2]][0];
    
    $s[2] 				= ! isset( $s[2] ) ? '' : $s[2];
    $s[0] 				= ! isset( $s[0] ) ? '' : $s[0];
    $hidden[$m[2]][$s[2]] 		= ! isset( $hidden[$m[2]][$s[2]] ) ? '' : $hidden[$m[2]][$s[2]];

    Please let me know if it works.

    Cheers
    Ash

  • Site Division

    @Ashok
    Thanks for helping me with this. :slight_smile:
    This is working as far as getting rid of the error, but the only issue is that the code is calling all the admin menu items on the main site. So if calling "Posts" I would normally get "Posts with the sub-menu items "All Posts", "Add New", "Categories" and "Tags". But now I'm only getting the last sub-menu item in each category. So I'm only getting "Posts" with the sub-menu Item "Tags" as seen in the screenshot.

  • Ash

    Hi there

    Would you please try this one?

    $m 				= ! isset( $m ) || ! is_array( $m ) ? array() : $m;
    $m[2] 				= ! isset( $m[2] ) ? '' : $m[2];
    $m[0] 				= ! isset( $m[0] ) ? '' : $m[0];
    $hidden[$m[2]] 			= ! isset( $hidden[$m[2]] ) || ! is_array( $hidden[$m[2]] ) ? array() : $hidden[$m[2]];
    $hidden[$m[2]][0] 		= ! isset( $hidden[$m[2]][0] ) ? '' : $hidden[$m[2]][0];
    
    $s[2] 				= ! isset( $s[2] ) ? '' : $s[2];
    $s[0] 				= ! isset( $s[0] ) ? '' : $s[0];
    $hidden[$m[2]][$s[2]] 		= ! isset( $hidden[$m[2]][$s[2]] ) ? '' : $hidden[$m[2]][$s[2]];

    Hope it helps :slight_smile: Please feel free to ask more question if you have any.

    Cheers
    Ash

  • Site Division

    @Ashok

    The entire code is below, the first line you have provided seems like its ok as it removes the error for the main menu item. The second line if removed gives back all the sub-menu items but the error for that line returns.

    <?php
    
    function samm_menu_manager_admin_menu() {
    	if( !is_super_admin() ) {
    		$hidden = samm_menu_manager_get_hidden();
    		foreach( $hidden as $menu => $items ) {
    			if( !empty( $items[0] ) )
    				remove_menu_page( $menu );
    			else {
    				foreach( array_keys( $items ) as $sub )
    					remove_submenu_page( $menu, $sub );
    			}
    		}
    	} else
    		add_management_page( __( 'Manage Menus', '' ), __( 'Manage Menus', '' ), 'manage_network_options', 'sa-menu-manager', 'samm_menu_manager_page' );
    }
    
    add_action( 'admin_menu', 'samm_menu_manager_admin_menu', 1000 );
    
    function samm_menu_manager_css() {
    	if( $_REQUEST['page'] == 'sa-menu-manager' || basename( $_SERVER['SCRIPT_NAME'] ) == 'post.php' )
    		wp_enqueue_style( 'sa-menu-manager', plugin_dir_url( __FILE__ ) . 'css/menu-manager.css' );
    }
    add_action( 'admin_init', 'samm_menu_manager_css' );
    
    function samm_menu_manager_page() {
    	global $menu, $submenu;
    	echo "<div id='sa-menu-manager' class='wrap'>";
    	if( !empty( $_POST['menu_manager'] ) ) {
    		samm_menu_manager_update( stripslashes_deep( $_POST['menu_manager'] ) );
    		echo '<div class="updated">' . __( 'Menus Updated', 'premium-manager' ) . '</div>';
    	} elseif( !empty( $_POST ) ) {
    		samm_menu_manager_update( array() );
    		echo '<div class="updated">' . __( 'Menus Cleared', 'premium-manager' ) . '</div>';
    	}
    	$hidden = samm_menu_manager_get_hidden();
    
    	echo '<h2>' . __( 'Manage Menus', 'premium-manager' ) . "</h2>\n";
    	echo '<em>' . __( 'Checked Menus are not shown to Site Admins', 'premium-manager' ) . "</em><form method='post'>\n";
    	wp_nonce_field( 'sa_menu_manager' );
    	echo "<p><input type='submit' class='button-secondary' value='" . __( 'Update', 'premium-manager' ) . "' />";
    	echo '&nbsp;<label><input type="checkbox" name="menu-reset" value="1" />&nbsp;<strong>' . sprintf( __( 'Reset Default Menus for %s', 'premium-manager' ), is_main_site() ? 'Network' : 'Site' ) . "</strong></label>\n";
    	if( !is_main_site() )
    		echo '&nbsp;<label><input type="checkbox" name="menu-exclude" value="1" ' . checked( true, empty( $hidden ), false ) . ' />&nbsp;<strong>' . __( 'Show all menus on this site', 'premium-manager' ) . "</strong></label>\n";
    	echo "</p><ul>\n";
    	foreach( $menu as $m ) {
    		if( empty( $m[0] ) )
    			continue;
    			$m 	= ! isset( $m ) || ! is_array( $m ) ? array() : $m; $m[2] = ! isset( $m[2] ) ? '' : $m[2]; $m[0] = ! isset( $m[0] ) ? '' : $m[0]; $hidden[$m[2]] = ! isset( $hidden[$m[2]] ) || ! is_array( $hidden[$m[2]] ) ? array() : $hidden[$m[2]]; $hidden[$m[2]][0] 		= ! isset( $hidden[$m[2]][0] ) ? '' : $hidden[$m[2]][0];
    		printf( '<li><label><input type="checkbox" name="menu_manager[%1$s][0]" id="menu_manager-%1$s" value="1" %2$s/>&nbsp;%3$s</label>', $m[2], checked( $hidden[$m[2]][0], 1, false ), $m[0] );
    		if( !empty( $submenu[$m[2]] ) ) {
    			echo "<ul>\n";
    			foreach( $submenu[$m[2]] as $s )
    			$s[2] = ! isset( $s[2] ) ? '' : $s[2]; $s[0] = ! isset( $s[0] ) ? '' : $s[0]; $hidden[$m[2]][$s[2]] 		= ! isset( $hidden[$m[2]][$s[2]] ) ? '' : $hidden[$m[2]][$s[2]];
    			printf( '<li><label><input type="checkbox" name="menu_manager[%1$s][%2$s]" id="menu_manager-%1$s-%2$s" value="1" %3$s/>&nbsp;%4$s</label></li>', $m[2], $s[2], checked( $hidden[$m[2]][$s[2]], 1, false ), $s[0] );
    			echo "</ul>\n";
    		}
    		echo "<div style='clear: left;'>&nbsp;</div></li>\n";
    	}
    	echo "</ul></form></div>\n";
    }
    function samm_menu_manager_update( $hidden ) {
    	check_admin_referer( 'sa_menu_manager' );
    	if( !empty( $_POST['menu-reset'] ) && '1' == $_POST['menu-reset'] )
    		$hidden = array();
    
    	if( !is_multisite() || is_main_site() )
    		update_site_option( 'sa_hidden_menus', $hidden );
    	else {
    		if( !empty( $_POST['menu-exclude'] ) && '1' == $_POST['menu-exclude'] )
    			update_option( 'sa_hidden_menus', array() );
    		elseif( empty( $hidden ) )
    			delete_option( 'sa_hidden_menus' );
    		else
    			update_option( 'ra_hidden_menus', $hidden );
    	}
    }
    function samm_menu_manager_get_hidden() {
    	if( !is_multisite() || is_main_site() || ( $hidden = get_option( 'sa_hidden_menus' ) ) === false )
    		$hidden = get_site_option( 'sa_hidden_menus', array() );
    
    	return $hidden;
    }
  • Ash

    Hi there

    Please try the following code:

    function samm_menu_manager_admin_menu() {
    	if( !is_super_admin() ) {
    		$hidden = samm_menu_manager_get_hidden();
    		foreach( $hidden as $menu => $items ) {
    			if( !empty( $items[0] ) )
    				remove_menu_page( $menu );
    			else {
    				foreach( array_keys( $items ) as $sub )
    					remove_submenu_page( $menu, $sub );
    			}
    		}
    	} else
    		add_management_page( __( 'Manage Menus', '' ), __( 'Manage Menus', '' ), 'manage_network_options', 'sa-menu-manager', 'samm_menu_manager_page' );
    }
    
    add_action( 'admin_menu', 'samm_menu_manager_admin_menu', 1000 );
    
    function samm_menu_manager_css() {
    	if( $_REQUEST['page'] == 'sa-menu-manager' || basename( $_SERVER['SCRIPT_NAME'] ) == 'post.php' )
    		wp_enqueue_style( 'sa-menu-manager', plugin_dir_url( __FILE__ ) . 'css/menu-manager.css' );
    }
    add_action( 'admin_init', 'samm_menu_manager_css' );
    
    function samm_menu_manager_page() {
    	global $menu, $submenu;
    	echo "<div id='sa-menu-manager' class='wrap'>";
    	if( !empty( $_POST['menu_manager'] ) ) {
    		samm_menu_manager_update( stripslashes_deep( $_POST['menu_manager'] ) );
    		echo '<div class="updated">' . __( 'Menus Updated', 'premium-manager' ) . '</div>';
    	} elseif( !empty( $_POST ) ) {
    		samm_menu_manager_update( array() );
    		echo '<div class="updated">' . __( 'Menus Cleared', 'premium-manager' ) . '</div>';
    	}
    	$hidden = samm_menu_manager_get_hidden();
    
    	echo '<h2>' . __( 'Manage Menus', 'premium-manager' ) . "</h2>\n";
    	echo '<em>' . __( 'Checked Menus are not shown to Site Admins', 'premium-manager' ) . "</em><form method='post'>\n";
    	wp_nonce_field( 'sa_menu_manager' );
    	echo "<p><input type='submit' class='button-secondary' value='" . __( 'Update', 'premium-manager' ) . "' />";
    	echo '&nbsp;<label><input type="checkbox" name="menu-reset" value="1" />&nbsp;<strong>' . sprintf( __( 'Reset Default Menus for %s', 'premium-manager' ), is_main_site() ? 'Network' : 'Site' ) . "</strong></label>\n";
    	if( !is_main_site() )
    		echo '&nbsp;<label><input type="checkbox" name="menu-exclude" value="1" ' . checked( true, empty( $hidden ), false ) . ' />&nbsp;<strong>' . __( 'Show all menus on this site', 'premium-manager' ) . "</strong></label>\n";
    	echo "</p><ul>\n";
    	foreach( $menu as $m ) {
    		if( empty( $m[0] ) )
    			continue;
    			$hidden[$m[2]] = isset( $hidden[$m[2]] ) ? $hidden[$m[2]] : array();
                            $hidden[$m[2]][0] = isset( $hidden[$m[2]][0] ) ? $hidden[$m[2]][0] : '';
    		printf( '<li><label><input type="checkbox" name="menu_manager[%1$s][0]" id="menu_manager-%1$s" value="1" %2$s/>&nbsp;%3$s</label>', $m[2], checked( $hidden[$m[2]][0], 1, false ), $m[0] );
    		if( !empty( $submenu[$m[2]] ) ) {
    			echo "<ul>\n";
    			foreach( $submenu[$m[2]] as $s ){
                                $hidden[$m[2]] = isset( $hidden[$m[2]] ) ? $hidden[$m[2]] : array();
                                $hidden[$m[2]][$s[2]] = isset( $hidden[$m[2]][$s[2]] ) ? $hidden[$m[2]][$s[2]] : '';
                                printf( '<li><label><input type="checkbox" name="menu_manager[%1$s][%2$s]" id="menu_manager-%1$s-%2$s" value="1" %3$s/>&nbsp;%4$s</label></li>', $m[2], $s[2], checked( $hidden[$m[2]][$s[2]], 1, false ), $s[0] );
                            }
    			echo "</ul>\n";
    		}
    		echo "<div style='clear: left;'>&nbsp;</div></li>\n";
    	}
    	echo "</ul></form></div>\n";
    }
    function samm_menu_manager_update( $hidden ) {
    	check_admin_referer( 'sa_menu_manager' );
    	if( !empty( $_POST['menu-reset'] ) && '1' == $_POST['menu-reset'] )
    		$hidden = array();
    
    	if( !is_multisite() || is_main_site() )
    		update_site_option( 'sa_hidden_menus', $hidden );
    	else {
    		if( !empty( $_POST['menu-exclude'] ) && '1' == $_POST['menu-exclude'] )
    			update_option( 'sa_hidden_menus', array() );
    		elseif( empty( $hidden ) )
    			delete_option( 'sa_hidden_menus' );
    		else
    			update_option( 'ra_hidden_menus', $hidden );
    	}
    }
    function samm_menu_manager_get_hidden() {
    	if( !is_multisite() || is_main_site() || ( $hidden = get_option( 'sa_hidden_menus' ) ) === false )
    		$hidden = get_site_option( 'sa_hidden_menus', array() );
    
    	return $hidden;
    }

    Hope it helps :slight_smile: Please feel free to ask more question if you have any.

    Cheers
    Ash

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.