How to get page id using page slugs in wordpress

One of my visitors asked me how to get page id using page slug and i wrote a small snippet to easily achieve this in a simple loop. Follow read more link to see the snippet.

To get the page ids we are going to use get_page_by_path function which returns a page using its path. This also works with slugs. Here is the snippet:

// Page slugs to be excluded
$exclude_pages = array('contact', 'tools', 'about');

// Generates list of ids to be excluded
$exclude = '';
foreach ( $exclude_pages as $path ){
	$page = get_page_by_path( $path );
	$exclude .= $page->ID . ',';
}

// Use the exlusion in dropdown arguments
$args =array(
    'menu' => 'pages',
    'exclude' => $exclude,
    'login' => true,
    'admin' => false,
    'vertical' => false,
    'home' => true,
    'align' => 'right',
    'width' => '500'
);

// Output dropdown html
shailan_dropdown_menu( $args );

This code will simply filter out pages given in the $exclude_pages array in your dropdown menu.

You can use this code with wp_list_pages function also.